最难游戏代码续2_skybeauty_新浪博客

 

#ifndef __HardestGameEver__explore__

#define __HardestGameEver__explore__

#include "cocos2d.h"

USING_NS_CC;

#include <iostream>

class Explore:public CCSprite

{

public:

  virtual bool initWithFile(const char *pszFilename);

   static Explore*create(const char *pszFilename);

  virtual void update(float dt);

  virtual void move(CCPoint pos);

  virtual void stop();

  virtual void produce(int tag);

  virtual void deletsprite();

  virtual void buttonpress();

  virtual void scoreshow(CCString*s);

  virtual void trans();

   ~Explore();

   

   

    

    

    

private:

   CCLabelTTF*label;

   int j,k,m;

   CCString*s1;

   CCString*score1;

};


#endif




#include "explore.h"

#include "SimpleAudioEngine.h"

#include "GameScene2.h"

#include "fail.h"

#include "LoadingScene.h"


bool Explore::initWithFile(const char *pszFilename)

{

   if (!CCSprite::initWithFile(pszFilename))

    {

        return false;

    }

    


       return true;

}

Explore*Explore::create(const char *pszFileName)

{

   Explore * pRet = new Explore();

   if (pRet&&pRet->initWithFile(pszFileName))

    {

        pRet->autorelease();

       return pRet;

    }

   else

    {

        CC_SAFE_RELEASE_NULL(pRet);

       return NULL;

    }

}

void Explore::update(float dt)

{

   

   if (m)

    {

       m--;

    }

   else 

    {

       if(k)

        {

           k--;

           m = 6;

        }

       else

        {

           if (j)

            {

               j--;

               k = 9;

                CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("WeaponBlink.mp3");

            }

        }

    }

    

    s1 = CCString::createWithFormat("%d:%d%d",j,k,m);

    label->setString(s1->getCString());

    

    

   if (m==0&&j==0&&k==0)

    {

    

       this->stop();

        GameSceneB::shared()->stopspritemove();

      

    }

   if (j==0&&k<6)

    {

       label->setColor(ccRED);

    }


}

//控制炸弹的移动

void Explore::move(CCPoint pos)

{

    GameSceneB::shared()->stopbutton(1);

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

   CCMoveTo*move = CCMoveTo::create(0.5f, ccp(pos.x, size.height*0.5));

    

   this->runAction(move);

    GameSceneB::shared()->scheduleOnce(schedule_selector(GameSceneB::begaintouch), 0.5f);

    this->scheduleUpdate();


           

}

//显示爆炸效果

void Explore::stop()

{

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

           

    CCSprite * fail = CCSprite::create("14_fail_bg-iphone4.png");

   GameSceneB::shared()->addChild(fail,1,100);

    fail->setPosition(ccp(this->getPositionX(), size.height*0.6));

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("14_bombExplod.mp3");

    this->scheduleOnce(schedule_selector(Explore::deletsprite), 0.25f);

    

}

//产生随机数

void Explore::produce(int tag)

{

    

       j = rand()%tag;

       k = rand()%tag;

       m = rand()%tag;

        

   if(j<3)

    {

       j+=3;

    }

    s1 = CCString::createWithFormat("%d:%d%d",j,k,m);

    label = CCLabelTTF::create(s1->getCString(), "SF Digital Readout Medium.ttf", 35);

            

   this->addChild(label);

   

    label->setPosition(ccp(this->getContentSize().width*0.5, this->getContentSize().height*0.5));

    label->setColor(ccGREEN);

           

    

}

void Explore::deletsprite()

{

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

    GameSceneB::shared()->removeChildByTag(100);

    CCSprite *fail2 = CCSprite::create("14_fail-iphone4.png");

   GameSceneB::shared()->addChild(fail2,1);

    fail2->setPosition(ccp(this->getPositionX(), size.height*0.6));

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("whistle.mp3");

    Fail * fail = Fail::create("Instant_out-iphone4.png",2);

    fail->setPosition(ccp(20, size.height*0.4));

   GameSceneB::shared()->addChild(fail,4);

    

    

 }

void Explore::buttonpress()

{

    

    int n = CCUserDefault::sharedUserDefault()->getIntegerForKey("button");

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("14_clickTheBomb.mp3");

    

    score1 = CCString::createWithFormat("%d.%d%d",j,k,m);

    CCLabelTTF * t0 = CCLabelTTF::create(score1->getCString(), "AriaIMT", 20);

   this->addChild(t0,4);

    t0->setPosition(ccp(50, -50));

   

       

   this->scoreshow(score1);

    GameSceneB::shared()->addscore(score1);

   

       

   if (n==3)

    {

        CCUserDefault::sharedUserDefault()->setIntegerForKey("button", 1);

       GameSceneB::shared()->setn(1);

    

       this->scheduleOnce(schedule_selector(Explore::trans), 0.25);

       

    }

 

}

