본문 바로가기

카테고리 없음

[cocos2d-x]기억력 카드게임 - 게임시작 부분

GameScene.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
 
#include "GameScene.h"
 
Scene* GameScene::createScene()
{
    auto scene = Scene::create();
 
    auto layer = GameScene::create();
    scene->addChild(layer);
 
    return scene;
}
 
bool GameScene::init()
{
    if (!Layer::init())
    {
        return false;
    }
 
    initGameData();
 
    initBG();
    initTopMenuLabel();
 
    initCard();
 
    initReady();
    initStart();
 
    actionReady();
 
    return true;
}
 
void GameScene::initGameData()
{
    winSize = Director::getInstance()->getWinSize();
 
    cardOK = 0;
    life = 3;
 
    srand(time(NULL));
 
    for (int i = 0; i<4; i++) {
        card[i] = rand() % 6;
    }
 
    countCard = 0;
}
 
void GameScene::initBG()
{
    auto spr = CCSprite::create("game/game-bg.png");
    spr->setPosition(Point(winSize.width / 2, winSize.height / 2));
    this->addChild(spr);
}
 
void GameScene::initTopMenuLabel()
{
    auto label_0 = Label::createWithSystemFont(""""20);
    label_0->setAnchorPoint(Point(01));
    label_0->setPosition(Point(10, winSize.height - 10));
    label_0->setTag(TAG_LABEL_CARD);
    label_0->setColor(Color3B::BLACK);
    this->addChild(label_0);
 
    auto label_1 = Label::createWithSystemFont(""""20);
    label_1->setAnchorPoint(Point(11));
    label_1->setPosition(Point(winSize.width - 10, winSize.height - 10));
    label_1->setTag(TAG_LABEL_LIFE);
    label_1->setColor(Color3B::BLACK);
    this->addChild(label_1);
 
    setLabelCard();
    setLabelLife();
}
 
void GameScene::setLabelCard()
{
    auto label = (Label*)this->getChildByTag(TAG_LABEL_CARD);
    label->setString(StringUtils::format("CARD : %d/4", cardOK));
}
 
void GameScene::setLabelLife()
{
    auto label = (Label*)this->getChildByTag(TAG_LABEL_LIFE);
    label->setString(StringUtils::format("LIFE : %d", life));
}
 
void GameScene::initCard()
{
    for (int i = 0; i<6; i++) {
        auto spr = Sprite::create("game/card-back.png");
        spr->setPosition(Point(winSize.width / 2 - 60 + 60 * 2 * (i % 2), winSize.height / 2 + 120 - 120 * (i / 2)));
        this->addChild(spr);
 
        auto sprFront = Sprite::create(String::createWithFormat("game/card_%d.png", (i + 1))->getCString());
        sprFront->setPosition(Point(winSize.width / 2 - 60 + 60 * 2 * (i % 2), winSize.height / 2 + 120 - 120 * (i / 2)));
        sprFront->setVisible(false);
        sprFront->setTag(TAG_SPRITE_CARD + i);
        this->addChild(sprFront);
    }
}
 
void GameScene::initReady() //Ready문구
{
    auto label = Label::createWithSystemFont("READY"""80);
    label->setPosition(Point(winSize.width / 2, winSize.height / 2));
    label->setTag(TAG_LABEL_READY);
    label->setColor(Color3B::BLACK);
    label->setScale(0.0);
    this->addChild(label);
}
 
void GameScene::initStart() //Start문구
{
    auto label = Label::createWithSystemFont("START"""80);
    label->setPosition(Point(winSize.width / 2, winSize.height / 2));
    label->setTag(TAG_LABEL_START);
    label->setColor(Color3B::BLACK);
    label->setScale(0.0);
    this->addChild(label);
}
 
void GameScene::actionReady()
{
    auto action = Sequence::create(
        ScaleTo::create(1.01.0),
        DelayTime::create(2.0),
        ScaleTo::create(1.00.0),
        CallFunc::create(CC_CALLBACK_0(GameScene::endReady, this)),
        NULL);
 
    auto label = (Label*)this->getChildByTag(TAG_LABEL_READY);
    label->runAction(action);
}
 
void GameScene::endReady()
{
    actionStart();
}
 
void GameScene::actionStart()
{
    auto action = Sequence::create(
        ScaleTo::create(1.01.0),
        DelayTime::create(2.0),
        ScaleTo::create(1.00.0),
        CallFunc::create(CC_CALLBACK_0(GameScene::endStart, this)),
        NULL);
 
    auto label = (Label*)this->getChildByTag(TAG_LABEL_START);
    label->runAction(action);
}
 
void GameScene::endStart()
{
    //    actionCard();
}
 
cs


GameScene.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__
 
#include "cocos2d.h"
 
USING_NS_CC;
 
#define TAG_LABEL_CARD          0
#define TAG_LABEL_LIFE          1
#define TAG_LABEL_READY         2
#define TAG_LABEL_START         3
#define TAG_SPRITE_O            4
#define TAG_SPRITE_X            5
#define TAG_LABEL_GAMEOVER      6
#define TAG_LABEL_GAMECLEAR     7
#define TAG_MENU                8
 
#define TAG_SPRITE_CARD         10
 
class GameScene : public Layer
{
public:
 
    static Scene* createScene();
 
    virtual bool init();
    CREATE_FUNC(GameScene);
 
    Size winSize;
 
    int cardOK, life;
    int card[4];
    int countCard;
 
    void initGameData();
 
    void initBG();
    void initTopMenuLabel();
 
    void setLabelCard();
    void setLabelLife();
 
    void initCard();
 
    void initReady();
    void initStart();
 
    void actionReady();
    void endReady();
 
    void actionStart();
    void endStart();
};
 
#endif
cs



실행결과


레디가 커졌다 작아지면서 사라지고 스타트도 커졌다 작아지면서 사라짐

필수적인 요소는 아니지만 부가적인 효과로 응용하기 좋을 것 같다.