diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index fe01a06..2cb4723 100644 Binary files a/docs/坦克大战笔记.docx and b/docs/坦克大战笔记.docx differ diff --git a/src/com/demo/tank/course4/Bullet.java b/src/com/demo/tank/course4/Bullet.java index 392ced0..cda1cdc 100644 --- a/src/com/demo/tank/course4/Bullet.java +++ b/src/com/demo/tank/course4/Bullet.java @@ -11,6 +11,7 @@ public class Bullet { private boolean live = true; private Group group = Group.BAD; private TankFrameV4 tf; + Rectangle rect = new Rectangle(); public Bullet(int x, int y, Direction direction, Group group, TankFrameV4 tf) { this.x = x; @@ -18,6 +19,11 @@ public class Bullet { this.direction = direction; this.group = group; this.tf = tf; + + rect.x = this.x; + rect.y = this.y; + rect.width = Bullet.WIDTH; + rect.height = Bullet.HEIGHT; } public void paint(Graphics g){ @@ -57,15 +63,15 @@ public class Bullet { if(x < 0 || y < 0 || x > TankFrameV4.GAME_WIDTH || y > TankFrameV4.GAME_HEIGHT){ live = false; } + rect.x = this.x; + rect.y = this.y; } //检测是否跟坦克碰撞 public void collideWith(Tank tank){ //关闭队友伤害 if(this.group == tank.getGroup()) return; - Rectangle bulletRect = new Rectangle(x, y, WIDTH, HEIGHT); - Rectangle tankRect = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT); - if(bulletRect.intersects(tankRect)){ + if(rect.intersects(tank.rect)){ tank.die(); this.die(); //爆炸 diff --git a/src/com/demo/tank/course4/Tank.java b/src/com/demo/tank/course4/Tank.java index 51c840f..a845e23 100644 --- a/src/com/demo/tank/course4/Tank.java +++ b/src/com/demo/tank/course4/Tank.java @@ -14,6 +14,7 @@ public class Tank { public static final int WIDTH = ResourceManager.tankD.getWidth(); public static final int HEIGHT = ResourceManager.tankD.getHeight(); private Random random = new Random(); + Rectangle rect = new Rectangle(); public Tank(int x, int y, Direction dir, Group group, TankFrameV4 tankFrame) { this.x = x; @@ -21,6 +22,11 @@ public class Tank { this.dir = dir; this.group = group; this.tankFrame = tankFrame; + + rect.x = this.x; + rect.y = this.y; + rect.width = Tank.WIDTH; + rect.height = Tank.HEIGHT; } public void paint(Graphics g) { @@ -69,6 +75,9 @@ public class Tank { //边界检测 boundsCheck(); + + rect.x = this.x; + rect.y = this.y; } private void boundsCheck() {