diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index 13c59ea..64835ed 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -14,11 +14,14 @@ public class Tank { private DirEnum dir; private static final int SPEED = 10; private boolean move = false; + //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 + private TankFrame tankFrame; - public Tank(int x, int y, DirEnum dir) { + public Tank(int x, int y, DirEnum dir, TankFrame tankFrame) { this.x = x; this.y = y; this.dir = dir; + this.tankFrame = tankFrame; } public void paint(Graphics g) { @@ -50,4 +53,8 @@ public class Tank { public void setMove(boolean move) { this.move = move; } + + public void fire() { + tankFrame.bullet = new Bullet(this.x, this.y, this.dir); + } } diff --git a/src/com/msb/TankFrame.java b/src/com/msb/TankFrame.java index 45eb69b..9cfc408 100644 --- a/src/com/msb/TankFrame.java +++ b/src/com/msb/TankFrame.java @@ -15,7 +15,7 @@ import java.util.Objects; */ public class TankFrame extends Frame { - Tank tank = new Tank(200, 200, DirEnum.RIGHT); + Tank tank = new Tank(200, 200, DirEnum.RIGHT, this); Bullet bullet = new Bullet(200, 200, DirEnum.DOWN); public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; @@ -92,6 +92,7 @@ public class TankFrame extends Frame { case KeyEvent.VK_RIGHT : bR = false; break; case KeyEvent.VK_UP : bU = false; break; case KeyEvent.VK_DOWN : bD = false; break; + case KeyEvent.VK_SPACE: tank.fire(); break; default: break; } setDir();