-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlappyBird.cpp
More file actions
182 lines (135 loc) · 3.69 KB
/
FlappyBird.cpp
File metadata and controls
182 lines (135 loc) · 3.69 KB
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
#define CONSOLE_GAME_ENGINE_IMPLEMENTATION
#include "ConsoleGameEngine.hpp"
#include <list>
using namespace std;
class FlappyBird : public ConsoleGameEngine
{
public:
FlappyBird()
{
sAppName = L"Flappy Bird";
}
float fBirdPosition;
float fBirdVelocity;
float fBirdAcceleration;
float fSectionWidth;
list<int> listSections;
int nScore;
int nBestScore;
float fLevelPosition;
bool bGameOver;
const float fGravity = 100.0f;
private:
void Reset()
{
srand(time(0));
fBirdPosition = (float)ScreenHeight() * 0.5f;
fBirdVelocity = 0.0f;
fBirdAcceleration = 0.0f;
listSections = { 0, 0, 0, 0 };
fSectionWidth = (float)ScreenWidth() / float(listSections.size() - 1);
nScore = 0;
fLevelPosition = 0.0f;
bGameOver = false;
}
bool OnUserCreate() override
{
Reset();
nBestScore = 0;
return true;
}
bool RectVsRect(float r1_x1, float r1_y1, float r1_x2, float r1_y2, float r2_x1, float r2_y1, float r2_x2, float r2_y2)
{
return r1_x1 <= r2_x2 && r1_y1 <= r2_y2 && r1_x2 >= r2_x1 && r1_y2 >= r2_y1;
}
bool OnUserUpdate(float fDeltaTime) override
{
if (bGameOver)
{
if (GetKey(VK_RETURN).bPressed)
Reset();
return true;
}
if (GetKey(VK_SPACE).bPressed && fBirdVelocity >= fGravity * 0.1f)
{
fBirdAcceleration = 0.0f;
fBirdVelocity = -fGravity * 0.25f;
}
else
fBirdAcceleration += fGravity * fDeltaTime;
if (fBirdAcceleration >= fGravity)
fBirdAcceleration = fGravity;
fBirdVelocity += fBirdAcceleration * fDeltaTime;
fBirdPosition += fBirdVelocity * fDeltaTime;
fLevelPosition += 14.0f * fDeltaTime;
if (fLevelPosition > fSectionWidth)
{
fLevelPosition -= fSectionWidth;
if (listSections.front() != 0)
nScore++;
listSections.pop_front();
int nSize = rand() % int((float)ScreenHeight() * 0.7f);
if (nSize <= int((float)ScreenHeight() * 0.17f)) nSize = 0;
listSections.push_back(nSize);
}
list<pair<pair<int, int>, pair<int, int>>> listObstacles;
int nSection = 0;
for (int s : listSections)
{
if (s != 0)
{
float fOffset = (float)nSection * fSectionWidth - fLevelPosition;
float fPos1 = fOffset + (float)ScreenWidth() * 0.125f;
float fPos2 = fOffset + (float)ScreenWidth() * 0.1875f;
listObstacles.push_back({
{ (int)fPos1, ScreenHeight() - s }, { (int)fPos2, ScreenHeight() }
});
listObstacles.push_back({
{ (int)fPos1, 0 }, { (int)fPos2, ScreenHeight() - s - ScreenHeight() / 4 }
});
}
nSection++;
}
Clear(PIXEL_SOLID, FG_BLACK);
int nBirdX = ScreenWidth() / 3;
int nBirdWidth = 5;
int nBirdHeight = 5;
if (fBirdVelocity > 0.0f)
{
DrawString(nBirdX, (int)fBirdPosition + 0, LR"(\\\)", FG_WHITE);
DrawString(nBirdX, (int)fBirdPosition + 1, LR"(<\\\=&)", FG_WHITE);
}
else
{
DrawString(nBirdX, (int)fBirdPosition + 0, L"<///=&", FG_WHITE);
DrawString(nBirdX, (int)fBirdPosition + 1, L"///", FG_WHITE);
}
for (const auto& o : listObstacles)
{
if (RectVsRect(
(float)nBirdX, fBirdPosition, (float)nBirdX + 6, fBirdPosition + 2,
o.first.first, o.first.second, o.second.first, o.second.second))
{
if (nBestScore < nScore)
nBestScore = nScore;
bGameOver = true;
}
FillRectangle(
o.first.first, o.first.second,
o.second.first - o.first.first, o.second.second - o.first.second,
PIXEL_SOLID, FG_GREEN);
}
DrawString(1, 1, L"Score: " + to_wstring(nScore), FG_WHITE);
DrawString(1, 2, L"Best: " + to_wstring(nBestScore), FG_WHITE);
if (bGameOver)
DrawString(int((float)ScreenWidth() * 0.4f), ScreenHeight() / 2, L"PRESS ENTER TO PLAY AGAIN", FG_WHITE);
return true;
}
};
int main()
{
FlappyBird app;
if (app.ConstructConsole(80, 60, 16, 16) == RC_OK)
app.Run();
return 0;
}