From 4a687543fa57a66c3dae08c528c05943e50946ca Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Tue, 4 Oct 2022 17:57:01 +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)-=E8=AE=A9=E6=95=8C=E6=96=B9=E4=B9=9F=E6=89=93?= =?UTF-8?q?=E5=87=BA=E5=AD=90=E5=BC=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/Bullet.java | 10 ++++++++-- src/com/msb/GroupEnum.java | 11 +++++++++++ src/com/msb/Tank.java | 18 ++++++++++++++---- src/com/msb/TankDemo.java | 2 +- src/com/msb/TankFrame.java | 18 +++++++++--------- 5 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 src/com/msb/GroupEnum.java diff --git a/src/com/msb/Bullet.java b/src/com/msb/Bullet.java index 6d3da50..f1460ec 100644 --- a/src/com/msb/Bullet.java +++ b/src/com/msb/Bullet.java @@ -14,14 +14,16 @@ public class Bullet { private DirEnum dir; public static final int WIDTH = ResourcesMgr.bulletD.getWidth(); public static final int HEIGHT = ResourcesMgr.bulletD.getHeight(); - public static final int SPEED = 5; + public static final int SPEED = 8; private boolean live = true; + private GroupEnum group = GroupEnum.BAD; private TankFrame tankFrame; - public Bullet(int x, int y, DirEnum dir, TankFrame tankFrame) { + public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { this.x = x; this.y = y; this.dir = dir; + this.group = group; this.tankFrame = tankFrame; } @@ -71,6 +73,10 @@ public class Bullet { //碰撞 public void collide(Tank tank) { + + if(this.group == tank.getGroup()) return; + + //TODO 每次碰撞的时候都小new 一个 Rectangle,应该用一个Rectangle来记录子弹的位置,产生内存占用,垃圾回收器会时不时运行一下 Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT); Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT); if(bullteRec.intersects(tankRec)) { diff --git a/src/com/msb/GroupEnum.java b/src/com/msb/GroupEnum.java new file mode 100644 index 0000000..a6e088c --- /dev/null +++ b/src/com/msb/GroupEnum.java @@ -0,0 +1,11 @@ +package com.msb; + +/** + * @Author bingor + * @Date 2022-10-04 17:12 + * @Description: com.msb + * @Version: 1.0 + */ +public enum GroupEnum { + GOOD, BAD +} diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index c505c2d..9599d05 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -1,6 +1,7 @@ package com.msb; import java.awt.*; +import java.util.Random; /** * @Author bingor @@ -12,18 +13,21 @@ public class Tank { private int x,y; private DirEnum dir; - private static final int SPEED = 10; - private boolean move = false; + private static final int SPEED = 5; + private boolean move = true; //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 private TankFrame tankFrame; public static final int WIDTH = ResourcesMgr.tankD.getWidth(); public static final int HEIGHT = ResourcesMgr.tankD.getHeight(); private boolean live = true; + private Random random = new Random(); + private GroupEnum group = GroupEnum.BAD; - public Tank(int x, int y, DirEnum dir, TankFrame tankFrame) { + public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { this.x = x; this.y = y; this.dir = dir; + this.group = group; this.tankFrame = tankFrame; } @@ -64,6 +68,8 @@ public class Tank { default: break; } + if(random.nextInt(10) > 8) this.fire(); + } public void setMove(boolean move) { @@ -74,7 +80,7 @@ public class Tank { //子弹在坦克每个方向上的中心点应该是不一样的. TODO int bX = this.x + WIDTH/2 - Bullet.WIDTH/2; int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2; - tankFrame.bullets.add(new Bullet(bX, bY, this.dir, tankFrame)); + tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame)); } public int getX() { @@ -88,4 +94,8 @@ public class Tank { public void die() { this.live = false; } + + public GroupEnum getGroup() { + return group; + } } diff --git a/src/com/msb/TankDemo.java b/src/com/msb/TankDemo.java index cbeb8d7..85f5672 100644 --- a/src/com/msb/TankDemo.java +++ b/src/com/msb/TankDemo.java @@ -13,7 +13,7 @@ public class TankDemo { //创建5个敌方坦克 for (int i=0; i<5; i++) { - tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, tankFrame)); + tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame)); } //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 diff --git a/src/com/msb/TankFrame.java b/src/com/msb/TankFrame.java index 00c0dc1..7d91473 100644 --- a/src/com/msb/TankFrame.java +++ b/src/com/msb/TankFrame.java @@ -17,7 +17,7 @@ import java.util.Objects; */ public class TankFrame extends Frame { - Tank tank = new Tank(100, 500, DirEnum.RIGHT, this); + Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this); // Bullet bullet = new Bullet(200, 200, DirEnum.DOWN); List bullets = new ArrayList<>(); List tanks = new ArrayList<>(); //敌方坦克 @@ -52,7 +52,7 @@ public class TankFrame extends Frame { g.drawString("子弹的数量:" + bullets.size(), 10, 60); g.drawString("敌方坦克数量:" + tanks.size(), 10, 90); g.setColor(color); - tank.paint(g); + myTank.paint(g); //画出敌方坦克 for (int i=0; i