Last active
October 13, 2015 18:38
-
-
Save tks2shimizu/4239350 to your computer and use it in GitHub Desktop.
card game 25 source code 3
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(); | |
//タイマー表示 | |
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::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)); | |
} | |
#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 countTimer(float time); | |
void afterGame(); | |
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