java怎么连发子弹_HTML-坦克大战-完成子弹连发功能(三)

本文介绍如何在HTML坦克大战游戏中实现子弹连发功能。通过创建坦克和子弹类,设置移动和发射子弹的方法,利用定时器控制子弹运动,并在页面上绘制子弹路径。详细讲解了各个函数的作用和代码逻辑。
摘要由CSDN通过智能技术生成

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值