1 Star 0 Fork 0

OOSAD-软工21011-21012/坦克大战 1211郭永森

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TankClient01.java 3.76 KB
一键复制 编辑 原始数据 按行查看 历史
郭永森 提交于 2023-05-05 12:13 . 森森
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class TankClient01 extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
int x = 50, y = 50;
Tank myTank = new Tank(50, 50, true, this);
Blood b = new Blood();
//Tank enemyTank = new Tank(100, 100, false, this);
List<Tank> tanks = new ArrayList<Tank>();
Wall w1 = new Wall(250,250,250,10,this);
// Wall w2 = new Wall(400,250,250,10,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);
g.drawString("tanks count: " + tanks.size(), 10, 90);
g.drawString("blood: " + myTank.getLife(), 10, 110);
for (int i = 0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
w1.draw(g);
myTank.draw(g);
b.draw(g);
myTank.eat(b);
myTank.collidesWithWall(w1);
myTank.collidesWithTanks(tanks);
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).draw(g);
tanks.get(i).collidesWithWall(w1);
tanks.get(i).collidesWithTanks(tanks);
}
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.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() {
for (int i = 0; i < 10; i++) {
tanks.add(new Tank(50 + 40 * (i + 1), 50, false, this));
tanks.get(i).collidesWithWall(w1);
// tanks.get(i).collidesWithWall(w2);
}
this.setLocation(300, 100);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
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) {
TankClient01 tc = new TankClient01();
tc.launchFrame();
}
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} 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/tank-battle-tan.git
git@gitee.com:oosad-soft-work-21011-21012/tank-battle-tan.git
oosad-soft-work-21011-21012
tank-battle-tan
坦克大战 1211郭永森
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385