Last active
October 13, 2015 18:37
-
-
Save tks2shimizu/4238817 to your computer and use it in GitHub Desktop.
card game 25 source code 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HelloWorldScene.h" | |
#include "SimpleAudioEngine.h" | |
using namespace cocos2d; | |
using namespace CocosDenshion; | |
using namespace std; | |
enum { | |
kTagTimerLabel = 100, | |
kTagHighScoreLabel = 200, | |
}; | |
CCScene* HelloWorld::scene() | |
{ | |
CCScene* scene = CCScene::create(); | |
HelloWorld* layer = HelloWorld::create(); | |
scene->addChild(layer); | |
return scene; | |
} | |
bool HelloWorld::init() | |
{ | |
if (!CCLayer::init()) | |
{ | |
return false; | |
} | |
//変数初期化 | |
nextNumber = 1; | |
gameTime = 0; | |
srand((unsigned)time(NULL)); | |
//タップイベントを取得する | |
this->setTouchEnabled(true); | |
this->setTouchMode(kCCTouchesOneByOne); | |
//ボタンを作成し配置する | |
makeButtons(); | |
//ハイスコア表示 | |
makeHighScoreLabel(); | |
//タイマー表示 | |
makeTimerLabel(); | |
//タイマー開始 | |
this->schedule(schedule_selector(HelloWorld::countTimer)); | |
return true; | |
} | |
void HelloWorld::makeButtons() | |
{ | |
//数値配列を初期化する | |
vector<int> numbers; | |
for (int i = 1; i <= 25; i++) | |
numbers.push_back(i); | |
//カードを5x5に配置する | |
for (int x = 0; x < 5; x++) | |
{ | |
for (int y = 0; y < 5; y++) | |
{ | |
//ランダムで1つの値を取得する | |
int index = rand() % numbers.size(); | |
//カードを生成する | |
CCString* fileName = CCString::createWithFormat("frontside%02d.png", numbers[index]); | |
CCSprite* button = CCSprite::create(fileName->getCString()); | |
button->setPosition(ccp((x + 0.5) * 128 + 160, (y + 0.5) * 128)); | |
button->setTag(numbers[index]); | |
this->addChild(button); | |
//数値配列から値を削除する | |
numbers.erase(numbers.begin() + index); | |
} | |
} | |
} | |
void HelloWorld::makeTimerLabel() | |
{ | |
//タイマーを表示 | |
CCSize bgSize = CCDirector::sharedDirector()->getWinSize(); | |
CCLabelTTF* timerLabel = CCLabelTTF::create("0.000s", "", 32.0f); | |
timerLabel->setPosition(ccp(bgSize.width * 0.9f, bgSize.height * 0.9f)); | |
timerLabel->setTag(kTagTimerLabel); | |
this->addChild(timerLabel); | |
} | |
void HelloWorld::makeHighScoreLabel() | |
{ | |
//以前のハイスコアを取得 | |
float highScore = CCUserDefault::sharedUserDefault()->getFloatForKey("highscore", 99.999f); | |
//以前のハイスコアを表示 | |
CCSize bgSize = CCDirector::sharedDirector()->getWinSize(); | |
CCString* highScoreString = CCString::createWithFormat("%8.3fs", highScore); | |
CCLabelTTF* highScoreLabel = CCLabelTTF::create(highScoreString->getCString(), "", 32.0f); | |
highScoreLabel->setPosition(ccp(bgSize.width * 0.9f, bgSize.height * 0.1f)); | |
highScoreLabel->setTag(kTagHighScoreLabel); | |
this->addChild(highScoreLabel); | |
} | |
void HelloWorld::countTimer(float time) | |
{ | |
gameTime += time; | |
//時間を表示する | |
CCString* timeString = CCString::createWithFormat("%8.3fs", gameTime); | |
CCLabelTTF* timerLabel = (CCLabelTTF*)this->getChildByTag(kTagTimerLabel); | |
timerLabel->setString(timeString->getCString()); | |
} | |
void HelloWorld::afterGame() | |
{ | |
//タイマー停止 | |
this->unschedule(schedule_selector(HelloWorld::countTimer)); | |
//以前のハイスコアを取得 | |
float highScore = CCUserDefault::sharedUserDefault()->getFloatForKey("highscore", 99.999f); | |
if (highScore > gameTime) { | |
//ハイスコアを記録 | |
CCUserDefault::sharedUserDefault()->setFloatForKey("highscore", gameTime); | |
CCUserDefault::sharedUserDefault()->flush(); | |
//ハイスコアを表示 | |
CCString* highScoreString = CCString::createWithFormat("%8.3fs", gameTime); | |
CCLabelTTF* highScoreLabel = (CCLabelTTF*)this->getChildByTag(kTagHighScoreLabel); | |
highScoreLabel->setString(highScoreString->getCString()); | |
} | |
//リトライボタンを表示 | |
CCSize bgSize = CCDirector::sharedDirector()->getWinSize(); | |
CCMenuItemFont* retryLabel = CCMenuItemFont::create("Retry", this, menu_selector(HelloWorld::menuRetryButton)); | |
retryLabel->setPosition(ccp(bgSize.width * 0.9f, bgSize.height * 0.2f)); | |
CCMenu* menu = CCMenu::create(retryLabel, NULL); | |
menu->setPosition(CCPointZero); | |
this->addChild(menu); | |
} | |
void HelloWorld::menuRetryButton(CCNode *node) | |
{ | |
CCDirector::sharedDirector()->getTouchDispatcher()->removeAllDelegates(); | |
CCScene* gameScene = (CCScene*)HelloWorld::create(); | |
CCDirector::sharedDirector()->replaceScene(gameScene); | |
} | |
#pragma mark - タップイベント | |
bool HelloWorld::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent) | |
{ | |
return true; | |
} | |
void HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent) | |
{ | |
//タップポイント取得 | |
CCPoint touchPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView()); | |
CCNode* node = this->getChildByTag(nextNumber); | |
if (node->boundingBox().containsPoint(touchPoint)) { | |
//裏カードを作成する | |
CCSprite* button = CCSprite::create("backside.png"); | |
button->setPosition(node->getPosition()); | |
button->setTag(nextNumber); | |
this->addChild(button); | |
//表カードを削除する | |
node->removeFromParentAndCleanup(true); | |
if (nextNumber >= 25) { | |
//ゲーム終了処理 | |
afterGame(); | |
return; | |
} | |
nextNumber++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HELLOWORLD_SCENE_H__ | |
#define __HELLOWORLD_SCENE_H__ | |
#include "cocos2d.h" | |
class HelloWorld : public cocos2d::CCLayer | |
{ | |
private: | |
int nextNumber; | |
float gameTime; | |
void makeButtons(); | |
void makeTimerLabel(); | |
void makeHighScoreLabel(); | |
void countTimer(float time); | |
void afterGame(); | |
virtual void menuRetryButton(CCNode *node); | |
public: | |
virtual bool init(); | |
static cocos2d::CCScene* scene(); | |
CREATE_FUNC(HelloWorld); | |
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); | |
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); | |
}; | |
#endif // __HELLOWORLD_SCENE_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment