本版本:让敌人的坦克更加智能(人工智能)
1.首先让它动起来:方向(move) 然后 随机数产生器
a.坦克中加方法:
public Tank(int x,int y,Boolean good,Direction dir,TankClient tc){
this(x,y,good);
this.dir = dir;
this.tc=tc;
}
修改主类中的代码:
Tank myTank = new Tank(50, 50,true,Tank.Direction.STOP,this);
修改敌方坦克代码:
for(int i=0;i<10;i++){
tanks.add(new Tank(50+40*(i+1),50,false,Tank.Direction.D,this));
}
b.随机让坦克运动
if(!good){
Direction[] dirs = Direction.values();
int rn = r.nextInt(dirs.length);<pre name="code" class="html">
dir = dirs[rn]; }
2.让敌军坦克发射炮弹:a.本军炮弹不打本军,添加bGood
<pre name="code" class="html">private boolean good; //子弹类
使用方法调用good
public boolean isGood() {
return good;
}
修改构造方法:
public Missile(int x, int y,boolean good,Tank.Direction dir,TankClient tc) {
this.x = x;
this.y = y;
this.good = good;
this.dir=dir;
this.tc= tc;
}
b.修改坦克fire方法
public Missile fire(){
if(!live) return null;
int x=this.x+Tank.WIDTH/2-Missile.HEIGHT/2;
int y=this.y-Missile.WIDTH/2+Tank.HEIGHT/2;
Missile m = new Missile(x, y,good,ptDir,this.tc);
tc.missiles.add(m);
return m;
}
c.修改hitTank方法 好不能打好的,坏不能打坏的
public boolean hitTank(Tank t){
if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good !=t.isGood()){
t.setLive(false);
this.live=false;
Explode e = new Explode(x, y, tc);
tc.explodes.add(e);
return true;
}
return false;
}
3.敌军坦克炮火不能太猛
if(r.nextInt(40)>38) this.fire();
if(r.nextInt(40)>38) this.fire();
总结:人工智能很复杂,在这里如果要做的更加智能,需要使敌方坦克的炮筒总是对准我方坦克 这时候需要角度的计算,比较复杂,有兴趣的可以尝试一下。
下一版本:添加墙,创建Will类