您的位置:首页 > Web前端 > JavaScript

JS贪吃蛇开发笔记2

2015-11-12 22:58 609 查看
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));

}

--------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: