diff --git a/src/main/java/com/example/tankbattle/Bullet.java b/src/main/java/com/example/tankbattle/Bullet.java index cef8d0a..2137bbe 100644 --- a/src/main/java/com/example/tankbattle/Bullet.java +++ b/src/main/java/com/example/tankbattle/Bullet.java @@ -1,6 +1,6 @@ package com.example.tankbattle; -import java.awt.Graphics; +import java.awt.*; public class Bullet { private static final int SPEED = 10; @@ -9,7 +9,7 @@ public class Bullet { private int x, y; private Dir dir; - private boolean live = true; + private boolean living = true; private TankFrame tf; @@ -21,7 +21,7 @@ public class Bullet { } public void paint(Graphics g) { - if (!live) { + if (!living) { tf.bullets.remove(this); } switch (dir) { @@ -45,6 +45,7 @@ public class Bullet { } private void move() { + if (!living) return; switch (dir) { case LEFT: x -= SPEED; @@ -63,6 +64,19 @@ public class Bullet { 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; } } diff --git a/src/main/java/com/example/tankbattle/Tank.java b/src/main/java/com/example/tankbattle/Tank.java index 93ac2d5..063c534 100644 --- a/src/main/java/com/example/tankbattle/Tank.java +++ b/src/main/java/com/example/tankbattle/Tank.java @@ -4,6 +4,23 @@ import java.awt.Graphics; public class Tank { 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 static final int SPEED = 5; @@ -14,6 +31,8 @@ public class Tank { private TankFrame tf = null; + private boolean living = true; + public boolean isMoving() { return moving; } @@ -41,6 +60,7 @@ public class Tank { } public void paint(Graphics g) { + if (!living) tf.tanks.remove(this); switch (dir) { case LEFT: g.drawImage(ResourceMgr.tankL, x, y, null); @@ -85,4 +105,8 @@ public class Tank { int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2; tf.bullets.add(new Bullet(bX, bY, this.dir, this.tf)); } + + public void die() { + this.living = false; + } } diff --git a/src/main/java/com/example/tankbattle/TankFrame.java b/src/main/java/com/example/tankbattle/TankFrame.java index 92411bb..9b93301 100644 --- a/src/main/java/com/example/tankbattle/TankFrame.java +++ b/src/main/java/com/example/tankbattle/TankFrame.java @@ -53,6 +53,7 @@ public class TankFrame extends Frame { Color c = g.getColor(); g.setColor(Color.WHITE); g.drawString("子弹的数量:" + bullets.size(), 10, 60); + g.drawString("敌方坦克的数量:" + tanks.size(), 10, 60); g.setColor(c); myTank.paint(g); @@ -63,6 +64,12 @@ public class TankFrame extends Frame { for (int i = 0; i < tanks.size(); i++) { 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 it = bullets.iterator(); it.hasNext()) { // Bullet b = it.next(); // if (!b.live) it.remove();