From 7fa73e7dddec452f88a85bbf6a4d9f986922de46 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Sat, 1 Oct 2022 09:41:28 +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)-=E5=AE=8C=E6=88=90=E5=AD=90=E5=BC=B9=E7=9A=84?= =?UTF-8?q?=E5=8F=AF=E6=8C=81=E7=BB=AD=E5=8F=91=E5=B0=84=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E5=AD=90=E5=BC=B9=E9=A3=9E=E5=87=BA=E8=BE=B9=E7=95=8C?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=B0=86=E5=AD=97=E6=AE=B5=E4=BB=8E?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E4=B8=AD=E7=A7=BB=E9=99=A4=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=87=BA=E7=8E=B0=E5=86=85=E5=AD=98=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/Bullet.java | 12 +++++++++++- src/com/msb/Tank.java | 2 +- src/com/msb/TankFrame.java | 13 +++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/com/msb/Bullet.java b/src/com/msb/Bullet.java index e3f4e2c..12083bf 100644 --- a/src/com/msb/Bullet.java +++ b/src/com/msb/Bullet.java @@ -15,14 +15,22 @@ public class Bullet extends Frame { public static final int WIDTH = 20; public static final int HEIGHT = 20; public static final int SPEED = 5; + private boolean live = true; + private TankFrame tankFrame; - public Bullet(int x, int y, DirEnum dir) { + public Bullet(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) { + if( ! this.live) { + tankFrame.bullets.remove(this); + return; + } + Color color = g.getColor(); g.setColor(Color.RED); g.fillOval(x, y, WIDTH, HEIGHT); @@ -40,5 +48,7 @@ public class Bullet extends Frame { default: break; } + if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) this.live = false; + } } diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index 64835ed..2df4bea 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -55,6 +55,6 @@ public class Tank { } public void fire() { - tankFrame.bullet = new Bullet(this.x, this.y, this.dir); + tankFrame.bullets.add(new Bullet(this.x, this.y, this.dir, tankFrame)); } } diff --git a/src/com/msb/TankFrame.java b/src/com/msb/TankFrame.java index 9cfc408..9163f89 100644 --- a/src/com/msb/TankFrame.java +++ b/src/com/msb/TankFrame.java @@ -5,6 +5,8 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; /** @@ -16,7 +18,8 @@ import java.util.Objects; public class TankFrame extends Frame { 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); + List bullets = new ArrayList<>(); public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; @@ -42,8 +45,14 @@ public class TankFrame extends Frame { @Override public void paint(Graphics g) { // super.paint(g); + Color color = g.getColor(); + g.setColor(Color.BLACK); + g.drawString("子弹的数量:" + bullets.size(), 10, 60); + g.setColor(color); tank.paint(g); - bullet.paint(g); + for (int i=0; i