//显示玩家分数

void Explore::scoreshow(CCString*s)

{

   

   

    

   float score = s->floatValue();

   if(score>=0.6)

    {

        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("okay.mp3");

        CCSprite*t1 = CCSprite::create("00_okay-iphone4.png");

        t1->setPosition(ccp(50,-25));

       this->addChild(t1,3);

    }

   else if(score>=0.3)

    {

        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("good.mp3");

        CCSprite*t2 = CCSprite::create("00_good-iphone4.png");

        t2->setPosition(ccp(50,-25));

       this->addChild(t2,3);


    }

   else if (score>=0.2)

    {

        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("great.mp3");

        CCSprite*t3 = CCSprite::create("00_great-iphone4.png");

        t3->setPosition(ccp(50,-25));

       this->addChild(t3,3);

    }

   else

    {

        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("perfect.mp3");

        CCSprite*t = CCSprite::create("00_perfect-iphone4.png");

        t->setPosition(ccp(50,-25));

       this->addChild(t,3);

    }

}

//再次添加炸弹

void Explore::trans()

{

    

    

    int h = CCUserDefault::sharedUserDefault()->getIntegerForKey("she")+3;

    GameSceneB::shared()->getChildByTag(33)->removeFromParent();

    GameSceneB::shared()->getChildByTag(66)->removeFromParent();

    GameSceneB::shared()->getChildByTag(77)->removeFromParent();


    

    

    

    GameSceneB::shared()->reorderChild(GameSceneB::shared()->getChildByTag(200),2);

    GameSceneB::shared()->reorderChild(GameSceneB::shared()->getChildByTag(220), 2);

    GameSceneB::shared()->reorderChild(GameSceneB::shared()->getChildByTag(230), 2);

        

   if (h==4)

    {

        CCUserDefault::sharedUserDefault()->setIntegerForKey("she",10);

       GameSceneB::shared()->stopbutton(1);

        if (CCUserDefault::sharedUserDefault()->getFloatForKey("score")/9<1.0)

        {

            GameSceneB::shared()->replace(Loadingscene::creat(TargetSceneWin2Scene, 3));

        }

       else

        {

            GameSceneB::shared()->replace(Loadingscene::creat(TargetSceneScoreScene, 3));

        }

        

    }

   else

    {

       GameSceneB::shared()->addexplore(h);

    }

   

   

}

Explore::~Explore()

{

    this->unscheduleAllSelectors();

    this->removeAllChildren();

    this->removeFromParent();

}





#ifndef __HardestGameEver__GameScene2__

#define __HardestGameEver__GameScene2__

#include "cocos2d.h"

using namespace cocos2d;

class Explore;

#include <iostream>

class GameSceneB:public CCLayer

{

public:

   virtual bool init();

   static CCScene * scene();


   virtual void stopspritemove();

   

   static GameSceneB*shared();

    

  virtual void addexplore(int tag);

  virtual void refresh();

  virtual void pause();

  virtual void move();

  virtual void start(int tag);

  virtual void replace(CCScene*scene);

  virtual void stopbutton(int tag);

  virtual void addscore(CCString*s);

  virtual void setn(int k);

  virtual void seth(int k);

  void begaintouch();

  virtual void keyMenuClicked();

   virtual void keyBackClicked();

   void yes();

   void no();


   ~GameSceneB();

    

    CREATE_FUNC(GameSceneB);

private:

   CCSprite * red;

   CCSprite * yellow;

   CCSprite * blue;

   static GameSceneB * gamescene;

   int tag;

   float score;

   int n,h;



    

};


#endif



 

 

 

#include "GameScene2.h"

#include "explore.h"

#include "buttonpress.h"

#include "SimpleAudioEngine.h"

#include "Instruction.h"

#include "pause.h"

#include "setting.h"

#include "GameBegain.h"

#include "LoadingScene.h"



//初始化单例

GameSceneB*GameSceneB::gamescene = NULL;

bool GameSceneB::init()

