这两天前辈要我用cocos2d-js做一个贪吃蛇,记录一下代码
joyStick.js
var joyStick = cc.Layer.extend(
{ctor:function()
{
this._super();
this.initGame();
//add 4 sprite
this.addChild(JoyStickBg);
this.addChild(joyStickPt);
this.addChild(helloLabel);
this.addChild(score_label);
GetDirection=function(a,b)
{
var pt = cc.p(cc.pSub(a,b));
if(Math.abs(pt.x)<0.000001&&Math.abs(pt.y)<0.000001) //judge if equal
return cc.p(0,0);
return cc.pNormalize(cc.pSub(a,b));
},
return true;
},
update:function(){
var newPos = cc.pAdd(snake_list[0][0].getPosition(),cc.pMult(snake_list[0][1],movespeed));
//judge if touch the wall
if(newPos.x<0)
{
this.SetGameState(2);
newPos.x = 0;
}
if(newPos.x>winSize.width)
{
this.SetGameState(2);
newPos.x= winSize.width;
}
if(newPos.y<0)
{
this.SetGameState(2);
newPos.y = 0;
}
if(newPos.y>winSize.height)
{
this.SetGameState(2);
newPos.y = winSize.height;
}
snake_list[0][0].setPosition(newPos); //set position of snake head
//set position and direction of child snake node
for (var i = snake_list.length - 1; i >= 1; i--) {
snake_list[i][1] = snake_list[i][2];
snake_list[i][2] = snake_list[i-1][1];
var dPos = cc.pAdd(snake_list[i-1][0].getPosition(),cc.pMult(snake_list[i-1][1],-movespeed));
//var dPos = cc.pAdd(snake_list[i][0].getPosition(),cc.pMult(snake_list[i][1],movespeed));
snake_list[i][0].setPosition(dPos);
}
//judge if eat food
if(cc.pDistance(newPos,cur_food.getPosition())<cur_food._contentSize.width)
{
this.AddNewFood();
this.AddSnakeNode(5);
}
//check collision
for (var i = snake_list.length - 1; i >= 2; i--) {
if(cc.pDistance(snake_list[i][0].getPosition(),newPos)<5)
{
this.SetGameState(2);
}
}
},
SetGameState:function(flag) //set game state flag=1 for win while flag =2 for fail
{
if(flag == 1)
{
cc.log("set win");
//set win
}
else
{
cc.log("set fail");
//remove 4 sprite for next use
this.removeChild(joyStickPt);
this.removeChild(JoyStickBg);
this.removeChild(helloLabel);
this.removeChild(score_label);
this.unscheduleUpdate();
var endGameScene = new endGame();
cc.director.runScene(endGameScene);
//set fail
}
},
AddNewFood :function() //add a new food on the map
{
if (cur_food==null)
{
cur_food = new cc.Sprite("res/pt.png");
this.addChild(cur_food);
}
var rd_spawn = cc.p(cc.random0To1()*(winSize.width-cur_food._contentSize.width)+cur_food._contentSize.width/2,cc.random0To1()*(winSize.height-cur_food._contentSize.width/2)+cur_food._contentSize.width/2);
cur_food.setColor(cc.color(255,0,255,0));
cur_food.setPosition(rd_spawn);
},
AddSnakeNode:function(sNum) //according to the giving sNum,add snake length and score
{
snake_length += sNum;
var spriteName = "";
database.score+=sNum;
score_label.string = database.score
if( snake_length>1)
{
spriteName = "res/body.png";
}
else
{
spriteName = "res/pt.png";
}
for (var i=0;i<sNum;i++)
{
var sLength = snake_list.length-1;
var newNode = [
sprite = new cc.Sprite(spriteName),
dir = snake_list[sLength][1],
nextdir = snake_list[sLength][1]
];
snake_list.push(newNode);
var sLength = snake_list.length-1;
this.addChild(snake_list[sLength][0]);
var newPos = cc.pAdd(snake_list[sLength-1][0].getPosition(),cc.pMult(snake_list[sLength-1][1],-snake_list[0][0]._contentSize.width-5));
snake_list[sLength][0].setPosition(newPos);
}
},
initGame:function()
{
this.scheduleUpdate();
bIsActived = false;
radius = 48;
snake_length = 1;
curDirection = cc.p(1,0);
game_state = 0;
movespeed = 5;
snake_list = [];
snake_head_pos = cc.p(200,200);
database.score = 0;
var snake_head = [
sprite = new cc.Sprite("res/pt.png"),
dir = cc.p(1,0),
nextdir = null
];
snake_list.push(snake_head);
cur_food =null;
score_label.string = database.score;
curPosition = cc.p(JoyStickBg.x,JoyStickBg.y);
centerPostion = cc.p(JoyStickBg.x,JoyStickBg.y);
this.addChild(snake_list[0][0]);
snake_list[0][0].setPosition(snake_head_pos);
joyStickPt.x = 58;
joyStickPt.y = 58;
cc.eventManager.addListener(listenr_touch,this);
this.AddNewFood();
this.AddSnakeNode(100);
}
});
bIsActived = false;
radius = 48;
snake_length = 1;
curDirection = cc.p(1,0);
game_state = 0;
movespeed = 5;
snake_list = [];
snake_head_pos = cc.p(200,200);
cur_food =null;
listenr_touch = cc.EventListener.create(
{
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:true,
onTouchBegan:function(touch,event)
{
var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());
if(cc.pDistance(centerPostion,locationInNode)<radius)
{
bIsActived = true;
}
return true;
},
onTouchMoved:function(touch,event)
{
if (bIsActived) {
var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var delta = touch.getDelta();
curPosition = cc.p(locationInNode.x+delta.x,locationInNode.y+delta.y);
if(cc.pDistance(centerPostion,curPosition)>radius)
{
curPosition = cc.pAdd(centerPostion,cc.pMult(GetDirection(locationInNode,centerPostion),radius));
}
joyStickPt.setPosition(curPosition);
curDirection = GetDirection(curPosition,centerPostion);
if((snake_list[0][1].x*curDirection.x+snake_list[0][1].y*curDirection.y)>=0.00001)
{
snake_list[0][1] = GetDirection(curPosition,centerPostion);
}
}
},
onTouchEnded:function(touch,event)
{
bIsActived = false;
curPosition = centerPostion;
joyStickPt.setPosition(curPosition);
}
});
addFlag = 0;
JoyStickBg = new cc.Sprite("res/bg.png");
JoyStickBg.x = 58;
JoyStickBg.y = 58;
JoyStickBg.opacity = 100;
joyStickPt = new cc.Sprite("res/pt.png");
joyStickPt.x = 58;
joyStickPt.y = 58;
var helloLabel = new cc.LabelTTF("Score:","",20);
helloLabel.x = 300;
helloLabel.y = 50;
score_label = new cc.LabelTTF("0","",38);
score_label.x = 400;
score_label.y = 50;
curPosition = cc.p(JoyStickBg.x,JoyStickBg.y);
centerPostion = cc.p(JoyStickBg.x,JoyStickBg.y);
winSize = cc.winSize;
startScene.js
var startLayer = cc.Layer.extend(
{ctor:function()
{
this._super();
var size = cc.winSize;
var helloLabel = new cc.LabelTTF("hello Word","",38);
helloLabel.x = size.width/2;
helloLabel.y = size.height/2;
this.addChild(helloLabel);
return true;
}});
var startScene = cc.Scene.extend(
{
onEnter:function(){
this._super();
//var newLayer = new startLayer();
var newLayer = ccs.load("res/startGame.json").node;
var btn_start = ccui.helper.seekWidgetByName(newLayer,"Button_5");
btn_start.addTouchEventListener(
function (sender,type)
{
if(type==ccui.Widget.TOUCH_ENDED)
{
cc.director.runScene(new gameScene());
}
}
);
this.addChild(newLayer);
}
});
var gameLayer = cc.Layer.extend(
{ctor:function()
{
this._super();
var joyStick = new joyStick();
this.addChild(joyStick);
return true;
}});
var gameScene = cc.Scene.extend(
{
onEnter:function(){
this._super();
var newLayer = new joyStick();
this.addChild(newLayer);
}
});
endGame.js
var endGame = cc.Scene.extend(
{ctor:function()
{
this._super();
var endGameLayer = ccs.load("res/endGame.json").node;
var label_score = ccui.helper.seekWidgetByName(endGameLayer,"label_score");
var btn_restart = ccui.helper.seekWidgetByName(endGameLayer,"Button_5");
btn_restart.addTouchEventListener(
function (sender,type)
{
if(type==ccui.Widget.TOUCH_ENDED)
{
this.removeFromParent();
var newGame = new cc.Scene();
newGame.addChild(new joyStick());
cc.director.runScene(newGame);
}
}
);
this.addChild(endGameLayer);
label_score.string = database.score;
return true;
}});