forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneLoneCoder_FlappyBird.cpp
194 lines (160 loc) · 5.05 KB
/
OneLoneCoder_FlappyBird.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
/*
OneLoneCoder.com - Code-It-Yourself! Flappy Bird
"You asked for it... wait, no one did, ever..." - @Javidx9
License
~~~~~~~
Copyright (C) 2018 Javidx9
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://www.github.com/onelonecoder
https://www.onelonecoder.com
https://www.youtube.com/javidx9
GNU GPLv3
https://github.com/OneLoneCoder/videos/blob/master/LICENSE
From Javidx9 :)
~~~~~~~~~~~~~~~
Hello! Ultimately I don't care what you use this for. It's intended to be
educational, and perhaps to the oddly minded - a little bit of fun.
Please hack this, change it and use it in any way you see fit. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
Cheers!
Background
~~~~~~~~~~
Look it's Flappy Bird, OK? Press Space Bar to Flap. Get a high score.
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
Video:
~~~~~~
https://youtu.be/b6A4XHkTjs8
Last Updated: 05/11/2017
*/
#include <iostream>
#include <string>
using namespace std;
#include "olcConsoleGameEngine.h"
class OneLoneCoder_FlappyBird : public olcConsoleGameEngine
{
public:
OneLoneCoder_FlappyBird()
{
m_sAppName = L"Flappy Bird";
}
private:
float fBirdPosition = 0.0f;
float fBirdVelocity = 0.0f;
float fBirdAcceleration = 0.0f;
float fGravity = 100.0f;
float fLevelPosition = 0.0f;
float fSectionWidth;
list<int> listSection;
bool bHasCollided = false;
bool bResetGame = false;
int nAttemptCount = 0;
int nFlapCount = 0;
int nMaxFlapCount = 0;
protected:
// Called by olcConsoleGameEngine
virtual bool OnUserCreate()
{
listSection = { 0, 0, 0, 0 };
bResetGame = true;
fSectionWidth = (float)ScreenWidth() / (float)(listSection.size() - 1);
return true;
}
// Called by olcConsoleGameEngine
virtual bool OnUserUpdate(float fElapsedTime)
{
if (bResetGame)
{
bHasCollided = false;
bResetGame = false;
listSection = { 0, 0, 0, 0 };
fBirdAcceleration = 0.0f;
fBirdVelocity = 0.0f;
fBirdPosition = ScreenHeight() / 2.0f;
nFlapCount = 0;
nAttemptCount++;
}
// Game
if (bHasCollided)
{
// Do nothing until user releases space
if (m_keys[VK_SPACE].bReleased)
bResetGame = true;
}
else
{
if (m_keys[VK_SPACE].bPressed && fBirdVelocity >= fGravity / 10.0f)
{
fBirdAcceleration = 0.0f;
fBirdVelocity = -fGravity / 4.0f;
nFlapCount++;
if (nFlapCount > nMaxFlapCount)
nMaxFlapCount = nFlapCount;
}
else
fBirdAcceleration += fGravity * fElapsedTime;
if (fBirdAcceleration >= fGravity)
fBirdAcceleration = fGravity;
fBirdVelocity += fBirdAcceleration * fElapsedTime;
fBirdPosition += fBirdVelocity * fElapsedTime;
fLevelPosition += 14.0f * fElapsedTime;
if (fLevelPosition > fSectionWidth)
{
fLevelPosition -= fSectionWidth;
listSection.pop_front();
int i = rand() % (ScreenHeight() - 20);
if (i <= 10) i = 0;
listSection.push_back(i);
}
// Display
Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');
// Draw Sections
int nSection = 0;
for (auto s : listSection)
{
if (s != 0)
{
Fill(nSection * fSectionWidth + 10 - fLevelPosition, ScreenHeight() - s, nSection * fSectionWidth + 15 - fLevelPosition, ScreenHeight(), PIXEL_SOLID, FG_GREEN);
Fill(nSection * fSectionWidth + 10 - fLevelPosition, 0, nSection * fSectionWidth + 15 - fLevelPosition, ScreenHeight() - s - 15, PIXEL_SOLID, FG_GREEN);
}
nSection++;
}
int nBirdX = (int)(ScreenWidth() / 3.0f);
// Collision Detection
bHasCollided = fBirdPosition < 2 || fBirdPosition > ScreenHeight() - 2 ||
m_bufScreen[(int)(fBirdPosition + 0) * ScreenWidth() + nBirdX].Char.UnicodeChar != L' ' ||
m_bufScreen[(int)(fBirdPosition + 1) * ScreenWidth() + nBirdX].Char.UnicodeChar != L' ' ||
m_bufScreen[(int)(fBirdPosition + 0) * ScreenWidth() + nBirdX + 6].Char.UnicodeChar != L' ' ||
m_bufScreen[(int)(fBirdPosition + 1) * ScreenWidth() + nBirdX + 6].Char.UnicodeChar != L' ';
// Draw Bird
if (fBirdVelocity > 0)
{
DrawString(nBirdX, fBirdPosition + 0, L"\\\\\\");
DrawString(nBirdX, fBirdPosition + 1, L"<\\\\\\=Q");
}
else
{
DrawString(nBirdX, fBirdPosition + 0, L"<///=Q");
DrawString(nBirdX, fBirdPosition + 1, L"///");
}
DrawString(1, 1, L"Attempt: " + to_wstring(nAttemptCount) + L" Score: " + to_wstring(nFlapCount) + L" High Score: " + to_wstring(nMaxFlapCount));
}
return true;
}
};
int main()
{
// Use olcConsoleGameEngine derived app
OneLoneCoder_FlappyBird game;
game.ConstructConsole(80, 48, 16, 16);
game.Start();
return 0;
}