var tankeColor=new Array("#BA9658","#FEF26E");var enemyColor=new Array("#00A2B5","#00FEFE");//坦克的父类
functionTanK(x,y,direct,color){this.x=x;this.y=y;this.direct=direct;this.speed=5;this.color=color;this.moveUp=function(){this.y-=this.speed;this.direct=0;
}this.moveDown=function(){this.y+=this.speed;this.direct=2;
}this.moveRight=function(){this.x+=this.speed;this.direct=1;
}this.moveLeft=function(){this.x-=this.speed;this.direct=3;
}
}//子弹类
functionBullet(x,y,direct,speed){this.x=x;this.y=y;this.direct=direct;this.speed=speed;this.timer=null;this.isLive=true;this.run=functionrun(){if(this.x<=0||this.x>=500||this.y<=0||this.y>=500){
window.clearInterval(this.timer);this.isLive=false;
}else{switch(this.direct){case 0:this.y-=this.speed;break;case 1:this.x+=this.speed;break;case 2:this.y+=this.speed;break;case 3:this.x-=this.speed;break;
}
}
document.getElementById("aa").innerHTML="子弹的x="+this.x+"y="+this.y;
}
}//定义一个hero类
//x、y表示初始坐标,direct表示方向
functionHero(x,y,direct,color){this.tanke=TanK;this.tanke(x,y,direct,color);this.shotEnemy=function(){switch(this.direct){case 0://上
heroBullet=new Bullet(this.x+9,this.y,this.direct,1);break;case 1://右
heroBullet=new Bullet(this.x+36,this.y+9,this.direct,1);break;case 2://下
heroBullet=new Bullet(this.x+9,this.y+36,this.direct,1);break;case 3://左
heroBullet=new Bullet(this.x-6,this.y+9,this.direct,1);break;
}//将子弹放入到子弹数组中
heroBullets.push(heroBullet);//每隔50毫秒运行每个子弹的run方法
var timer=window.setInterval("heroBullets["+(heroBullets.length-1)+"].run()",50);
heroBullets[heroBullets.length-1].timer=timer;
}
}//定义一个hero类
//x、y表示初始坐标,direct表示方向
functionEnemyTanke(x,y,direct,color){this.tanke=TanK;this.tanke(x,y,direct,color);
}var heroBullet=null;//定义子弹变量
//画自己的子弹
functiondrawHeroBullet(){for(var i=0;i
heroBullet=heroBullets[i];if(heroBullet!=null&&heroBullet.isLive){
cxt.fillStyle="#FEF26E";
cxt.fillRect(heroBullet.x,heroBullet.y,2,2);
}
}
}//把创建坦克的方法封装为一个对象
//该函数可以画自己的坦克,也可以画敌人的坦克
//tanke就是一个对象
functiondrawTanke(tanke){//坦克的方向
switch(tanke.direct){case 0:case 2:
{//上
//画出自己的坦克设置颜色
cxt.fillStyle=tanke.color[0];
cxt.fillRect(tanke.x,tanke.y,5,30);//左齿轮
cxt.fillRect(tanke.x+15,tanke.y,5,30);//右齿轮
cxt.fillRect(tanke.x+6,tanke.y+5,8,20);//中间的坦克体
//画中间的圆形的炮筒体
cxt.fillStyle=tanke.color[1];
cxt.beginPath();
cxt.arc(tanke.x+10,tanke.y+15,4.5,0,360,false);
cxt.closePath();
cxt.fill();//画出炮筒
cxt.strokeStyle=tanke.color[1];//cxt.fillStyle="#FFD972";
cxt.lineWidth=1.9;
cxt.beginPath();
cxt.moveTo(tanke.x+10,tanke.y+15);//设置点的位置
if(tanke.direct==0){
cxt.lineTo(tanke.x+10,tanke.y-6);//设置第二个点的位置
}else if(tanke.direct==2){
cxt.lineTo(tanke.x+10,tanke.y+36);//设置第二个点的位置
}
cxt.closePath();
cxt.stroke();
}break;case 1:case 3:
{//右
//画出自己的坦克设置颜色
cxt.fillStyle=tanke.color[0];
cxt.fillRect(tanke.x,tanke.y,30,5);//左齿轮
cxt.fillRect(tanke.x,tanke.y+15,30,5);//右齿轮
cxt.fillRect(tanke.x+5,tanke.y+6,20,8);//中间的坦克体
//画中间的圆形的炮筒体
cxt.fillStyle=tanke.color[1];
cxt.beginPath();
cxt.arc(tanke.x+15,tanke.y+10,4.5,0,360,false);
cxt.closePath();
cxt.fill();//画出炮筒
cxt.strokeStyle=tanke.color[1];//cxt.fillStyle="#FFD972";
cxt.lineWidth=1.9;
cxt.beginPath();
cxt.moveTo(tanke.x+15,tanke.y+10);//设置点的位置
if(tanke.direct==1){//右
cxt.lineTo(tanke.x+36,tanke.y+10);//设置第二个点的位置
}else if(tanke.direct==3){//左
cxt.lineTo(tanke.x-6,tanke.y+10);//设置第二个点的位置
}
cxt.closePath();
cxt.stroke();
}break;
}
}