{

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

   if (!CCLayer::init())

    {

        return false;

    }

    

    this->setKeypadEnabled(true);

    //播种子,防止产生同样的随机数

   srand(time(0));

    gamescene = this;

   tag =0;

   this->score = 0;

   this->n = 1;

   this->h = 10;

    CCUserDefault::sharedUserDefault()->setIntegerForKey("button", n);

    CCUserDefault::sharedUserDefault()->setIntegerForKey("she", h);

   

    //添加背景图片

    red = CCSprite::create("00_Rbg01-iphone4.jpeg");

   this->addChild(red,2,200);

    yellow = CCSprite::create("00_Ybg01-iphone4.jpeg");

   this->addChild(yellow,2,220);

    blue = CCSprite::create("00_Bbg01-iphone4.jpeg");

   this->addChild(blue,2,230);

   CCSize redsize = red->getContentSize();

   red->setPosition(ccp(redsize.width*0.5, size.height*0.5));

   yellow->setPosition(ccp(redsize.width*1.5, size.height*0.5));

   blue->setPosition(ccp(redsize.width*2.5, size.height*0.5));

    

   CCSprite *  sprite1 = CCSprite::create("00_Rbg02-iphone4.jpeg");

   this->addChild(sprite1,1,321);

    sprite1->setPosition(ccp(red->getPositionX(), size.height*0.5));

    

   CCSprite *  sprite2 = CCSprite::create("00_Ybg02-iphone4.jpeg");

   GameSceneB::shared()->addChild(sprite2,1,421);

    sprite2->setPosition(ccp(yellow->getPositionX(), size.height*0.5));

    

   CCSprite *  sprite3 = CCSprite::create("00_Bbg02-iphone4.jpeg");

   GameSceneB::shared()->addChild(sprite3,1,521);

    sprite3->setPosition(ccp(blue->getPositionX(), size.height*0.5));


    

    //添加stopbutton

    

    

    

    ButtonPress*redpress = ButtonPress::create("00_Rbt-iphone4.png");

   this->addChild(redpress,3,666);

    

   ButtonPress*yellowpress = ButtonPress::create("00_Ybt-iphone4.png");

   this->addChild(yellowpress,3,777);

    

    

   ButtonPress*bulepress = ButtonPress::create("00_Bbt-iphone4.png");

   this->addChild(bulepress,3,888);

    

    

    redpress->setPosition(ccp(redsize.width*0.5, redpress->getContentSize().height*0.5));

    yellowpress->setPosition(ccp(redsize.width*1.5, yellowpress->getContentSize().height*0.5));

    bulepress->setPosition(ccp(redsize.width*2.5, bulepress->getContentSize().height*0.5));

    this->stopbutton(1);

    


       

    //添加警戒线

    CCSprite * jj = CCSprite::create("14_line-iphone4.png");

   this->addChild(jj,3);

    jj->setPosition(ccp(size.width*0.5, size.height-jj->getContentSize().height*0.5));

    CCSprite * jj2 = CCSprite::create("14_line-iphone4.png");

   this->addChild(jj2,3);

    jj2->setPosition(ccp(size.width*0.5, size.height*0.2+10));

    

    

    //添加readygo

   CCSprite * ready = CCSprite::create("ready-iphone4.png");

    

   this->addChild(ready, 3,1000);

    

    ready->setPosition(ccp(size.width*0.5, size.height*0.5f));

    

    this->scheduleOnce(schedule_selector(GameSceneB::move), 0.3);

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("readyGo.mp3");

    

    

    

    

    //添加游戏场景的菜单

    CCMenuItemImage * pause = CCMenuItemImage::create("stage_button01-iphone4.png", "stage_button01-iphone4.png", this, menu_selector(GameSceneB::pause));

    pause->setPosition(ccp(size.width, size.height*0.7));

    CCMenuItemImage * refresh = CCMenuItemImage::create("stage_button02-iphone4.png", "stage_button02-iphone4.png", this, menu_selector(GameSceneB::refresh));

    refresh->setPosition(ccp(size.width, size.height*0.8));

   CCMenu * menu = CCMenu::create(pause,refresh,NULL);

    menu->setPosition(CCPointZero);

   this->addChild(menu,3);

    

   

   

           

    return true;

}

CCScene*GameSceneB::scene()

{

   CCScene * scene = CCScene::create();

   GameSceneB * layer = GameSceneB::create();

    scene->addChild(layer);

   return scene;

}

GameSceneB*GameSceneB::shared()

{

    return gamescene;

}

void GameSceneB::stopspritemove()

{

   this->start(0);

    this->stopbutton(1);

  

}

void GameSceneB::addexplore(int tag)

