JS贪吃蛇开发笔记3

6.移动所有身体
if (SNAKE_BODY.length !=0){
var Snode = null;
for (var i = SNAKE_BODY.length - 1; i>=0; i--){   //为什么从后开始遍历
Snode = SNAKE_BODY[i];
if (i==0){
  switch (dir){
     case SNAKE_DIR.UP:
         Snode.now_row = this._head.now_row - 1;  //向上走得话蛇身在蛇头下一行
         Snode.now_col = this._head.now_col;
         break;
     case SNAKE_DIR.DOWN:
         Snode.now_row = this._head.now_row + 1;
         Snode.now_col = this._head.now_col;
         break;
     case SNAKE_DIR.LEFT:
         Snode.now_row = this._head.now_row;
         Snode.now_col = this._head.now_col + 1;  //向左走得话蛇身在蛇头右边一列,所以+1
         break;
     case SNAKE_DIR.RIGHT:
         Snode.now_row = this._head.now_row;
         Snode.now_col = this._head.now_col - 1;
         break;
       default: break;
    }
 }else{
     Snode.now_col = SNAKE_BODY[i-1].now_col;
     Snode.now_row = SNAKE_BODY[i-1].now_row;
 }
 Snode.setPosition(cc.p(Snode.now_col*63,Snode.now_row*63));
}
}
-----------------------------------------------------------------------------
7.死亡判定之蛇头碰蛇身以及触摸回调
//蛇头碰蛇身
if (SNAKE_BODY.length !=0){
for (var i = SNAKE_BODY.length - 1;i>=0; i--){
  if (this._head.now_col == SNAKE_BODY[i].now_col && this._head.now_row == SNAKE_BODY[i].now_row){
  this.onGameOver();
       }
}
}


//GameOver
  onGameOver:function(){
  director.pause();  //暂停游戏
  var gameOver = new cc.LabelTTF("Game Over","Arial",50);
  gameOver.setPosition(winSize.width/2,winSize.height/2);
  this.addChild(gameOver);
  }


//触摸回调
onTouchBegan:function(touch,event){
var target = event.getCurrentTarget();
var mx = Math.abs((touch.getLocation().x-5)-target._head.now_col*63);
var my = Math.abs((touch.getLocation().y-250)-target._head.now_row*63);
if (mx>my) {  //左右移动
 if((touch.getLocation().x-5)>target._head.now_col*63){   //触摸横坐标要减去背景层横坐标5
    dir = SNAKE_DIR.RIGHT;
 }else{
    dir = SNAKE_DIR.LEFT;
 }
}else{  //上下移动
 if((touch.getLocation().y-250)>target._head.now_row*63){  //触摸高度要减去背景层高度250
    dir = SNAKE_DIR.UP;
 }else{
    dir = SNAKE_DIR.DOWN;
     }
       }

}

---------------------------菜鸟写程序,还请大家多多指教---------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值