碰撞检测,敌方坦克被击中后消失

master
kn5886348135 3 years ago
parent 41e9c91ac8
commit 2f49ccba39

@ -1,6 +1,6 @@
package com.example.tankbattle; package com.example.tankbattle;
import java.awt.Graphics; import java.awt.*;
public class Bullet { public class Bullet {
private static final int SPEED = 10; private static final int SPEED = 10;
@ -9,7 +9,7 @@ public class Bullet {
private int x, y; private int x, y;
private Dir dir; private Dir dir;
private boolean live = true; private boolean living = true;
private TankFrame tf; private TankFrame tf;
@ -21,7 +21,7 @@ public class Bullet {
} }
public void paint(Graphics g) { public void paint(Graphics g) {
if (!live) { if (!living) {
tf.bullets.remove(this); tf.bullets.remove(this);
} }
switch (dir) { switch (dir) {
@ -45,6 +45,7 @@ public class Bullet {
} }
private void move() { private void move() {
if (!living) return;
switch (dir) { switch (dir) {
case LEFT: case LEFT:
x -= SPEED; x -= SPEED;
@ -63,6 +64,19 @@ public class Bullet {
break; break;
} }
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) live = false; if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
}
public void collideWith(Tank tank){
Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if (rect1.intersects(rect2)) {
tank.die();
this.die();
}
}
private void die() {
this.living = false;
} }
} }

@ -4,6 +4,23 @@ import java.awt.Graphics;
public class Tank { public class Tank {
private int x,y; private int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private Dir dir = Dir.DOWN; private Dir dir = Dir.DOWN;
private static final int SPEED = 5; private static final int SPEED = 5;
@ -14,6 +31,8 @@ public class Tank {
private TankFrame tf = null; private TankFrame tf = null;
private boolean living = true;
public boolean isMoving() { public boolean isMoving() {
return moving; return moving;
} }
@ -41,6 +60,7 @@ public class Tank {
} }
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) tf.tanks.remove(this);
switch (dir) { switch (dir) {
case LEFT: case LEFT:
g.drawImage(ResourceMgr.tankL, x, y, null); g.drawImage(ResourceMgr.tankL, x, y, null);
@ -85,4 +105,8 @@ public class Tank {
int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2; int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tf.bullets.add(new Bullet(bX, bY, this.dir, this.tf)); tf.bullets.add(new Bullet(bX, bY, this.dir, this.tf));
} }
public void die() {
this.living = false;
}
} }

@ -53,6 +53,7 @@ public class TankFrame extends Frame {
Color c = g.getColor(); Color c = 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, 60);
g.setColor(c); g.setColor(c);
myTank.paint(g); myTank.paint(g);
@ -63,6 +64,12 @@ public class TankFrame extends Frame {
for (int i = 0; i < tanks.size(); i++) { for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).paint(g); tanks.get(i).paint(g);
} }
for (int i = 0; i < bullets.size(); i++) {
for (int j = 0; j < tanks.size(); j++) {
bullets.get(i).collideWith(tanks.get(j));
}
}
// for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) { // for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) {
// Bullet b = it.next(); // Bullet b = it.next();
// if (!b.live) it.remove(); // if (!b.live) it.remove();

Loading…
Cancel
Save