游戏界面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 object
scene = CCScene::node();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
GameScene *layer = GameScene::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return 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 enemy
float 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;
}
}
//background
CCSprite* 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);
//mainFish
CCTexture2D *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);
//menu
std::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 left
if(direction)
{
fish->setAnchorPoint(ccp(1,0));
fish->setPosition(ccp(0,startHeight*winSize.height/10));
}
//begin on right
else
{
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))
{
//如果小鱼出了屏幕,就remove
this->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);
else
mainFish->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.