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);