上一篇我们创建了敌人的坦克和自己的坦克,接下来就应该让坦克发子弹了,我们下面来看一下如何让我们的坦克发出子弹。
前面我们用面向对象的思想对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;
}
}
//定义一个enemytank类
function enemytank(x,y,direct,color){
this.tank=tank;
this.tank(x,y,direct,color);
}
//绘制坦克
function drawtank(tank){
//考虑方向
switch(tank.direct){
case 0: //向上
case 2: //向下
//设置颜色
cxt.fillstyle=tank.color[0];
//左边的矩形
cxt.fillrect(tank.x,tank.y,5,30);
//右边的矩形
cxt.fillrect(tank.x+17,tank.y,5,30);
//画中间的矩形
cxt.fillrect(tank.x+6,tank.y+5,10,20);
//画出坦克的盖子
cxt.fillstyle=tank.color[1];
cxt.arc(tank.x+11,tank.y+15,5,0,math.pi*2,true);
cxt.fill();
//画出炮筒
cxt.strokestyle=tank.color[1];
cxt.linewidth=1.5;
cxt.beginpath();
cxt.moveto(tank.x+11,tank.y+15);
if(tank.direct==0){ //只是炮筒的方向不同
cxt.lineto(tank.x+11,tank.y);
}else{
cxt.lineto(tank.x+11,tank.y+30);
}
cxt.closepath();
cxt.stroke();
break;
case 1:
case 3:
//设置颜色
cxt.fillstyle="#ba9658";
//上边的矩形
cxt.fillrect(tank.x-4,tank.y+4,30,5);
//下边的矩形
cxt.fillrect(tank.x-4,tank.y+17+4,30,5);
//画中间的矩形
cxt.fillrect(tank.x+5-4,tank.y+6+4,20,10);
//画出坦克的盖子
cxt.fillstyle="#fef26e";
cxt.arc(tank.x+15-4,tank.y+11+4,5,0,math.pi*2,true);
cxt.fill();
//画出炮筒
cxt.strokestyle="#fef26e";
cxt.linewidth=1.5;
cxt.beginpath();
cxt.moveto(tank.x+15-4,tank.y+11+4);
if(tank.direct==1){ //只是炮筒的方向不同
cxt.lineto(tank.x+30-4,tank.y+11+4);
}else{
cxt.lineto(tank.x-4,tank.y+11+4);
}
cxt.closepath();
cxt.stroke();
break;
}
}
坦克大战.html
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body onkeydown="getcommand();">
<h1>html5-坦克大战</h1>
<!--坦克大战的战场-->
<canvas id="tankmap" width="400px" height="300px" style="background-color:black"></canvas>
<!--将tankgame04.js引入-->
<script type="text/javascript" src="tankgame06.js"></script>
<script type="text/javascript">
//得到画布
var canvas1=document.getelementbyid("tankmap");
//得到绘图上下文
var cxt=canvas1.getcontext("2d");
//我的tank
//规定0向上、1向右、2向下、3向左
var hero=new hero(40,40,0,herocolor);
//定义子弹数组
var herobullets=new array();
//敌人的tank
var enemytanks=new array();
for(var i=0;i<3;i++){
var enemytank = new enemytank((i+1)*50,0,2,enemycolor);
enemytanks[i]=enemytank;
}
//画出自己的子弹
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);
}
}
}
//定时刷新我们的作战区(定时重绘)
//自己的坦克,敌人坦克,子弹,炸弹,障碍物
function flashtankmap(){
//把画布清理
cxt.clearrect(0,0,400,300);
//我的坦克
drawtank(hero);
//我的子弹
drawherobullet();
//敌人的坦克
for(var i=0;i<3;i++){
drawtank(enemytanks[i]);
}
}
flashtankmap();
//接收用户按键的函数
function getcommand(){
var code = event.keycode; //键盘上字幕的ascii码
switch(code){
case 87: //w :上
hero.moveup();
break;
case 68: //d :右
hero.moveright();
break;
case 83: //s :下
hero.movedown();
break;
case 65: //a :左
hero.moveleft();
break;
case 74: //j :发子弹
hero.shotenemy();
break;
}
flashtankmap();
}
//每隔100毫秒去刷新一次作战区
window.setinterval("flashtankmap()",100);
</script>
</body>
</html>
以上就是 小强的html5移动开发之路(9)——坦克大战游戏3的内容。