Cocos2d-x 3.0final 终结者系列教程13-贪食蛇游戏案例(全)

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.0final 终结者系列教程13-贪食蛇游戏案例(全)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

---------------------------------快过节了,弄个案例,大家不妨假期做做,@H_403_1@

运行效果展示:@H_403_1@


@H_403_1@

全部代码和资源:@H_403_1@

http://download.csdn.net/detail/sdhjob/7424329@H_403_1@

1.准备资源@H_403_1@

背景图片menuback.png:@H_403_1@


@H_403_1@

节点图片@H_403_1@

greenstar.png @H_403_1@

redstar.png @H_403_1@

yellowstar.png
@H_403_1@

@H_403_1@

2.创建一个新项目(如何配置环境和创建新项目,参考前面教程):@H_403_1@

cocos new -p com.xdl.game -l cpp -d ~/Desktop/test0515 snamegame@H_403_1@

3.添加文件@H_403_1@

首先将HelloWoldScene.h HelloWorld.cpp移走,然后添加GameScene.h GameScene.cpp HelpScene.h HelpScene.cpp MainMenu.h MainMenu.cpp@H_403_1@

加上原来自动生成的AppDelegate.h 和AppDelegate.cpp共8个文件@H_403_1@

4.编码@H_403_1@

AppDelegate.h (这个文件基本没改动)@H_403_1@

@H_403_1@

#ifndef _APP_DELEGATE_H_@H_403_1@

#define _APP_DELEGATE_H_@H_403_1@

#include"cocos2d.h"@H_403_1@

class AppDelegate :privatecocos2d::Application@H_403_1@

{@H_403_1@

public:@H_403_1@

AppDelegate();@H_403_1@

virtual~AppDelegate();@H_403_1@

virtualboolapplicationDidFinishLaunching();@H_403_1@

virtualvoidapplicationDidEnterBackground();@H_403_1@

virtualvoidapplicationWillEnterForeground();@H_403_1@

};@H_403_1@

#endif// _APP_DELEGATE_H_@H_403_1@

AppDelegate.cpp@H_403_1@

#include"AppDelegate.h"@H_403_1@

#include"MainMenu.h"@H_403_1@

#include"SimpleAudioEngine.h" @H_403_1@

USING_NS_CC;@H_403_1@

usingnamespaceCocosDenshion;@H_403_1@

AppDelegate::AppDelegate() {@H_403_1@

}@H_403_1@

AppDelegate::~AppDelegate()@H_403_1@

