From 9ab12006b70935f74ff981465b64a33c8c70eaf8 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Wed, 5 Oct 2022 19:50:17 +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=95=8C=E4=BA=BA=E5=9D=A6=E5=85=8B=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E5=8A=A8-=E8=BF=9B=E8=A1=8C2=E6=AC=A1=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/Bullet.java | 3 +++ src/com/msb/Tank.java | 28 +++++++++++++++++++++++++++- src/com/msb/TankDemo.java | 3 ++- src/com/msb/TankFrame.java | 1 + 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/com/msb/Bullet.java b/src/com/msb/Bullet.java index f1460ec..314d003 100644 --- a/src/com/msb/Bullet.java +++ b/src/com/msb/Bullet.java @@ -18,6 +18,7 @@ public class Bullet { private boolean live = true; private GroupEnum group = GroupEnum.BAD; private TankFrame tankFrame; +// private Rectangle rectangle; public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { this.x = x; @@ -25,6 +26,7 @@ public class Bullet { this.dir = dir; this.group = group; this.tankFrame = tankFrame; +// this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT); } public void paint(Graphics g) { @@ -80,6 +82,7 @@ public class Bullet { 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)) { +// if(this.rectangle.intersects(tank.getRectangle())) { this.die(); tank.die(); } diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index 269bffa..4084611 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -22,6 +22,7 @@ public class Tank { private boolean live = true; private Random random = new Random(); private GroupEnum group = GroupEnum.BAD; +// private Rectangle rectangle; public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { this.x = x; @@ -29,6 +30,7 @@ public class Tank { this.dir = dir; this.group = group; this.tankFrame = tankFrame; +// this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT); } public void paint(Graphics g) { @@ -52,6 +54,10 @@ public class Tank { this.dir = dir; } + public DirEnum getDir() { + return dir; + } + public void moving() { if( ! move) return; @@ -64,8 +70,17 @@ public class Tank { default: break; } - if(random.nextInt(10) > 8) this.fire(); +// this.boundsCheck(); + + //敌方坦克随机发射炮弹 + if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire(); + //敌方坦克随机改变方向进行 + if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.randomDir(); + + } + public void randomDir() { + this.dir = DirEnum.values()[random.nextInt(4)]; } public void setMove(boolean move) { @@ -79,6 +94,12 @@ public class Tank { tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame)); } + public void boundsCheck() { + if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) { + this.setDir(DirEnum.valueOf(random.nextInt(30)%4)); + } + } + public int getX() { return x; } @@ -95,4 +116,9 @@ public class Tank { public GroupEnum getGroup() { return group; } + + /*public Rectangle getRectangle() { + return rectangle; + }*/ + } diff --git a/src/com/msb/TankDemo.java b/src/com/msb/TankDemo.java index 4bbdb11..4a6b454 100644 --- a/src/com/msb/TankDemo.java +++ b/src/com/msb/TankDemo.java @@ -16,7 +16,8 @@ public class TankDemo { //创建5个敌方坦克 for (int i=0; i<5; i++) { - tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(3)), GroupEnum.BAD, tankFrame)); +// tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(30)%4), GroupEnum.BAD, 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 2edf76d..630cf7e 100644 --- a/src/com/msb/TankFrame.java +++ b/src/com/msb/TankFrame.java @@ -53,6 +53,7 @@ public class TankFrame extends Frame { g.setColor(Color.WHITE); g.drawString("子弹的数量:" + bullets.size(), 10, 60); g.drawString("敌方坦克数量:" + tanks.size(), 10, 90); + g.drawString("爆炸的数量:" + explodes.size(), 10, 120); g.setColor(color); myTank.paint(g);