From 87698795ef86143797faf3945ceec4af01e48a46 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Fri, 30 Sep 2022 16:48:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98(=E4=B8=80?= =?UTF-8?q?=E6=9C=9F)-=E6=8C=89=E7=A9=BA=E6=A0=BC=E5=8F=91=E5=B0=84?= =?UTF-8?q?=E4=B8=80=E9=A2=97=E5=AD=90=E5=BC=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/Tank.java | 9 ++++++++- src/com/msb/TankFrame.java | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) 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();