{

        

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

   Explore * redexplore = Explore::create("14_bomb-iphone4.png");

    redexplore->setPosition(ccp(red->getPositionX(), size.height));

   this->addChild(redexplore,3,33);

    redexplore->move(red->getPosition());

    redexplore->produce(tag);

    

    

   Explore * yellowexplore = Explore::create("14_bomb-iphone4.png");

    yellowexplore->setPosition(ccp(yellow->getPositionX(), size.height));

   this->addChild(yellowexplore,3,66);

    yellowexplore->move(yellow->getPosition());

    yellowexplore->produce(tag);

    

    

    

   Explore * blueexplore = Explore::create("14_bomb-iphone4.png");

    blueexplore->setPosition(ccp(blue->getPositionX(), size.height));

   this->addChild(blueexplore,3,77);

    blueexplore->move(blue->getPosition());

    blueexplore->produce(tag);

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("21_down.mp3");

    

    //设置button键的触摸事件

   

   ButtonPress*redpress = (ButtonPress*)this->getChildByTag(666);

    redpress->settouch();

   ButtonPress*redpress2 = (ButtonPress*)this->getChildByTag(777);

    redpress2->settouch();

   ButtonPress*redpress3 = (ButtonPress*)this->getChildByTag(888);

    redpress3->settouch();

    

    



}

void GameSceneB::move()

{

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

    this->removeChildByTag(1000);

    

   CCSprite * ready = CCSprite::create("go-iphone4.png");

    

   this->addChild(ready, 3);

    

    ready->setPosition(ccp(size.width*0.5, size.height*0.5f));

        

   CCMoveTo * action = CCMoveTo::create(0.5f, ccp(-ready->getContentSize().width*0.5, size.height*0.5));

    

    ready->runAction(action);

    

    this->addexplore(10);

    

       

    

          

}

void GameSceneB::refresh()

{

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("gen_click.mp3");

    Loadingscene * s = Loadingscene::creat(TargetSceneInstructionScene, 2);

    CCDirector::sharedDirector()->replaceScene(s);

   

}

void GameSceneB::pause()

{

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("gen_click.mp3");

    this->stopspritemove();

     CCDirector::sharedDirector()->pushScene(Pause::scene(2));

        

}

void GameSceneB::start(int tag)

{

   if(tag)

    {

        this->getChildByTag(33)->scheduleUpdate();

        this->getChildByTag(66)->scheduleUpdate();

        this->getChildByTag(77)->scheduleUpdate();

        

    }

   else

    {

        this->getChildByTag(33)->unscheduleUpdate();

        this->getChildByTag(66)->unscheduleUpdate();

        this->getChildByTag(77)->unscheduleUpdate();

    }

}

void GameSceneB::replace(CCScene*scene)

{

    CCDirector::sharedDirector()->replaceScene(scene);

    

    

}

void GameSceneB::stopbutton(int tag)

{

   ButtonPress*redpress = (ButtonPress*)this->getChildByTag(666);

   

   ButtonPress*redpress1 = (ButtonPress*)this->getChildByTag(777);

   

   ButtonPress*redpress2 = (ButtonPress*)this->getChildByTag(888);

    


   if(tag)

    {

        redpress->removetouch();

        redpress1->removetouch();

        redpress2->removetouch();

            

    }

   else

    {

        redpress->starttouch();

        redpress1->starttouch();

        redpress2->starttouch();

    }


}

//获得总分数

void GameSceneB::addscore(CCString*s)

{

   n++;

   h--;

   this->score +=s->floatValue();

    

    

    CCUserDefault::sharedUserDefault()->setFloatForKey("score", score);

    CCUserDefault::sharedUserDefault()->setIntegerForKey("button", n);

    CCUserDefault::sharedUserDefault()->setIntegerForKey("she", h);

   

    

}

void GameSceneB::setn(int k)

{

   this->n = k;

}

void GameSceneB::seth(int k)

{

   this->h = k;

}

GameSceneB::~GameSceneB()

{

    this->removeAllChildren();

    this->unscheduleAllSelectors();

}

void GameSceneB::begaintouch()

{

        

    this->stopbutton(0);

}

void GameSceneB::keyMenuClicked()

{

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("ComboMax.mp3");

    CCDirector::sharedDirector()->replaceScene(GameBegain::scene());

    

}


void GameSceneB::keyBackClicked()

