坦克大战(一期)-按空格发射一颗子弹

Network
bingor_yhj 2 years ago
parent 29c1d47f42
commit 87698795ef

@ -14,11 +14,14 @@ public class Tank {
private DirEnum dir; private DirEnum dir;
private static final int SPEED = 10; private static final int SPEED = 10;
private boolean move = false; 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.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tankFrame = tankFrame;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -50,4 +53,8 @@ public class Tank {
public void setMove(boolean move) { public void setMove(boolean move) {
this.move = move; this.move = move;
} }
public void fire() {
tankFrame.bullet = new Bullet(this.x, this.y, this.dir);
}
} }

@ -15,7 +15,7 @@ import java.util.Objects;
*/ */
public class TankFrame extends Frame { 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); Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
public static final int GAME_WIDTH = 800; public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600; 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_RIGHT : bR = false; break;
case KeyEvent.VK_UP : bU = false; break; case KeyEvent.VK_UP : bU = false; break;
case KeyEvent.VK_DOWN : bD = false; break; case KeyEvent.VK_DOWN : bD = false; break;
case KeyEvent.VK_SPACE: tank.fire(); break;
default: break; default: break;
} }
setDir(); setDir();

Loading…
Cancel
Save