cocos2d游戏制作代码及过程

更新时间:2023-05-27 08:26:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《被逼的鱼》游戏说明

一、游戏介绍:

被逼的鱼(英文名:BABY FISH)是一款简单的手机游戏,它的界面操作简单,没有太多的操作步骤,适合很多群体的玩家。如:7~12岁的少年儿童、60岁以上的老年人、还有少数的青年玩家。而它之所以会为这些群体接受的另一些因素则是:这款游戏可以提高玩家的反应能力,在玩游戏的时候需要眼睛和手紧密的配合,从而在玩游戏的时候才会得心应手。所以,这款游戏对于开发儿童智力,提高反应能力有很大的帮助;同时,这款游戏也适用于老年人玩,它可以对防御老年痴呆有一定的帮助,因为游戏很好的帮助他们动脑和手。因此,我觉得这款游戏也有它开发的必要。

二、游戏界面:

1、开始界面:

游戏的开始界面主要包括:开始背景、进入游戏界面的开始按钮“NEW GAME”、游戏背景介绍的按钮“INFORMATION”。

开始界面可以帮助玩家了解游戏背景和操作步骤。通过开始界面背景,玩家可以大概了解到游戏将会在海底进行;还有游戏的界面操作。

图1-1 开始界面

图1-2 切换效果

图1-3 说明界面

2、游戏界面:

游戏界面主要包括:游戏界面背景、各种各样的坠落物、玩家扮演的鱼、左上角的游戏分数、右上角玩家的生命值。鱼在游戏中可以左右移动,从而避免与

坠落物发生碰撞,玩家在玩游戏的时候还会有背景音乐,背景音乐很好地创造了游戏的氛围。而游戏界面也与游戏背景相符合,都是在海底进行。

图2-1 开始游戏

图2-2 死亡一次

3、结束界面:

游戏结束界面主要包括:游戏结束(GAME OVER)、返回新游戏(NEW GAME)。 从这个界面可以获取游戏时间,五秒后又将返回开始界面。

图3-1 结束界面

三、游戏规则

游戏中需操作的是左右移动键(“←”、“→”),玩家通过键盘操作来避免鱼与坠落物相碰,玩家扮演的鱼一共有3条性命,鱼每与坠落物碰一次就会少一条性命,直到鱼的3条性命用完,游戏GAME OVER。若是还想继续游戏,则点击按钮“NEW GAME”继续游戏。

四、前期准备

被逼的鱼

开始界面:(注:页码为《Cocos2d-x游戏开发之旅》相关页码代码)

1.加载游戏封面(p30)

2.开始按钮(p49)

游戏界面:

1.添加坠落物

2.碰撞之后转结束界面

3.背景音乐

4.主角左右移动

5.设置坠落物出现范围

6.释放消失坠落物

结束界面:

1.获取游戏时间(见下文网络参照)(后期改为计分模式)

2.显示时间(见网络参照)

3.五秒后返回开始界面(后期改为按钮转换界面)

4.结束条件判断

以下为前期参考代码

(精灵运动参考)HelloCPP 三个按钮 :精灵到100,100 ;放大1.5倍 ;跳

跃到100,100;

CCMenuItemImage

*item=CCMenuItemImage::create("CloseNormal.png","CloseNormal.png","CloseNormal.png",this,menu_selector(HelloWorld::move));

item->setPosition(ccp(0,0)); CCMenu *menu=CCMenu::create(item,NULL); this->addChild(menu,0); CCMenuItemImage CCSize Screensize=CCDirector::sharedDirector()->getVisibleSize(); player=CCSprite::create("HelloWorld.png"); CCPoint origin=CCDirector::sharedDirector()->getVisibleOrigin(); player->setPosition(ccp(Screensize.width/4+origin.x,Screensize.height/4+origthis->addChild(player,0); in.y)); //init()中添加精灵

*item1=CCMenuItemImage::create("CloseNormal.png","CloseNormal.png","CloseNormal.png",this,menu_selector(HelloWorld::enlarge));

item1->setPosition(ccp(50,50)); CCMenu *menu1=CCMenu::create(item1,NULL); this->addChild(menu1,0); CCMenuItemImage

*item2=CCMenuItemImage::create("CloseNormal.png","CloseNormal.png","CloseNormal.png",this,menu_selector(HelloWorld::jump));

void HelloWorld::move(CCObject *obj1){

CCActionInterval *actionTo=CCMoveTo::create(2,CCPointMake(100,100));

}

void HelloWorld::enlarge(CCObject *obj2){

}

void HelloWorld::jump(CCObject *obj3)

{

} CCActionInterval *actionTo=CCJumpTo::create(2,CCPointMake(200,200),54,4); player->runAction(actionTo); CCActionInterval *actionTo=CCScaleTo::create(2,1.3f); player->runAction(actionTo); player->runAction(actionTo); item2->setPosition(ccp(100,100)); CCMenu *menu2=CCMenu::create(item2,NULL); this->addChild(menu2,0); //init()中控制精灵

