首页 > 代码库 > 编写小游戏《贪头蛇》第一篇

编写小游戏《贪头蛇》第一篇

小游戏《贪头蛇》设计场景有: 主菜单、游戏主场景、游戏继续、游戏设置、游戏暂停、关于

进入游戏第一场景,显示该款游戏由哪家工作室或个人开发的


代码:

SnakeSceneManager类用来管理游戏所有场景的切换,包括创建新的场景,切换下一个场景,和关卡场景

OpenLayer类用来创建该场景

CCScene *SnakeSceneManager::createscene()
{
	CCScene *Scene = CCScene::create();
	//进入游戏第1个图层
	OpenLayer *openlayer = OpenLayer::create();//工作室logo
	Scene->addChild(openlayer);//加入该场景中

	return Scene;
	
}

init()函数创建背景图片,实现浅入浅出场景效果

bool OpenLayer::init()
{
	if (!CCLayer::init())
	{
		return true;
	}
	CCSize size = CCDirector::sharedDirector()->getVisibleSize();
	CCSprite *sprite1 = CCSprite::create("bn.png");//工作室图片
	sprite1->setAnchorPoint(ccp(0.5, 0.5));
	sprite1->setPosition(ccp(size.width/2, size.height/2));

	//先浅入场景再浅出
	CCActionInterval *act1 = CCFadeIn::create(1.5f);
	CCActionInterval *act2 = act1->reverse();
	//完成动作后切换到主菜单页面
	sprite1->runAction(CCSequence::create(act1, act2, CCCallFuncND::create(this, callfuncND_selector(OpenLayer::nextmenuscene),NULL), NULL));
	this->addChild(sprite1);

	return true;
}
切换下一个主菜单场景

void OpenLayer::nextmenuscene(CCNode* pNode, void* pVo)
{
	this->removeChild(pNode);
	ssm->MenuScene();
}
void SnakeSceneManager::MenuScene()
{
	CCScene *menu = CCScene::create();
	SnakeMenuScene *sms = SnakeMenuScene::create();//菜单图层
	menu->addChild(sms);
	CCDirector::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.5f, menu));
}

主菜单场景


代码:

主菜单场景类SnakeMenuScene,头文件

class SnakeMenuScene:public cocos2d::CCLayer
{
public:
	virtual bool init();
	CREATE_FUNC(SnakeMenuScene);

	void newgame(CCObject* pObject);//新游戏按钮回调
	void continuegame(CCObject *pObject);//游戏继续按钮回调
	void aboutgame(CCObject *pObject);//游戏关于按钮回调

	SnakeSceneManager *ssm1;
};
init()函数,创建这个菜单按钮

	if (!CCLayer::init())
	{
		return false;
	}
	CCSize size = CCDirector::sharedDirector()->getVisibleSize();
	//背景设置
	CCSprite *sp1 = CCSprite::create("snake_background.png");
	sp1->setAnchorPoint(ccp(0.0, 0.0));
	sp1->setPosition(ccp(0, 0));
	this->addChild(sp1);

	SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background.mp3", true);


	//new game 按钮
	CCMenuItemImage *image1 = CCMenuItemImage::create("newgameA.png", "newgameB.png", this, menu_selector(SnakeMenuScene::newgame));
	//continue 按钮
	CCMenuItemImage *image2 = CCMenuItemImage::create("continueA.png", "continueB.png", this, menu_selector(SnakeMenuScene::continuegame));
	//about 按钮
	CCMenuItemImage *image3 = CCMenuItemImage::create("aboutA.png", "aboutB.png", this, menu_selector(SnakeMenuScene::aboutgame));

	CCMenu *menu = CCMenu::create(image1, image2, image3, NULL);
	menu->setPosition(CCPointZero);
	this->addChild(menu);

菜单按钮出场动画,由上往下回落

CCNode *child=NULL;
	CCArray *pArray = menu->getChildren();
	for (int m=0; m<(pArray->count()); m++)//遍历菜单项
	{
		int h ;
		switch (m)//菜单项间隔
		{
		case 0:
			h=50; break;
		case 1:
			h=0; break;
		case 2:
			h=-50; break;
		}
		child = (CCNode*)pArray->objectAtIndex(m);
		child->setScale(0.5f);
		child->setPosition(size.width/2, size.height+10);
		CCActionInterval *ac1 = CCMoveTo::create(0.2f, ccp(size.width/2, -40));//起始位置
		CCActionInterval *ac2 = CCMoveTo::create(0.2f, ccp(size.width/2, size.height/2+h));//弹回的位置
		CCActionInterval *ac3 = CCEaseBackInOut::create(ac2);
		CCDelayTime *delay = CCDelayTime::create(0.2f*(pArray->count()-m-1));//每个菜单项间隔时间
		child->runAction(CCSequence::create(delay, ac1, ac3, NULL));
	}

菜单按钮回调函数

//切换到游戏主场景
void SnakeMenuScene::newgame(CCObject* pObject)
{
	CCLOG("newame...");
	ssm1->nextscene();
}
void SnakeMenuScene::continuegame(CCObject* pObject)
{
	CCLOG("continuegame...");
}
void SnakeMenuScene::aboutgame(CCObject* pObject)
{
	CCLOG("aboutgame...");
}