1 Star 0 Fork 0

OOSAD-软工21011-21012/高圣杰1213坦克大战

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TankClient.java 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
高圣杰 提交于 2023-05-06 21:25 . v17
package V17;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(50, 50, true, this);
//new ������һ���з���̹��
Tank enemyTank = new Tank(100, 100, false, this);
List<Missile> missiles = new ArrayList<Missile>();
List<Explode> explodes = new ArrayList<Explode>();
//����һ������ͼƬ
Image offScreenImage = null;
//paint�����������Ҫ�����ã�һ��Ҫ���ػ���ʱ�ᱻ�Զ�����
public void paint(Graphics g) {
//��ʾ��������װ�˶����ڵ�
g.drawString("missiles count: " + missiles.size(), 10, 50);
//��ʾ�ж��ٸ���ը
g.drawString("explodes count: " + explodes.size(), 10, 70);
//�������е��ڵ����������
for(int i = 0; i < missiles.size(); i++) {
Missile missile = missiles.get(i);
missile.hitTank(enemyTank);
missile.draw(g);
}
//�������еı�ը���������
for(int i = 0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
myTank.draw(g);
enemyTank.draw(g);
}
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
//�õ����ͼƬ�Ļ���
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
print(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
//���ƴ���
public void launchFrame() {
this.setLocation(300, 50);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
addWindowListener(new WindowAdapter() {//�رմ���
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
this.setBackground(Color.GREEN);
this.addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.launchFrame();
}
private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
//��Ӽ��̼�����
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/oosad-soft-work-21011-21012/gao-shengjie-1213-tank-battle.git
git@gitee.com:oosad-soft-work-21011-21012/gao-shengjie-1213-tank-battle.git
oosad-soft-work-21011-21012
gao-shengjie-1213-tank-battle
高圣杰1213坦克大战
master

搜索帮助