본문 바로가기

카테고리 없음

[cocos2d-x]기억력 카드게임 - 카드 보여주기,터치,카드 선택

GameScene.cpp

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#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;
    }
 
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
 //터치이벤트 생성
    isTouch = false;
 
    initGameData();
 
    initBG();
    initTopMenuLabel();
 
    initCard();
 
    initReady();
    initStart();
 
    initO();
    initX();
 
    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()
{
    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()
{
    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();
}
 
void GameScene::actionCard()
{
    for (int i = 0; i<4; i++) { //보여주는 카드가 4장이므로 for문을 사용
        auto action = Sequence::create(
            DelayTime::create(3.0*i), //보여주는시간
            Show::create(),
            DelayTime::create(2.0), //가려지는 시간
            Hide::create(),
            NULL);
 
        auto spr = (Sprite *)this->getChildByTag(TAG_SPRITE_CARD + card[i]);
        spr->runAction(action);
    }
 
    auto action = Sequence::create(
        DelayTime::create(11.0),
        CallFunc::create(CC_CALLBACK_0(GameScene::endCard, this)),
        NULL);
    this->runAction(action);
}
 
void GameScene::endCard()
{
    isTouch = true;
}
 
void GameScene::initO()
{
    auto spr = Sprite::create("game/o.png");
    spr->setPosition(Point(winSize.width / 2, winSize.height / 2));
    spr->setTag(TAG_SPRITE_O);
    spr->setVisible(false);
    this->addChild(spr);
}
 
void GameScene::initX()
{
    auto spr = Sprite::create("game/x.png");
    spr->setPosition(Point(winSize.width / 2, winSize.height / 2));
    spr->setTag(TAG_SPRITE_X);
    spr->setVisible(false);
    this->addChild(spr);
}
 
void GameScene::actionOX(bool isO)
{
    auto action = Sequence::create(
        Show::create(),
        DelayTime::create(0.5),
        Hide::create(),
        CallFunc::create(CC_CALLBACK_0(GameScene::endOX, this)),
        NULL);
 
    Sprite *spr;
 
    if (isO) {
        spr = (Sprite*)this->getChildByTag(TAG_SPRITE_O);
    }
    else {
        spr = (Sprite*)this->getChildByTag(TAG_SPRITE_X);
    }
 
    spr->runAction(action);
}
 
void GameScene::endOX()
{
    if (!(cardOK == 4 || life == 0)) {
        isTouch = true;
    }
}
 
bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)
{
    if (!isTouch) return true;
 //isTouch 값이 false이면 리턴
    Point location = touch->getLocation();
 //터치한 영역의 좌표 가져오기
    for (int i = 0; i<6; i++) {
        auto spr = (Sprite*)this->getChildByTag(TAG_SPRITE_CARD + i);
        Rect sprRect = spr->getBoundingBox();
 
        if (sprRect.containsPoint(location)) {
 //카드 영역을 터치하였는지 유무 확인
            isTouch = false;
 //카드 영역을 터치하였을 경우 중복을 막기 위해 false로 변경
            auto action = Sequence::create(
                Show::create(),
                DelayTime::create(0.5),
                Hide::create(),
                NULL);
            spr->runAction(action);
 
            if (i == card[countCard]) {
 
                cardOK++;
                countCard++;
 
                setLabelCard();
                actionOX(true);
            }
            else {
 
                life--;
 
                setLabelLife();
                actionOX(false);
            }
        }
    }
 
    return true;
}
 
 
cs


실행 결과




귀차니즘의 관계로... 실제로 실행하면 랜덤으로 6개중 4개의 카드가 순서대로 보여집니다.

보여진 이후에 같은 순서대로 클릭하면 되고 틀릴경우에는 LIFE값이 줄어들게 됩니다.

모두 다 맞추고나서 이후부분은 아직 구현되지 않았습니다.