因为是单机版,所以敌人的坦克是可以随机方向移动的。如何实现,请看代码:
if(this.group == Group.BAD) {//敌人的坦克才随机发射子弹
if (random.nextInt(100) > 92) this.fire();
//敌人的坦克随机方向移动 方向变化太频繁 需要优化 改为只有百分之十的几率变方向
if (random.nextInt(100) > 90) randomDir();
}
private void randomDir(){
//Dir.values()获取方向的数组,我们只要随机一个数传递给这个数组,就是可以获取随机的方向。
this.dir= Dir.values()[random.nextInt(4)];
}
爆炸的位置不对,应该在敌人坦克的中心位置爆炸,调整代码:
public void collidewith(Tank tank) {
if(this.group == tank.getGroup()) return;//如果当前的子弹跟坦克是一个组的就不碰撞检测。
Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if (rect1.intersects(rect2)) {
tank.die();
this.die();
int ex = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2;
int ey = tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2;
tankFrame.explodes.add(new Explode(ex,ey,tankFrame));
}
}
07-31
5178
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)