//init()下 menuCloseCallback()上 创建功能函数

void move(CCObject *obj1); void enlarge(CCObject *obj2); void jump(CCObject *obj3); cocos2d::CCSprite *player;

//头文件CCLayer中声明功能函数以及变量player

背景音乐

HelloWorldScene.cpp添加头文件

#include "SimpleAudioEngine.h"

CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("kalimba.mp3"); (inti()初始化中设置背景音乐)

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("system.wav");

(函数中点击引用音乐,初始化添加this->setTouchEnabled(true);)

右键HelloWorld-打开属性-选择C/C++下常规-编辑附加包含目录-选择(添加)路径$(ProjectDir)..\..\..\..\cocos2dx\CocosDenshion\ios

精灵随机从左到右

void HelloWorld::creatTo(float);

void HelloWorld::Move(float d);

Screensize=CCDirector::sharedDirector()->getVisibleSize(); this->schedule(schedule_selector(HelloWorld::creatTo), 1.5f); //时间函数(创建)

void HelloWorld::creatTo(float d)

{

} CCSprite *player=CCSprite::create("CloseNormal.png"); //创建精灵 player->setPosition(ccp(0,y)); //(设置坐标变量) addChild(player,1); //添加精灵 CCMoveTo *scale=CCMoveTo::create(4,ccp((Screensize.width),y)); //(控制精灵)移动到 int random= CCRANDOM_0_1()*(Screensize.height); //随机函数 y=random; player->runAction(scale); //创建 player->schedule(schedule_selector(HelloWorld::Move), 0.1f); //时间函数(移动)

void HelloWorld::Move(float d)

{

} CCSize Screensize=CCDirector::sharedDirector()->getVisibleSize(); //获取银幕尺寸 x=Screensize.width; if(this->getPositionX()>=x) { } this->removeFromParent(); //消失函数

//少用全局变量 避免重复

网络参照获取时间: long millisecondNow()

{

struct cc_timeval now;

CCTime::gettimeofdayCocos2d(&now, NULL);

return (_sec * 1000 + _usec / 1000);

}

五、游戏的核心代码

本工程包含AppDelegate、StartGame、Information、GameLayer、GameMark、GameOver项,以下为各个项核心代码,顺序以游戏顺序为主:

1、从AppDelegate转入开始场景StartGame

CCScene *pScene = StartGame::scene();

pDirector->runWithScene(pScene);

2、A:开始StartGame场景中添加背景

CCSize size=CCDirector::sharedDirector()->getWinSize();

B:开始StartGame场景中添加按钮

CCMenuItemImage* startGameItem=CCMenuItemImage::create(

管理多个按钮

CCMenu *mainmenu=CCMenu::create(startGameItem,IntroduceItem,NULL);

点击第一个按钮效果函数--切换场景

void StartGame::newGame(CCObject* pSender)

{

CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.0f,GameLayer::scene()));

}

//Introduce介绍场景按钮添加同上

3、游戏GameLayer场景添加多条命

blood = 3;

CCSpriteBatchNode* uib = CCSpriteBatchNode::create("z.png"); blood1 = CCSprite::createWithTexture(uib->getTexture()); blood1->setPosition(ccp(450,visibleSize.height - 10)); blood1->setScale(0.2f); uib->addChild(blood1); blood2 = CCSprite::createWithTexture(uib->getTexture()); blood2->setPosition(ccp(470,visibleSize.height - 10)); blood2->setScale(0.2f); mainmenu->setPosition(CCPointZero); this->addChild(mainmenu,1,1); "B00.png", "B0.png", this, menu_selector(StartGame::newGame)); CCSprite *bj=CCSprite::create("BEGIN.png"); bj->setPosition(ccp(size.width/2,size.height/2)); this->addChild(bj); startGameItem->setScale(0.5); startGameItem->setPosition(ccp(size.width/2+140,size.height/2+50));//其他按钮同上

blood3 = CCSprite::createWithTexture(uib->getTexture()); blood3->setPosition(ccp(490,visibleSize.height - 10)); blood3->setScale(0.2f); uib->addChild(blood3); addChild(uib,4); gameover = CCSprite::create("B22.png"); gameover->setAnchorPoint(ccp(0.5,0.5)); gameover->setPosition(ccp(0,0)); gameover->setPosition(ccp(visibleSize.width/2,visibleSize.height/2 + 35)); gameover->setVisible(false); gameover->setScale(0.5); addChild(gameover,5);

void GameLayer::setherohurt(){

}

void GameLayer::setover(){

CCMenu* pMenu = (CCMenu *)this->getChildByTag(25); pMenu->setVisible(true); yu->stopAllActions(); switch(blood){ case 3: } CCActionInterval*action = CCBlink::create(5, 10); yu->runAction(action); schedule(schedule_selector(GameLayer::resetreduce), 5.0f); isreduce = true; blood1->setVisible(false); blood --; break; blood2->setVisible(false); blood --; break; blood3->setVisible(false); blood --; break; if(! isover){ } break; isover = true; setover(); case 2: case 1: case 0:

} gameover->setVisible(true); gameover->setScale(0); pMenu->setScale(0); pMenu->runAction(CCScaleTo::create(0.5,1)); gameover->runAction(CCScaleTo::create(0.5,0.5));

