<토이프로젝트>/[C++ SFML 게임]

[C++ SFML 게임] 두들 점프 게임

BlockDMask 2020. 9. 1. 00:11
반응형

안녕하세요. BlockDMask 입니다.

오늘은 C++ SFML을 이용해서 만든 두들 점프 게임을 소개 해보겠습니다.


<목차>

1. 게임 영상

2. 소스 코드와 깃허브 주소

1. 게임 영상


▶ 두들 점프 게임 영상 주소  : https://youtu.be/2fJ1_gTLDRE



2. 소스 코드


▶ 소스코드

글 하단에 깃허브 주소가 있습니다. 

해당 주소에 이미지가 존재하니 다운받으셔서 실습해보시길 추천 드립니다.

단, 사용하실때 출처 남겨주세요. 소스코드도 제가 직접 작성한것이고, 그림도 제가 직접 그렸습니다. 


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
// [C++ SFML] doodle jump
// by.BlockDMask
// Blog : https://blockdmask.tistory.com/373
// Youtube : https://youtu.be/2fJ1_gTLDRE
 
#include<SFML/Graphics.hpp>
#include<ctime>
#include<vector>
using namespace sf;
using namespace std;
 
#define WIDTH 400        //가로
#define HEIGHT 600        //세로
#define BAR_COUNT 10    //밟는 bar 개수
static const float GRAVITY = 0.2f;    //중력
 
//플래이어 클래스
class Player
{
private:
    int x;
    int y;
    int imgWidth;
    int imgHeight;
    float dy;
    Sprite* imgJump;
    Sprite* imgReady;
    Texture t1;
    Texture t2;
    bool jumpFlag;
private:
    const Sprite& GetImg()
    {
        if (jumpFlag)
        {
            return *imgJump;
        }
        else
        {
            return *imgReady;
        }
    }
 
public:
    Player() : dy(0), jumpFlag(true)
    {
        x = static_cast<int>(WIDTH / 2);
        y = static_cast<int>(HEIGHT / 2);
 
        t1.loadFromFile("images/p1.png");
        t2.loadFromFile("images/p2.png");
 
        imgJump = new Sprite(t1);
        imgReady = new Sprite(t2);
 
        imgWidth = static_cast<int>(imgReady->getTexture()->getSize().x);
        imgHeight = static_cast<int>(imgReady->getTexture()->getSize().y);
    }
    ~Player()
    {
        delete(imgJump);
        delete(imgReady);
    }
 
    void SetPosition()
    {
        imgReady->setPosition(x, y);
        imgJump->setPosition(x, y);
    }
    void Move()
    {
        if (Keyboard::isKeyPressed(Keyboard::Right)) //오른쪽이동
        {
            x += 4;
        }
        if (Keyboard::isKeyPressed(Keyboard::Left)) //왼쪽이동
        {
            x -= 4;
        }
        if (x <= 0)    //왼쪽 벽 뚫지 못하게
        {
            x = 0;
        }
        if (x >= WIDTH - imgWidth)    //오른쪽 벽 뚫지 못하게
        {
            x = WIDTH - imgWidth;
        }
 
        jumpFlag = true;
        dy += GRAVITY;
        y += static_cast<int>(dy);
        
        if (y > HEIGHT - imgHeight)
        {
            jumpFlag = false;
            dy = -10;
        }
 
    }
    void Draw(RenderWindow& window)
    {
        window.draw(GetImg());
    }
 
    float GetDy() const
    {
        return dy;
    }
    int GetY() const
    {
        return y;
    }
    int GetX() const
    {
        return x;
    }
    int GetWidth() const
    {
        return imgWidth;
    }
    int GetHeight() const
    {
        return imgHeight;
    }
    void SetY(int _y)
    {
        y = _y;
    }
    void Jump()
    {
        jumpFlag = false;
        dy = -10;
    }
};
cs


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
//점프 bar 클래스
class Bar
{
private:
    struct Pos
    {
        int x;
        int y;
    };
    vector<Pos> vBar;
    Sprite* imgBar;
    Texture t;
    int imgWidth;
public:
    Bar()
    {
        srand(static_cast<unsigned int>(time(nullptr)));
 
        t.loadFromFile("images/bar.png");
        imgBar = new Sprite(t);
 
        imgWidth = imgBar->getTexture()->getSize().x;
 
        for (int i = 0; i < BAR_COUNT; ++i)
        {
            Pos p;
            p.x = rand() % WIDTH - imgWidth / 2;
            p.y = rand() % HEIGHT;
            vBar.push_back(p);
        }
 
        vBar[0].y = HEIGHT - 200;
    }
    ~Bar()
    {
        delete(imgBar);
    }
    
    void Draw(RenderWindow& window)
    {
        for (int i = 0; i < BAR_COUNT; ++i)
        {
            imgBar->setPosition(vBar[i].x, vBar[i].y);
            window.draw(*imgBar);
        }
    }
    bool CheckCollision(Player* pPlayer)
    {
        //null check.
        if (pPlayer == nullptr)
        {
            return false;
        }
 
        for (int i = 0; i < BAR_COUNT; ++i)
        {
            if (pPlayer->GetDy() > 0
                && pPlayer->GetX() + pPlayer->GetWidth() > vBar[i].x
                && pPlayer->GetX() < vBar[i].x + imgWidth
                && pPlayer->GetY() + pPlayer->GetHeight() > vBar[i].y
                && pPlayer->GetY() + pPlayer->GetHeight() < vBar[i].y + 10)
            {
                pPlayer->Jump();
                return true;
            }
        }
        return false;
    }
    void MoveAndReset(Player* pPlayer)
    {
        static const int limit = HEIGHT / 3;
        if (pPlayer->GetY() < limit)
        {
            for (int i = 0; i < BAR_COUNT; ++i)
            {
                pPlayer->SetY(limit);
                vBar[i].y -= static_cast<int>(pPlayer->GetDy());
                if (vBar[i].y > HEIGHT + 10)
                {
                    vBar[i].y = rand() % HEIGHT / 3 + 100;
                    vBar[i].x = rand() % WIDTH;
                }
            }
        }
    }
 
};
 
int main(void)
{
    RenderWindow window(VideoMode(WIDTH, HEIGHT), "Doodle Game by.BlockDMask");
    window.setFramerateLimit(60);
 
    //setting
    Player* pPlayer = new Player();
    Bar* pBar = new Bar();
 
    while (window.isOpen())
    {
        Event e;
        if (window.pollEvent(e))
        {
            if (e.type == Event::Closed)
            {
                window.close();
            }
        }
        //logic
        pPlayer->Move();
        pBar->MoveAndReset(pPlayer);
        pBar->CheckCollision(pPlayer);
        pPlayer->SetPosition();
 
        //draw
        window.clear(Color::White);
        pPlayer->Draw(window);
        pBar->Draw(window);
        window.display();
    }
 
    delete(pBar);
    delete(pPlayer);
    return 0;
}
 
cs



깃 주소 : https://github.com/BlockDMask/SFML_DoodleJump_Game

감사합니다.

이상으로 C++ 게임 두들 점프 소개를 마치겠습니다.

반응형