JS贪吃蛇开发笔记2

4.添加蛇头 食物  蛇身
//snakeHead
this._head = new SnakeGame(1);  //type = 1,蛇头
this._head.setScale(0.9);  //保证蛇头的大小和画出的方格大小基本一致
 //Math.round()为四舍五入,Math.random()产生0~1的随机数,乘以9之后产生0~9的随机数,表示row随机,下同
this._head.now_row = Math.round(Math.random()* 9); 
this._head.now_col = Math.round(Math.random()* 9);
this._head.setPosition(cc.p(this._head.now_col * 63,this._head.now_row * 63));
bgc.addChild(this._head,3);


//snakeFood
this._food = new SnakeGame(3);
this._food.setScale(0.9);
this._food.now_row = Math.round(Math.random()* 9);
this._food.now_col = Math.round(Math.random()* 9);
this._food.setPosition(cc.p(this._food.now_col * 63,this._head.now_row * 63));
bgc:addChild(this._food,3);


//计划任务
this.schedule(this.updateHead,0.5);
dir = SNAKE_DIR.RIGHT;  //初始化方向 向右


//清空数组
SNAKE_BODY=[];


//score
score = new cc.LabelTTF("分数:0","Arial",40);
score.setPosition(cc.p(winSize.width/2+180,winSize.height-200));
score.setColor(cc.color(123,123,123));
this.addChild(score,4);
-------------------------------------------------------------
5.主函数 主逻辑
updateHead:function(){
   //死亡判定之蛇头出界
   if (this._head.now_col<0 || this._head.now_col>9 || this._head.now_row<0 || this._head.now_row>9){
            that.onGameOver();  //自己定义一个GameOver的函数吧  这里就不写了
        }
switch (dir){   //dir的定义请往前面看
        case SNAKE_DIR.UP:
          this._head.now_row = this._head.now_row + 1;  //向上走,行增加
          break;
        case SNAKE_DIR.DOWN:
          this._head.now_row = this._head.now_row - 1;
          break;
        case SNAKE_DIR.LEFT:
          this._head.now_col = this._head.now_col - 1;
          break;
        case SNAKE_DIR.RIGHT:
          this._head.now_col = this._head.now_col + 1;
          break;
}
this._head.setPosition(cc.p(this._head.now_col * 63,this._head.now_row * 63));


//检测是否吃到了食物  蛇头和食物的行列相等
if (this._head.now_col == this.food.now_col && this._head.now_row == this._food.now_row){
   this.m_score += 90; //吃到一个食物加90分  随意定
   score.setString("分数:"+this.m_score);
 //update the position of Food  吃掉食物之后重置食物位置
 this._food.now_row = Math.round(Math.random()* 9);
 this._food.now_col = Math.round(Math.random()* 9);
 this._food.setPosition(cc.p(this._food.now_col * 63,this._head.now_row * 63));
    
    //同时增加一个蛇身
    this._snakeBody = SnakeGame.create(2);  //type==2 蛇身
    this._snakeBody.setScale(0.9);
    //增加蛇身依旧有两种情况,已经有身体和没有身体,没有身体就是蛇头和食物第一次撞击
    if (SNAKE_BODY.length == 0 || SNAKE_BODY.length == null){  //还没有身体
       switch (dir){
        case SNAKE_DIR.UP:
          this._snakeBody.now_row = this._head.now_row - 1;  //向上走,身体的行数比蛇头少一个
          this._snakeBody.now_col = this._head.now_col;
          break;
        case SNAKE_DIR.DOWN:
          this._snakeBody.now_row = this._head.now_row + 1;
          this._snakeBody.now_col = this._head.now_col;
          break;
        case SNAKE_DIR.LEFT:
          this._snakeBody.now_row = this._head.now_row;
          this._snakeBody.now_col = this._head.now_col + 1;
          break;
        case SNAKE_DIR.RIGHT:
          this._snakeBody.now_row = this._head.now_row;
          this._snakeBody.now_col = this._head.now_col - 1;
          break;
        defaule: break;
        }
      }else{   //已经有身体了
          switch (dir){
        case SNAKE_DIR.UP:
          this._snakeBody.now_row = this._snakeBody.now_row - 1;  //向上走,身体的行数比蛇头少一个
          this._snakeBody.now_col = this._snakeBody.now_col;
          break;
        case SNAKE_DIR.DOWN:
          this._snakeBody.now_row = this._snakeBody.now_row + 1;
          this._snakeBody.now_col = this._snakeBody.now_col;
          break;
        case SNAKE_DIR.LEFT:
          this._snakeBody.now_row = this._snakeBody.now_row;
          this._snakeBody.now_col = this._snakeBody.now_col + 1;
          break;
        case SNAKE_DIR.RIGHT:
          this._snakeBody.now_row = this._snakeBody.now_row;
          this._snakeBody.now_col = this._snakeBody.now_col - 1;
          break;
        defaule: break;
      }
   }
   //将蛇身添加到数组 再添加到背景层中
   SNAKE_BODY.push(this._snakeBody);
   this.getChildByTag(111).addChild(this._snakeBody,2);  //111为背景层的tag 上面去查
   this._snakeBody.setPosition(cc.p(this._snakeBody.now_col * 63,this._snakeBody.now_row * 63));
}
--------------------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值