java项目之——坦克大战19

本版本:让敌人的坦克更加智能(人工智能)

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类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值