本文主要包含HTML5,移动开发,坦克大战游戏等相关知识,匿名希望在学习及工作中可以帮助到您
上一篇我们创建了敌人的坦克和自己的坦克,接下来就应该让坦克发子弹了,我们下面来看一下如何让我们的坦克发出子弹。
前面我们用面向对象的思想对Tank进行了封装,又利用对象冒充实现了我们的坦克和敌人的坦克,仿照这种方式我们是不是也应该封装一个Bullet,答案是肯定的。好吧,那么我们再想想这个Bullet"类“都应该封装什么东西呢?位置应该有吧、子弹飞行的方向应该有吧、飞行的速度也应该有吧、自己飞出去的动作应该有吧。好啦,大概就这些,封装后的Bulle”t类“如下:
//子弹类 function Bullet(x,y,direct,speed){ this.x=x; this.y=y; this.speed=speed; this.direct=direct; this.timer=null; this.run=function(){ 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; } } }
我们已经创建好子弹的模型了,下面就用我们的坦克创造子弹将子弹发出去,在Hero类中添加shotEnemy方法。
//定义一个Hero类 function Hero(x,y,direct,color){ //下面两句话的作用是通过对象冒充达到继承的效果 this.tank=Tank; this.tank(x,y,direct,color); //射击敌人函数 this.shotEnemy=function(){ switch(this.direct){ case 0: heroBullet=new Bullet(this.x+10,this.y,this.direct,1); break; case 1: heroBullet=new Bullet(this.x+30-4,this.y+10+4,this.direct,1); break; case 2: heroBullet=new Bullet(this.x+10,this.y+30,this.direct,1); break; case 3: heroBullet=new Bullet(this.x-4,this.y+10+4,this.direct,1); break; } //把这个子弹放入数组中——》push函数 //调用我们子弹的run //var timer=window.setInterval("heroBullet.run()",50); //heroBullet.timer=timer; heroBullets.push(heroBullet); var timer=window.setInterval("heroBullets["+(heroBullets.length-1)+"].run()",50); heroBullets[heroBullets.length-1].timer=timer; } }
再在按键监听函数中添加一个监听发射子弹的键“J"
case 74: //J :发子弹 hero.shotEnemy(); break;
好了我们来试着发射一下子弹吧!子弹怎么只能发一颗,而且越发越快,这是怎么回事呢?再看看我们上面写的代码,原来我们的子弹一旦发出去就不能停止了,虽然跑出了我们的”战场“但是还是在朝一个方向跑,一旦发第二颗子弹第一颗子弹就会由于重新刷新界面没有重绘子弹而消失。好了既然知道原因了下面我们判断一下子弹是否出界,然后给子弹一个状态isLive(这个状态标记子弹是否存在,如果不存在则在重绘子弹的时候不再重绘),修改后的代码如下:
//子弹类 unction Bullet(x,y,direct,speed){ this.x=x; this.y=y; this.speed=speed; this.direct=direct; this.timer=null; this.isLive=true; this.run=function(){ //判断子弹是否已经到边界了 if(this.x<=0||this.x>=400||this.y<=0||this.y>=300){ //子弹要停止 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: break; } } }
如果子弹超出了canvas的范围则将isLive属性该为false
然后我们在前面的刷新界面函数中添加一个刷新子弹函数
//定时刷新我们的作战区(定时重绘) //自己的坦克,敌人坦克,子弹,炸弹,障碍物 function flashTankMap(){ //把画布清理 cxt.clearRect(0,0,400,300); //我的坦克 drawTank(hero); //我的子弹 drawHeroBullet(); //敌人的坦克 for(var i=0;i<3;i++){ drawTank(enemyTanks[i]); } }
//画出自己的子弹 function drawHeroBullet(){ for(var i=0;i<heroBullets.length;i++){ var heroBullet=heroBullets[i]; if(heroBullet!=null&&heroBullet.isLive){ cxt.fillStyle="#FEF26E"; cxt.fillRect(heroBullet.x,heroBullet.y,2,2); } } }
可以看到上面的drawHeroBullet中判断了子弹的isLive属性。
看看运行结果吧
全部源代码如下:
tankGame06.js
//为了编程方便,我们定义两个颜色数组 var heroColor=new Array("#BA9658","#FEF26E"); var enemyColor=new Array("#00A2B5","#00FEFE"); //子弹类 function Bullet(x,y,direct,speed){ this.x=x; this.y=y; this.speed=speed; this.direct=direct; this.timer=null; this.isLive=true; this.run=function(){ //判断子弹是否已经到边界了 if(this.x<=0||this.x>=400||this.y<=0||this.y>=300){ //子弹要停止 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; } } } } //定义一个Tank类(基类) function Tank(x,y,direct,color){ this.x=x; this.y=y; this.speed=1; this.direct=direct; this.color=color; //上移 this.moveUp=function(){ this.y-=this.speed; this.direct=0; } //右移 this.moveRight=function(){ this.x+=this.speed; this.direct=1; } //下移 this.moveDown=function(){ this.y+=this.speed; this.direct=2; } //左移 this.moveLeft=function(){ this.x-=this.speed; this.direct=3; } } //定义一个Hero类 function Hero(x,y,direct,color){ //下面两句话的作用是通过对象冒充达到继承的效果 this.tank=Tank; this.tank(x,y,direct,color); //设计敌人函数 this.shotEnemy=function(){ switch(this.direct){ case 0: heroBullet=new Bullet(this.x+10,this.y,this.direct,1); break; case 1: heroBullet=new Bullet(this.x+30-4,this.y+10+4,this.direct,1); break; case 2: heroBullet=new Bullet(this.x+10,this.y+30,this.direct,1); break; case 3: heroBullet=new Bullet(this.x-4,this.y+10+4,this.direct,1); break; } //把这个子弹放入数组中——》push函数 //调用我们子弹的run //var timer=window.setInterval("heroBullet.run()",50); //heroBullet.timer=timer; heroBullets.push(heroBullet); var timer=window.setInterval("heroBullets["+(heroBullets.length-1)+"].run()",50); heroBullets[heroBullets.length-1].timer=timer;