贪吃蛇:
判断键盘的keyCode值(上下左右)
switch (e.keyCode){
case 37:if(snack.direct!="right") snack.direct="left";break;
case 38:if(snack.direct!="bottom") snack.direct="top";break;
case 39:if(snack.direct!="left") snack.direct="right";break;
case 40:if(snack.direct!="top") snack.direct="bottom";break;
}
让蛇从一头进,另一头出
//判断是否跑出黑色屏幕
this.snackYue=function(){
if(this._snackbody[0][0]>=40){
this._snackbody[0][0]=0;
}
if(this._snackbody[0][0]<0){
this._snackbody[0][0]=39;
}
if(this._snackbody[0][1]>=20){
this._snackbody[0][1]=0;
}
if(this._snackbody[0][1]<0){
this._snackbody[0][1]=19;
}
飞机大战
判断子弹是否与敌军相撞函数,在创建子弹的函数里添加如下函数:
this.shouterEmeny=function(emeny,index,arry){
for(var key in emeny){
if(this.y>=emeny[key].y&&this.y<=emeny[key].y+emeny[key].height&&this.x>emeny[key].x-this.width&&this.x<emeny[key].x+emeny[key].width){
emeny[key].blood-=1;
if(emeny[key].blood<=0){
Score+=emeny[key].score;
scoretxt.innerHTML="score:"+Score;
emeny[key]._enemy.remove();
emeny.splice(key,1);
}
this._shouter.remove();
arry.splice(index,1);
}
}
}
判断飞机与敌军相撞,并清除所有定时器,在飞机的函数里添加此循环
for(var key in s_enemy){
if(this.y>s_enemy[key].y-this.height+10&&this.y<s_enemy[key].y+s_enemy[key].height-10&&this.x>s_enemy[key].x-this.width+20&&this.x<s_enemy[key].x+s_enemy[key].width-20){
clearInterval(timeenemy);
clearInterval(timeenemymake);
clearInterval(timeplane);
clearInterval(timeshouter);
alert("Game Over!!!");
map.οnmοusemοve=null;
return;
}
}