任务
代码
为 Tank() 类构造函数添加方向数学,(自己的坦克可以控制,坏坦克只有构造方向了
为坏坦克方向随机生成一个数,用来确定移动方向
private static Random r = new Random();
private int steps = r.nextInt(15) + 3;
写入 move() 方法,并控制敌方坦克开火频率
if(!good) {
Direction[] dirs = Direction.values();
if(steps == 0) {
steps = r.nextInt(12) + 3;
int rn = r.nextInt(dirs.length);
ptDir = dir = dirs[rn];
}
steps--;
if(dir != Direction.STOP && r.nextInt(40) > 38)
this.fire();
}
炮弹不打自己人,在 hitTank() 方法中增加敌我好坏和炮弹是否还活着判断
另外在 fire() 方法,如果 live 已经为 false ,直接 return
最后,在 paint() 方法遍历子弹时,添加打自己坦克的方法
m.hitTank(myTank);