{

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("ComboMax.mp3");

    

   this->start(0);

    

    

    CCLabelTTF * label = CCLabelTTF::create("确定要退出游戏么?", "MarkerFelt-Thin", 15);

   CCSprite * bac = CCSprite::create("rt4c.jpg");

   this->addChild(bac,4,782);

    bac->setPosition(ccp(160, 480+bac->getContentSize().height*0.5));

    CCMenuItemImage * yes = CCMenuItemImage::create("arcade_button02-iphone41.png", "arcade_button02-iphone41.png", this, menu_selector(GameSceneB::yes));

    yes->setPosition(ccp(bac->getContentSize().width*0.25, bac->getContentSize().height*0.5));

    

    CCMenuItemImage * no = CCMenuItemImage::create("arcade_button01-iphone4.png", "arcade_button01-iphone4.png", this, menu_selector(GameSceneB::no));

    no->setPosition(ccp(bac->getContentSize().width*0.75,bac->getContentSize().height*0.5));

   CCMenu * s = CCMenu::create(yes,no,NULL);

    s->setPosition(CCPointZero);

    bac->addChild(s);

    bac->addChild(label);

    label->setPosition(ccp(bac->getContentSize().width*0.5, bac->getContentSize().height*0.75));

   CCMoveTo * action = CCMoveTo::create(1.0f, ccp(160, 240));

    bac->runAction(action);

    

}


void GameSceneB::yes()

{

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("gen_click.mp3");

    this->removeChildByTag(782);

    

    CCDirector::sharedDirector()->end();

    

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

   exit(0);

#endif

    

}

void GameSceneB::no()

{

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("gen_click.mp3");

    this->removeChildByTag(782);

   this->start(1);

      

}


最难游戏代码续2


 

#ifndef __HardestGameEver__buttonpress__

#define __HardestGameEver__buttonpress__

#include "cocos2d.h"

using namespace cocos2d;

#include <iostream>

class ButtonPress:public CCSprite,public CCTouchDelegate

{

public:

   bool initWithFile(const char *pszFilename);

   static ButtonPress*create(const char *pszFilename);

   virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

   bool isToucheMe(CCPoint pos);

   void removetouch();

   void starttouch();

   void settouch();

    

    ~ButtonPress();

private:

   bool touch;

   

    

};



 

#include "buttonpress.h"

#include "GameScene2.h"

#include "explore.h"

bool ButtonPress::initWithFile(const char *pszFilename)

{

   if (!CCSprite::initWithFile(pszFilename))

    {

        return false;

    }

    

    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -1, false);

   touch = true;

    return true;

}

ButtonPress*ButtonPress::create(const char *pszFilename)

{

    ButtonPress*pRet = new ButtonPress();

   if (pRet&&pRet->initWithFile(pszFilename))

    {

        pRet->autorelease();

       return pRet;

    }

   else

    {

        CC_SAFE_RELEASE_NULL(pRet);

       return pRet;

    }

}

bool ButtonPress::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)

{

   CCPoint location = pTouch->getLocation();

   if (isToucheMe(location))

    {

           if(touch)

        {

             touch=false;

            

           if (location.x<this->getContentSize().width)

           {

             

            Explore * explore = (Explore *)GameSceneB::shared()->getChildByTag(33);

             explore->unscheduleUpdate();

             explore->buttonpress();

            GameSceneB::shared()->reorderChild(GameSceneB::shared()->getChildByTag(321), 2);

               

           }

           else if(location.x<this->getContentSize().width*2)

           {

           

              

             Explore * explore = (Explore *)GameSceneB::shared()->getChildByTag(66);

              explore->unscheduleUpdate();

              explore->buttonpress();

             GameSceneB::shared()->reorderChild(GameSceneB::shared()->getChildByTag(421), 2);

              

           }

           else

           {

            

             

            Explore * explore = (Explore *)GameSceneB::shared()->getChildByTag(77);

             explore->unscheduleUpdate();

             explore->buttonpress();

             GameSceneB::shared()->reorderChild(GameSceneB::shared()->getChildByTag(521), 2);

                          

           }

        }

                         

    }

     

      return isToucheMe(location);

}

bool ButtonPress::isToucheMe(CCPoint pos)

{

    

    CCRect touchrect = CCRectMake(this->getPositionX()-this->getContentSize().width*0.5, this->getPositionY()-this->getContentSize().height*0.5, this->getContentSize().width, this->getContentSize().height);

        

   return touchrect.containsPoint(pos);

    

}

void ButtonPress::removetouch()

{

    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

}

void ButtonPress::starttouch()

{

    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -1, true);

}

ButtonPress::~ButtonPress()

{

    this->removeAllChildren();

    this->removeFromParent();

}

void ButtonPress::settouch()

{

   this->touch = true;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值