坦克大战(一期)-干掉敌方坦克

Network
bingor_yhj 2 years ago
parent 72573b4c18
commit dbee942b01

@ -60,4 +60,26 @@ public class Bullet {
if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) this.live = false; if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) this.live = false;
} }
public int getX() {
return x;
}
public int getY() {
return y;
}
//碰撞
public void collide(Tank tank) {
Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if(bullteRec.intersects(tankRec)) {
this.die();
tank.die();
}
}
private void die() {
this.live = false;
}
} }

@ -18,6 +18,7 @@ public class Tank {
private TankFrame tankFrame; private TankFrame tankFrame;
public static final int WIDTH = ResourcesMgr.tankD.getWidth(); public static final int WIDTH = ResourcesMgr.tankD.getWidth();
public static final int HEIGHT = ResourcesMgr.tankD.getHeight(); public static final int HEIGHT = ResourcesMgr.tankD.getHeight();
private boolean live = true;
public Tank(int x, int y, DirEnum dir, TankFrame tankFrame) { public Tank(int x, int y, DirEnum dir, TankFrame tankFrame) {
this.x = x; this.x = x;
@ -27,6 +28,12 @@ public class Tank {
} }
public void paint(Graphics g) { public void paint(Graphics g) {
if( ! live) {
tankFrame.tanks.remove(this);
return;
}
/* Color color = g.getColor(); /* Color color = g.getColor();
g.setColor(Color.YELLOW); g.setColor(Color.YELLOW);
g.fillRect(x, y, 50, 50); g.fillRect(x, y, 50, 50);
@ -64,8 +71,21 @@ public class Tank {
} }
public void fire() { public void fire() {
//子弹在坦克每个方向上的中心点应该是不一样的. TODO
int bX = this.x + WIDTH/2 - Bullet.WIDTH/2; int bX = this.x + WIDTH/2 - Bullet.WIDTH/2;
int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2; int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, tankFrame)); tankFrame.bullets.add(new Bullet(bX, bY, this.dir, tankFrame));
} }
public int getX() {
return x;
}
public int getY() {
return y;
}
public void die() {
this.live = false;
}
} }

@ -50,17 +50,25 @@ public class TankFrame extends Frame {
Color color = g.getColor(); Color color = g.getColor();
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60); g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.setColor(color); g.setColor(color);
tank.paint(g); tank.paint(g);
//画出敌方坦克 //画出敌方坦克
for (Tank tank : tanks) { for (int i=0; i<tanks.size(); i++) {
tank.paint(g); tanks.get(i).paint(g);
} }
for (int i=0; i<bullets.size(); i++) { for (int i=0; i<bullets.size(); i++) {
bullets.get(i).paint(g); bullets.get(i).paint(g);
} }
for (int i=0; i<bullets.size(); i++) {
for (int j=0; j<tanks.size(); j++) {
bullets.get(i).collide(tanks.get(j));
}
}
} }
//双缓冲,解决闪烁现象 //双缓冲,解决闪烁现象

Loading…
Cancel
Save