{@H_403_1@

boolAppDelegate::applicationDidFinishLaunching() {@H_403_1@

// initialize director@H_403_1@

autodirector =Director::getInstance();@H_403_1@

autoglview = director->getOpenGLView();@H_403_1@

if(!glview) {@H_403_1@

glview =GLView::create("My Game");@H_403_1@

director->setOpenGLView(glview);@H_403_1@

}@H_403_1@

// turn on display FPS@H_403_1@

director->setDisplayStats(false);@H_403_1@

// set FPS. the default value is 1.0/60 if you don't call this@H_403_1@

director->setAnimationInterval(1.0/60);@H_403_1@

// create a scene. it's an autorelease object@H_403_1@

autoscene =MainMenu::createScene();@H_403_1@

// run@H_403_1@

director->runWithScene(scene);@H_403_1@

//开始播放背景音乐@H_403_1@

SimpleAudioEngine::getInstance()->playBackgroundMusic("background.mp3");@H_403_1@

returntrue;@H_403_1@

}@H_403_1@


@H_403_1@

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too@H_403_1@

voidAppDelegate::applicationDidEnterBackground() {@H_403_1@

Director::getInstance()->stopAnimation();@H_403_1@

// if you use SimpleAudioEngine,it must be pause@H_403_1@

SimpleAudioEngine::getInstance()->pauseBackgroundMusic();@H_403_1@

// this function will be called when the app is active again@H_403_1@

voidAppDelegate::applicationWillEnterForeground() {@H_403_1@

Director::getInstance()->startAnimation();@H_403_1@

403_1@

SimpleAudioEngine::getInstance()->resumeBackgroundMusic();@H_403_1@

}@H_403_1@ 说明: 在入口类中加入了背景音乐的播放,并且入口场景设计为MainMenu,往下看

MainMenu.h@H_403_1@

#ifndef __snakegame__MainMenu__@H_403_1@

#define __snakegame__MainMenu__@H_403_1@

#include"cocos2d.h"@H_403_1@

classMainMenu:publicLayer{@H_403_1@

public:@H_403_1@

staticScene* createScene();@H_403_1@

CREATE_FUNC(MainMenu);@H_403_1@

virtualboolinit();@H_403_1@

voidmenuCallBack(Ref* object);@H_403_1@

};@H_403_1@

#endif@H_403_1@


@H_403_1@ MainMenu.cpp #include"MainMenu.h"@H_403_1@

#include"GameScene.h"@H_403_1@

#include"HelpScene.h"@H_403_1@

Scene*MainMenu::createScene()@H_403_1@

{ autoscene=Scene::create();@H_403_1@

autolayer=MainMenu::create();@H_403_1@

scene->addChild(layer);@H_403_1@

returnscene;@H_403_1@

boolMainMenu::init(){@H_403_1@

if(!Layer::init())@H_403_1@

{@H_403_1@

returnfalse;@H_403_1@

autosize=Director::getInstance()->getWinSize();@H_403_1@

//添加背景@H_403_1@

autospriteBK=Sprite::create("menuback.png");@H_403_1@

spriteBK->setPosition(Point(size.width/2,size.height/2));@H_403_1@

this->addChild(spriteBK);@H_403_1@

//添加2菜单条目@H_403_1@

automenuItemStart=MenuItemFont::create("Start",CC_CALLBACK_1(MainMenu::menuCallBack,this));@H_403_1@

menuItemStart->setTag(1);@H_403_1@

automenuItemHelp=MenuItemFont::create("Help",51); font-family:Arial; font-size:14px; line-height:26px"> menuItemHelp->setTag(2);@H_403_1@

automenu=Menu::create(menuItemStart,menuItemHelp,NULL);@H_403_1@

menu->setPosition(Point::ZERO);@H_403_1@

menuItemStart->setPosition(Point(size.width-menuItemStart->getContentSize().width-100,menuItemStart->getContentSize().height+10));@H_403_1@

menuItemHelp->setPosition(Point(size.width-menuItemHelp->getContentSize().width-10,menuItemHelp->getContentSize().height+10));@H_403_1@

this->addChild(menu);@H_403_1@

returntrue;@H_403_1@

voidMainMenu::menuCallBack(Ref* object){@H_403_1@

autotarget=(Node*)object;@H_403_1@

Scene* scene;@H_403_1@

switch(target->getTag()) {@H_403_1@

case1://startgame@H_403_1@

scene=Game::createScene();@H_403_1@

break;@H_403_1@

case2://Helpgame@H_403_1@

scene=Help::createScene();@H_403_1@

default:@H_403_1@

Director::getInstance()->replaceScene(scene);@H_403_1@

}@H_403_1@ 说明:在菜单场景中实现了跳转到帮助场景和游戏场景,往下看: HelpScene.h@H_403_1@

#ifndef __snakegame__HelpScene__@H_403_1@

#define __snakegame__HelpScene__@H_403_1@

USING_NS_CC;@H_403_1@

classHelp:publicLayer{@H_403_1@

public:@H_403_1@

staticScene* createScene();@H_403_1@

CREATE_FUNC(Help);@H_403_1@

virtualboolinit();@H_403_1@

voidmenuCallBack(Ref* object);@H_403_1@

#endif@H_403_1@ HelpScene.cpp #include"HelpScene.h"@H_403_1@

#include"MainMenu.h"@H_403_1@

Scene*Help::createScene(){@H_403_1@

autoscene=Scene::create();@H_403_1@

autolayer=Help::create();@H_403_1@

returnscene;@H_403_1@

}@H_403_1@

boolHelp::init(){@H_403_1@

{@H_403_1@

returnfalse;@H_403_1@

}@H_403_1@

autosize=Director::getInstance()->getWinSize();@H_403_1@

spriteBK->setOpacity(75);@H_403_1@

//帮助信息@H_403_1@

autolabelscore=Label::create("帮助信息","宋体",25);@H_403_1@

labelscore->setPosition(Point(size.width-80,size.height-50));@H_403_1@

this->addChild(labelscore);@H_403_1@

//返回按钮@H_403_1@

automenuItemBack=MenuItemFont::create("Back",CC_CALLBACK_1(Help::menuCallBack,51); font-family:Arial; font-size:14px; line-height:26px"> automenu=Menu::create(menuItemBack,51); font-family:Arial; font-size:14px; line-height:26px"> menuItemBack->setPosition(Point(size.width-menuItemBack->getContentSize().width-100,menuItemBack->getContentSize().height+10));@H_403_1@

voidHelp::menuCallBack(Ref* object){@H_403_1@

autoscene=MainMenu::createScene();@H_403_1@

Director::getInstance()->replaceScene(scene);@H_403_1@

说明:这里只是实现了一个帮助信息显示,可以返回到菜单,下面看游戏场景 GameScene.h@H_403_1@

#ifndef __snakegame__GameScene__@H_403_1@

#define __snakegame__GameScene__@H_403_1@

enumclassENUM_DIR{@H_403_1@

DIR_UP,@H_403_1@

DIR_DOWN,51); font-family:Arial; font-size:14px; line-height:26px"> DIR_LEFT,51); font-family:Arial; font-size:14px; line-height:26px"> DIR_RIGHT,51); font-family:Arial; font-size:14px; line-height:26px"> DIR_STOP@H_403_1@

};@H_403_1@

classSnakeNode:publicSprite@H_403_1@

{@H_403_1@

public:@H_403_1@

enumENUM_DIRm_dir;//移动方向@H_403_1@

intnodeType; //节点类型1蛇头2身体3食物@H_403_1@

intm_row,m_col; //当前节点的行列坐标@H_403_1@

@H_403_1@

staticSnakeNode* create(inttype);@H_403_1@

virtualboolinit(inttype);@H_403_1@

voidsetPositionRC(introw,intcol);//设置节点的坐标@H_403_1@

};@H_403_1@

classGame:publicLayer{@H_403_1@

SnakeNode* spFood;//食物@H_403_1@

SnakeNode* spHead;//蛇头@H_403_1@

intm_score;@H_403_1@

Vector<SnakeNode*> allBody;//身体@H_403_1@

CREATE_FUNC(Game);@H_403_1@

voidgameLogic(floatt);@H_403_1@

voidnewBody();//添加一个新的身体节点@H_403_1@

voidmoveBody();//移动所有的身体节点@H_403_1@

GameScene.cpp //@H_403_1@

// GameScene.cpp@H_403_1@

// Created byshen on 14-5-27.@H_403_1@

//@H_403_1@


@H_403_1@

#include"SimpleAudioEngine.h"@H_403_1@

usingnamespaceCocosDenshion;@H_403_1@

Scene * Game::createScene(){@H_403_1@

autoscene=Scene::create();@H_403_1@

autolayer=Game::create();@H_403_1@

scene->addChild(layer);@H_403_1@

returnscene;@H_403_1@


@H_403_1@

SnakeNode* SnakeNode::create(inttype)@H_403_1@

SnakeNode *pRet =newSnakeNode();@H_403_1@

if(pRet && pRet->init(type))@H_403_1@

pRet->autorelease();@H_403_1@

returnpRet;@H_403_1@

else@H_403_1@

deletepRet;@H_403_1@

pRet =NULL;@H_403_1@

returnNULL;@H_403_1@

}@H_403_1@

boolSnakeNode::init(inttype){@H_403_1@

if(!Sprite::init())@H_403_1@

returnfalse;@H_403_1@

///根据类型不同初始化不同的纹理@H_403_1@

switch(type) {@H_403_1@

case1://蛇头@H_403_1@

{autosprite=Sprite::create("redstar.png");@H_403_1@

sprite->setAnchorPoint(Point::ZERO);@H_403_1@

this->addChild(sprite);@H_403_1@

m_dir=ENUM_DIR::DIR_RIGHT;//向右移动@H_403_1@

break;@H_403_1@

case2://身体@H_403_1@

{autosprite=Sprite::create("greenstar.png");@H_403_1@

this->addChild(sprite);@H_403_1@

@H_403_1@

m_dir=ENUM_DIR::DIR_STOP;//@H_403_1@

case3://食物@H_403_1@

{autosprite=Sprite::create("yellowstar.png");@H_403_1@

default:@H_403_1@

}@H_403_1@

returntrue;@H_403_1@

voidSnakeNode::setPositionRC(introw,intcol)//设置节点的坐标@H_403_1@

{ this->m_row=row;@H_403_1@

this->m_col=col;@H_403_1@

setPosition(Point(col*32,row*32));@H_403_1@

bool Game::init(){@H_403_1@

if(!Layer::init())@H_403_1@

}@H_403_1@

//添加地图@H_403_1@

autodraw=DrawNode::create();@H_403_1@

draw->setAnchorPoint(Point::ZERO);@H_403_1@

draw->setPosition(Point::ZERO);@H_403_1@

this->addChild(draw);@H_403_1@

for(inti=0;i<11;i++)@H_403_1@

{@H_403_1@

draw->drawSegment(Point(0,32*i),Point(320,1,Color4F(1,1));@H_403_1@

draw->drawSegment(Point(32*i,0),Point(32*i,320),51); font-family:Arial; font-size:14px; line-height:26px"> //添加蛇头@H_403_1@

spHead=SnakeNode::create(1);@H_403_1@

@H_403_1@

@H_403_1@

this->addChild(spHead);@H_403_1@

//添加身体@H_403_1@

//添加食物@H_403_1@

spFood=SnakeNode::create(3);@H_403_1@

@H_403_1@

introw=rand()%10;@H_403_1@

intcol=rand()%10;@H_403_1@

spFood->setPositionRC(row,col);@H_403_1@

this->addChild(spFood);@H_403_1@

autosize=Director::getInstance()->getWinSize();@H_403_1@

//添加背景@H_403_1@

autospriteBK=Sprite::create("menuback.png");@H_403_1@

spriteBK->setPosition(Point(size.width/2,size.height/2));@H_403_1@

spriteBK->setOpacity(75);@H_403_1@

this->addChild(spriteBK);@H_403_1@

//分数显示@H_403_1@

m_score=0;@H_403_1@

autolabelscore=Label::create("分数:0","宋体",25);@H_403_1@

labelscore->setTag(110);@H_403_1@

labelscore->setPosition(Point(size.width-80,size.height-50));@H_403_1@

this->addChild(labelscore);@H_403_1@

//返回按钮@H_403_1@

automenuItemBack=MenuItemFont::create("Back",CC_CALLBACK_1(Game::menuCallBack,this));@H_403_1@

automenu=Menu::create(menuItemBack,NULL);@H_403_1@

menu->setPosition(Point::ZERO);@H_403_1@

menuItemBack->setPosition(Point(size.width-menuItemBack->getContentSize().width-50,menuItemBack->getContentSize().height+10));@H_403_1@

this->addChild(menu);@H_403_1@

//计划任务@H_403_1@

this->schedule(schedule_selector(Game::gameLogic),0.5);@H_403_1@

//加入用户触摸事件侦听@H_403_1@

autolistener=EventListenerTouchOneByOne::create();@H_403_1@

listener->setSwallowTouches(true);@H_403_1@

listener->onTouchBegan=[&](Touch * t,Event * e){@H_403_1@

//改变贪食蛇移动的方向@H_403_1@

intcol=t->getLocation().x/32;@H_403_1@

introw=t->getLocation().y/32;@H_403_1@

intspHeadCol=spHead->getPositionX()/32;@H_403_1@

intspHeadRow=spHead->getPositionY()/32;@H_403_1@

if(abs(spHeadCol-col)>abs(spHeadRow-row))@H_403_1@

if(spHeadCol<col)@H_403_1@

{@H_403_1@

spHead->m_dir=ENUM_DIR::DIR_RIGHT;@H_403_1@

}else@H_403_1@

spHead->m_dir=ENUM_DIR::DIR_LEFT;@H_403_1@

}@H_403_1@

else@H_403_1@

{if(spHeadRow<row)@H_403_1@

{@H_403_1@

spHead->m_dir=ENUM_DIR::DIR_UP;@H_403_1@

}else@H_403_1@

{@H_403_1@

spHead->m_dir=ENUM_DIR::DIR_DOWN;@H_403_1@

}@H_403_1@

@H_403_1@

}@H_403_1@

returntrue;@H_403_1@

};@H_403_1@

_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);@H_403_1@

void Game::menuCallBack(Ref * object){@H_403_1@

autoscene=MainMenu::createScene();@H_403_1@

Director::getInstance()->replaceScene(scene);@H_403_1@

voidGame::gameLogic(floatt)@H_403_1@

{ moveBody();//移动所有身体节点@H_403_1@

//蛇头移动@H_403_1@

switch(spHead->m_dir) {@H_403_1@

caseENUM_DIR::DIR_RIGHT:@H_403_1@

spHead->runAction(MoveBy::create(0.3,Point(32,0)));@H_403_1@

spHead->m_col++;@H_403_1@

caseENUM_DIR::DIR_LEFT:@H_403_1@

32,51); font-family:Arial; font-size:14px; line-height:26px"> spHead->m_col--;@H_403_1@

caseENUM_DIR::DIR_DOWN:@H_403_1@

0,-32)));@H_403_1@

spHead->m_row--;@H_403_1@

caseENUM_DIR::DIR_UP:@H_403_1@

32)));@H_403_1@

spHead->m_row++;@H_403_1@

break;@H_403_1@

//碰撞检测@H_403_1@

if(spHead->m_row==spFood->m_row&&@H_403_1@

spHead->m_col==spFood->m_col)@H_403_1@

{//音效的播放@H_403_1@

SimpleAudioEngine::getInstance()->playEffect("eat.wav");@H_403_1@

//分数增加@H_403_1@

this->m_score+=100;@H_403_1@

Label * label=(Label *)this->getChildByTag(110);@H_403_1@

charstrscore[20];@H_403_1@

sprintf(strscore,"分数:%d",m_score);@H_403_1@

label->setString(strscore);@H_403_1@

//食物产生新的位置@H_403_1@

introw=rand()%10;@H_403_1@

intcol=rand()%10;@H_403_1@

spFood->setPositionRC(row,51); font-family:Arial; font-size:14px; line-height:26px"> //添加节点@H_403_1@

newBody();@H_403_1@

}@H_403_1@

voidGame::newBody()//添加一个新的身体节点@H_403_1@

autobodynode=SnakeNode::create(2);@H_403_1@

//设置这个节点的方向和坐标@H_403_1@

if(allBody.size()>0)//有身体节点@H_403_1@

{//最后一个身体的节点@H_403_1@

autolastbody=allBody.at(allBody.size()-1);@H_403_1@

bodynode->m_dir=lastbody->m_dir;@H_403_1@

switch(bodynode->m_dir) {@H_403_1@

caseENUM_DIR::DIR_UP:@H_403_1@

bodynode->setPositionRC(lastbody->m_row-1,lastbody->m_col);@H_403_1@

break;@H_403_1@

caseENUM_DIR::DIR_DOWN:@H_403_1@

bodynode->setPositionRC(lastbody->m_row+1,51); font-family:Arial; font-size:14px; line-height:26px"> caseENUM_DIR::DIR_LEFT:@H_403_1@

bodynode->setPositionRC(lastbody->m_row,lastbody->m_col+1);@H_403_1@

caseENUM_DIR::DIR_RIGHT:@H_403_1@

1);@H_403_1@

default:@H_403_1@

}else@H_403_1@

{//新节点的方向等于蛇头的方向@H_403_1@

bodynode->m_dir=spHead->m_dir;@H_403_1@

bodynode->setPositionRC(spHead->m_row-1,spHead->m_col);@H_403_1@

bodynode->setPositionRC(spHead->m_row+1,51); font-family:Arial; font-size:14px; line-height:26px"> bodynode->setPositionRC(spHead->m_row,spHead->m_col+1);@H_403_1@

1);@H_403_1@

//添加节点到当前图层@H_403_1@

this->addChild(bodynode);@H_403_1@

//添加节点到集合中@H_403_1@

allBody.pushBack(bodynode);@H_403_1@

voidGame::moveBody()//移动所有的身体节点@H_403_1@

if(allBody.size()==0){return;}@H_403_1@

for(autobodynode:allBody)@H_403_1@

switch(bodynode->m_dir) {@H_403_1@

bodynode->runAction(MoveBy::create(0.3,51); font-family:Arial; font-size:14px; line-height:26px"> bodynode->m_col++;@H_403_1@

bodynode->m_col--;@H_403_1@

bodynode->m_row--;@H_403_1@

bodynode->m_row++;@H_403_1@

//移动完成之后,改变每个body的方向@H_403_1@

for(inti=allBody.size()-1;i>0;i--)@H_403_1@

{//每个节点的方向调整为它前一个节点的方向@H_403_1@

allBody.at(i)->m_dir=allBody.at(i-1)->m_dir;@H_403_1@

allBody.at(0)->m_dir=spHead->m_dir;@H_403_1@

--------------------------------------------祝你成功----------------------- 原文链接:https://www.f2er.com/cocos2dx/344214.html

猜你在找的Cocos2d-x相关文章