void GameLayer::resetreduce(float dt){

}

添加多条精灵

void GameLayer::MyMethod(float at)

{

gmark->addnumber(100); CCSize Screensize=CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin=CCDirector::sharedDirector()->getVisibleOrigin(); int r=rand()% int(Screensize.width); int i=CCRANDOM_0_1()*7+1; CCMoveTo* move=CCMoveTo::create(4,ccp(r,-150)); switch(i) { case 1: player=CCSprite::create("aa.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f);break; _player->addObject(player); player=CCSprite::create("bb.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f);break; _player->addObject(player); player=CCSprite::create("cc.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f);break; _player->addObject(player); isreduce = false; CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.1f,GameOver::scene())); case 2: case 3:

} } player=CCSprite::create("dd.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f);break; _player->addObject(player); player=CCSprite::create("ee.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f);break; _player->addObject(player); player=CCSprite::create("ff.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f);break; _player->addObject(player); player=CCSprite::create("gg.png"); player->setPosition(ccp(r,Screensize.height+10)); this->addChild(player,0); player->runAction(move); player->schedule(schedule_selector(GameLayer::remove),0.1f); _player->addObject(player); case 5: case 6: default:

主角跟踪移动

this->setTouchEnabled(true);

void GameLayer::remove(float f)

{

}

void GameLayer::ccTouchesMoved(CCSet* pTouches, CCEvent* pEvent)

{

CCSetIterator it = pTouches->begin(); if (this->getPositionY()<-10) { } removeFromParentAndCleanup(true);

} CCPoint touchLocation = touch->getLocation(); yu->setPosition(touchLocation);

跟新鱼碰撞

void GameLayer::updateCollision(float delta){

CCArray* player_to_delete=new CCArray; player_to_delete->retain(); CCObject* obj_player; CCARRAY_FOREACH(_player,obj_player){ CCSprite* player=(CCSprite*)obj_player; CCRect

yu_qy=CCRectMake(yu->getPositionX()+5,yu->getPositionY()+5,yu->getContentSize().width+10,yu->getContentSize().height+10);

CCRect

player_qy=CCRectMake(player->getPositionX()+5,player->getPositionY()+5,player->getContentSize().width,player->getContentSize().height);

}

背景音乐

#include "SimpleAudioEngine.h"

CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background.mp3"

4.显示分数GameMark项

GameMark层init函数新建计数图

A、bits = CCArray::create();

CCSprite *title = CCSprite::create("score.png"); title->setPosition(ccp(20,size.height - 10)); title->setScale(0.5); this->addChild(title); } CCARRAY_FOREACH(player_to_delete,obj_player){ } player_to_delete->release(); _player->removeObject(obj_player); CCSprite* play=(CCSprite*)obj_player; this->GameLayer::removePlayer(play); if (yu_qy.intersectsRect(player_qy)) { } CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("bung.mp3"); setherohurt(); player_to_delete->addObject(player);

{ } bits->retain(); mark = 0; CCSprite * shu = CCSprite::create("shu.png"); ui = shu->getTexture(); shu->setScale(0.5); shu->setTextureRect(CCRectMake(117,0,13,16)); shu->setPosition(ccp(75- i * 8,visibleSize.height - 10)); bits->addObject(shu); addChild(shu);

改变分数函数

void GameMark::addnumber(int var) {

}else{ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); }else{ } temp = (mark % 1000) / 100; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(2))->setTextureRect(CCRectMake((temp - 1) * ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(1))->setTextureRect(CCRectMake(117,0,13,16)); mark += var; int temp = mark % 10; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(0))->setTextureRect(CCRectMake((temp - 1) * 13,0,13,16)); }else{ } temp = (mark % 100) / 10; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(1))->setTextureRect(CCRectMake((temp - 1) * ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(0))->setTextureRect(CCRectMake(117,0,13,16)); 13,0,13,16)); 13,0,13,16));

} } temp = (mark % 10000) / 1000; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(3))->setTextureRect(CCRectMake((temp - 1) * 13,0,13,16)); }else{ } temp = mark / 10000; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(4))->setTextureRect(CCRectMake((temp - 1) * ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(3))->setTextureRect(CCRectMake(117,0,13,16)); 13,0,13,16)); }else{ } ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(4))->setTextureRect(CCRectMake(117,0,13,16));

B、GameLayer层声明添加计数项

gmark=new GameMark();

addChild(gmark,2);

GameLayer层中MyMethod函数调用计数函数 gmark->addnumber(100);

5.GameOver层按钮、背景同StartGame层

本文来源:https://www.bwwdw.com/article/f0b4.html

Top