cocos2d-x学习(10)----大鱼吃小鱼(二)
来源:互联网 发布:淘宝商城二手电脑 编辑:程序博客网 时间:2024/05/17 05:01
游戏界面1:
游戏界面2:
游戏界面3:
游戏成功界面:
游戏失败界面:
详细代码:
GameScene.h
class GameScene : public cocos2d::CCLayer{public:virtual bool init();static cocos2d::CCScene* scene();virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);void backCallback( CCObject *pSender );//LAYER_NODE_FUNC(GameScene);// implement the "static node()" method manually CREATE_FUNC(GameScene);private://生成鱼void createFish(float dt);//检测鱼void checkFish(float dt);//屏幕上的鱼最多的总数:6,不包括自己std::vector<MyCCSprite> fishes;//可以吃的鱼std::vector<cocos2d::CCSprite*> canEatEnemy;//玩家自己cocos2d::CCSprite* mainFish;//触摸点的位置cocos2d::CCPoint touchLocation;float fishSize;};class MyCCSprite{public:MyCCSprite(cocos2d::CCSprite* _sprite,int _fishLevel):sprite(_sprite),fishLevel(_fishLevel){}cocos2d::CCSprite* sprite;int fishLevel;};
GameScene.cpp
cocos2d::CCScene* GameScene::scene(){CCScene * scene = NULL;do {// 'scene' is an autorelease objectscene = CCScene::node();CC_BREAK_IF(! scene);// 'layer' is an autorelease objectGameScene *layer = GameScene::create();CC_BREAK_IF(! layer);// add layer as a child to scenescene->addChild(layer);} while (0);// return the scenereturn scene;}bool GameScene::init(){bool bRet = false;do {CCSize winSize = CCDirector::sharedDirector()->getWinSize();if(g_isMusic){CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.05f);CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg.wav",true);}std::string titleStr = "You Can Eat:";CCLabelTTF* pLable = CCLabelTTF::labelWithString(titleStr.c_str(),"Thonburi",30);CC_BREAK_IF(!pLable);pLable->setColor(ccRED);pLable->setAnchorPoint(ccp(0,1));pLable->setPosition(ccp(0,winSize.height));this->addChild(pLable,1);//can eat enemyfloat enemyXPosition=0;for(int i=1;i<5;i++)for(int j=0;j<4;j++){char filename[20];//enemy10.png enemy11.png enemy12.png enemy13.png//enemy20.png enemy21.png enemy22.png enemy23.png//enemy30.png enemy31.png enemy32.png enemy33.png//enemy40.png sprintf_s(filename,"enemy%d%d.png",i,j);CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage(filename);float frameWidth = texture->getContentSize().width/5;float frameheight = texture->getContentSize().height;CCSprite* enemy=CCSprite::spriteWithTexture(texture,CCRectMake(0,0,frameWidth,frameheight));CC_BREAK_IF(!enemy);if(i!=1)//enemy->setIsVisible(false);enemy->setVisible(false);enemy->setPosition(ccp(enemyXPosition,winSize.height*0.85));enemyXPosition+=frameWidth/2+10;enemy->setAnchorPoint(ccp(0,0.5));enemy->setScale(0.5f);this->addChild(enemy,1);//在尾部加入一个数据canEatEnemy.push_back(enemy);if(i==4){enemy->setPosition(ccp(winSize.width/2,winSize.height*0.8));break;}}//backgroundCCSprite* pSprite = CCSprite::spriteWithFile("bg01.png");CC_BREAK_IF(!pSprite);pSprite->setScaleX(winSize.width/pSprite->getContentSize().width);pSprite->setScaleY(winSize.height/pSprite->getContentSize().height);pSprite->setPosition(ccp(winSize.width/2,winSize.height/2));this->addChild(pSprite,0);//mainFishCCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage("mainfish.png");mainFish=CCSprite::spriteWithTexture(texture,CCRectMake(0,0,100,69));CCAnimation *animation = CCAnimation::animation();//animation->setDelay(0.2f);animation->setDelayPerUnit(0.2f);for(int i=0;i<3;i++)//animation->addFrameWithTexture(texture,CCRectMake((float)i*100,0,100,69));animation->addSpriteFrameWithTexture(texture,CCRectMake((float)i*100,0,100,69));CCAnimate* animate = CCAnimate::actionWithAnimation(animation);animate->setDuration(0.5f);mainFish->runAction(CCRepeatForever::actionWithAction(animate));fishSize=0.4f;mainFish->setScale(fishSize);mainFish->setPosition(ccp(winSize.width/2,winSize.height/2));this->addChild(mainFish,1);//menustd::string str;str="Back";CCMenuItemFont* backMenu = CCMenuItemFont::create(str.c_str(),this,menu_selector(GameScene::backCallback));CCMenu* mn = CCMenu::menuWithItems(backMenu,NULL);mn->setPosition(ccp(0,0));backMenu->setAnchorPoint(ccp(1,0));backMenu->setPosition(ccp(winSize.width,0));this->addChild(mn,1);this->schedule(schedule_selector(GameScene::createFish));this->schedule(schedule_selector(GameScene::checkFish));this->setTouchEnabled(true);bRet = true;} while (0);return(bRet);}void GameScene::createFish(float dt){//屏幕上的鱼最多的总数:6,不包括自己if(fishes.size()>5)return;CCSize winSize = CCDirector::sharedDirector()->getWinSize();//开始的位置int startHeight=rand()%8;//鱼的方向int direction=rand()%2;CCLog("direction:%i", direction);//速度int speed = rand()%3+1;//鱼的等级int fishLeveltemp=rand()%10;//CCLog("fishLeveltemp:%i", fishLeveltemp);//鱼的等级int fishLevel;if(fishLeveltemp<5)fishLevel=1;else if(fishLeveltemp<7)fishLevel=2;else if(fishLeveltemp<9)fishLevel=3;else fishLevel=4;//CCLog("fishLevel:%i", fishLevel);//鱼的数目(第几个)int fishNum=rand()%4;if (fishLevel==4)fishNum=0;char filename[20];sprintf_s(filename,"enemy%d%d.png",fishLevel,fishNum);CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage(filename);float frameWidth = texture->getContentSize().width/5;float frameheight = texture->getContentSize().height;CCAnimation *animation = CCAnimation::animation();animation->setDelayPerUnit(0.2f);for(int i=0;i<3;i++)animation->addSpriteFrameWithTexture(texture,CCRectMake(i*frameWidth,0,frameWidth,frameheight));CCSprite* fish=CCSprite::spriteWithTexture(texture,CCRectMake(0,0,frameWidth,frameheight));//begin on leftif(direction){fish->setAnchorPoint(ccp(1,0));fish->setPosition(ccp(0,startHeight*winSize.height/10));}//begin on rightelse{fish->setAnchorPoint(ccp(0,0));fish->setPosition(ccp(winSize.width,winSize.height/10));fish->setFlipX(true);}this->addChild(fish,1);//不断的生成鱼(最多6条)fishes.push_back(MyCCSprite(fish,fishLevel));CCAnimate* animate = CCAnimate::actionWithAnimation(animation);animate->setDuration(0.5f);fish->runAction(CCRepeatForever::actionWithAction(animate));CCActionInterval *action=CCMoveBy::actionWithDuration(0.1f,ccp(speed*(direction?1:(-1)),0));fish->runAction(CCRepeatForever::actionWithAction(action));}void GameScene::checkFish(float dt){if (!mainFish->isVisible()){return;}CCSize winSize=CCDirector::sharedDirector()->getWinSize();CCRect screen=CCRectMake(0,0,winSize.width,winSize.height);CCRect mainFishBox=mainFish->boundingBox();for (std::vector<MyCCSprite>::iterator it=fishes.begin(); it != fishes.end();){CCSprite *fish =(*it).sprite;CCPoint position=fish->getPosition();CCRect fishBox=fish->boundingBox();//如果自己和其它的鱼碰到if (CCRect::CCRectIntersectsRect(mainFishBox,fishBox)){if((fishSize*5)>(*it).fishLevel+1){float temp = fishSize*5-(*it).fishLevel;if(temp<3){if(fishSize<1.2)fishSize+=0.05f/((int)temp+1);if((fishSize*5)>3&&!canEatEnemy[4]->isVisible())for(int i=4;i<=7;i++)canEatEnemy[i]->setVisible(true);if((fishSize*5)>4&&!canEatEnemy[8]->isVisible())for(int i=8;i<=11;i++)canEatEnemy[i]->setVisible(true);if(fishSize*5>5)canEatEnemy[12]->setVisible(true);//游戏成功if((*it).fishLevel==4){this->stopAllActions();this->setTouchEnabled(false);CCSprite* pSpriteWin = CCSprite::spriteWithFile("you_win.png");pSpriteWin->setPosition(ccp(winSize.width/2,winSize.height/2));this->addChild(pSpriteWin,2);}}//吃掉小鱼if(g_isSound)CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("eat.wav");this->removeChild(fish, true);it=fishes.erase(it);mainFish->setScale(fishSize);}else{//玩家自己被鱼吃掉this->stopAllActions();this->setTouchEnabled(false);CCSprite* pSpriteGameOver = CCSprite::spriteWithFile("game_over.png");pSpriteGameOver->setPosition(ccp(winSize.width/2,winSize.height/2));this->addChild(pSpriteGameOver,2);if(g_isSound)CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("byeat.wav");mainFish->setVisible(false);break;}}else if (!CCRect::CCRectContainsPoint(screen,position)){//如果小鱼出了屏幕,就removethis->removeChild(fish, true);it=fishes.erase(it);}else{it++;}}}void GameScene::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){//CCTouch *touch = (CCTouch*)pTouches->anyObject();//CCPoint location = touch->locationInView(touch->view());//touchLocation = CCDirector::sharedDirector()->convertToGL(location); CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); touchLocation = touch->getLocation(); }void GameScene::ccTouchesMoved( CCSet *pTouches, CCEvent *pEvent ){//CCTouch *touch = (CCTouch*)pTouches->anyObject();//CCPoint location = touch->locationInView(touch->view());//CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location); CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); CCPoint convertedLocation = touch->getLocation(); CCPoint fishLocation = mainFish->getPosition();fishLocation.x+=convertedLocation.x-touchLocation.x;fishLocation.y+=convertedLocation.y-touchLocation.y;CCSize winSize = CCDirector::sharedDirector()->getWinSize();CCRect rect=CCRectMake(0,0,winSize.width,winSize.height);if(CCRect::CCRectContainsPoint(rect,fishLocation)){if((convertedLocation.x-touchLocation.x)<0)mainFish->setFlipX(true);elsemainFish->setFlipX(false);mainFish->setPosition(fishLocation);}touchLocation=convertedLocation;}void GameScene::backCallback( CCObject *pSender ){if(g_isMusic)CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();CCScene * scene=HelloWorld::scene();CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene));}
说明:
上面的代码,是我在CSDN中,下载而来,但是现在忘了是哪位兄弟,在这里感谢这位无私奉献的兄弟,就是因为大家的共享和分享精神,知识才会积累和分享,技术才能更进步,大家才能学到更,谢谢csdner.
源码下载地址:
http://download.csdn.net/detail/hfreeman2008/5783445
- cocos2d-x学习(10)----大鱼吃小鱼(二)
- cocos2d-x学习(9)----大鱼吃小鱼(一)
- Cocos2d-x学习(二十一):cocos2d-x引擎结构
- Cocos2d-x学习(二十一):cocos2d-x引擎结构
- Cocos2d-x学习(二十五):ubuntu 配置cocos2d-x
- Cocos2d-x学习(二十一):cocos2d-x引擎结构
- Cocos2d-x学习笔记(二)Cocos2d-x整体框架
- Cocos2D-X学习笔记(二)
- cocos2d-x+cocosbuilder 学习笔记 (二)
- Cocos2d-x学习笔记(二)
- Cocos2d-x学习笔记(二)------HelloWorld
- cocos2d-x Physics 学习笔记(二)
- cocos2D-x 学习之路(二)
- Cocos2d-x学习(二十):cocos2d中的容器CCArray
- Cocos2d-x学习(二十):cocos2d中的容器CCArray
- Cocos2d-x学习(二十):cocos2d中的容器CCArray
- Cocos2d-x学习(二十六):cocos2d旋转矩阵推导
- Cocos2d-x学习(十六):Cocos2d-x内存管理(二)
- sql 分区表
- 从30岁到35岁:为你的生命多积累一些厚度
- poj 1511&zoj 2008 最短路
- 动态规划(自底向上)
- JDBC关键部分
- cocos2d-x学习(10)----大鱼吃小鱼(二)
- C++《MFC浏览器的实现》
- 绘画多行文字,设置行高,省略号
- 编辑ZWCAD实体--缩放对象
- Android 中如何关闭线程 .
- The First Pro with wxpython
- powerdesigner注释及名称转换
- 经济萧条时代让我们温暖的10行业(做好准备)
- php实现 数组行里最大 列里最小