js(知识点之两个游戏,贪吃蛇和飞机大战)

 贪吃蛇:
判断键盘的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;
                }
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值