-
Notifications
You must be signed in to change notification settings - Fork 1
/
flappybird.cpp
436 lines (373 loc) · 10.3 KB
/
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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <kosSyst.h>
#include <kosFile.h>
#include "images.hpp"
//Global const strings
const char HEADER_STRING[] = "Flappy bird";
const char CONTROL_STRING[] = "SPACEBAR TO JUMP";
const char GAMEOVER_STRING[] = "GAMEOVER";
const char ANY_KEY_STRING[] = "Press any key for restart";
const char SELECT_SPEED_STRING[] = "select the speed of the game";
const char FAST_STRING[] = "1 FAST";
const char SLOW_STRING[] = "2 SLOW";
//Global const variables
const int WINDOW_WIDTH = 400;
const int WINDOW_HEIGHT = 400;
const int BORDER_TOP = 24;
const int BORDER_LEFT = 5;
const int BORDER_RIGHT = 5;
const int BORDER_DOWN = 5;
enum GameState
{
GAMESTATE_MENU,
GAMESTATE_STARTED,
GAMESTATE_GAMEOVER
};
struct ScreenSize
{
int width;
int height;
};
class Bird
{
public:
static const int sizeX = 19;
static const int sizeY = 20;
static const int x = 100;
int prev_y;
int y;
int acceleration;
inline void initialize()
{
y = WINDOW_HEIGHT / 2;
acceleration = 0;
}
inline void move()
{
if (acceleration <= 30)
acceleration += 2;
prev_y = y;
y += acceleration / 10;
}
inline void jump()
{
acceleration = -50;
}
inline void draw()
{
kos_PutImage(birdImage, sizeX, sizeY, x, y);
}
};
class Tube
{
public:
static const int width = 50;
static const int gapHeight = 100;
static const int headHeight = 18;
int x;
int gapY;
inline void randomize()
{
x = WINDOW_WIDTH + 1;
gapY = rtlRand() % 200 + 50;
}
inline void move()
{
x -= 2;
if (x < -width - 2)
randomize();
}
void draw()
{
//cleanup
int pixels = (WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1)) - (x + width + 2);
if (pixels >= -1)
{
pixels = (pixels == -1) ? 1 : 2;
kos_DrawBar(x + width, gapY - headHeight, pixels, headHeight, 0x00FFFF);
kos_DrawBar(x + width, gapY + gapHeight, pixels, headHeight, 0x00FFFF);
}
int offset = x >= 0 ? 0 : -x;
int trim = x + width >= WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1) ? WINDOW_WIDTH - x - width - (BORDER_LEFT + BORDER_RIGHT - 1) : 0;
int trimHead = x + width >= WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1) ? WINDOW_WIDTH - x - width - (BORDER_LEFT + BORDER_RIGHT - 1) : 0;
//top
for (int y = 0; y < gapY - headHeight; ++y)
kos_PutImage(tubeBodyImage + offset, width - offset + trim, 1, x + offset, y);
//head top
for (int y = gapY - headHeight; y < gapY; ++y)
kos_PutImage(tubeHeadImage + width * (y - (gapY - headHeight)) + offset, width - offset + trimHead, 1, x + offset, y);
//head down
for (int y = gapY + gapHeight; y < gapY + gapHeight + headHeight; ++y)
kos_PutImage(tubeHeadImage + width * (y - (gapY + gapHeight)) + offset, width - offset + trimHead, 1, x + offset, y);
//down
for (int y = gapY + gapHeight + headHeight; y < WINDOW_HEIGHT - (BORDER_TOP + BORDER_DOWN - 1); ++y)
kos_PutImage(tubeBodyImage + offset, width - offset + trim, 1, x + offset, y);
}
};
//Global variables
int loopDelay;
GameState gameState;
char scoreString[] = "Score: ";
bool scoreChanged;
int score;
Bird bird;
int tubeNumber;
Tube tubes[3];
int windowX;
int windowY;
//Function prototypes
void kos_Main();
void startGame();
ScreenSize getScreenSize();
void updateScoreString();
void WriteBorderedText(Word x, Word y, Byte fontType, Dword textColor, const char* textPtr, Dword textLen, Dword borderColor, int borderSize);
inline bool checkAddScore(Tube tube);
inline bool checkCollision(Tube tube);
void drawMenuWindow();
void drawGameWindow();
void redrawGameWindow();
void drawGameoverWindow();
//Functions
void startGame()
{
kos_SetMaskForEvents(0x7); /// 111 in binary
bird.initialize();
score = 0;
memset((Byte*)scoreString + 6, ' ', 3);
updateScoreString();
tubeNumber = 1;
tubes[0].randomize();
gameState = GAMESTATE_STARTED;
drawGameWindow();
}
ScreenSize getScreenSize()
{
Dword result;
__asm {
push 14 //System function 14
pop eax
int 0x40
mov result, eax
}
ScreenSize screenSize;
screenSize.height = (result & 0xFFFF) + 1; //last two bytes
screenSize.width = (result >> 16) + 1; //first two bytes
return screenSize;
}
void kos_Main()
{
rtlSrand( kos_GetSystemClock() );
//Centring window
ScreenSize screenSize = getScreenSize();
windowX = (screenSize.width - WINDOW_WIDTH) / 2;
windowY = (screenSize.height - WINDOW_HEIGHT) / 2;
gameState = GAMESTATE_MENU;
kos_SetMaskForEvents(0x27); // 100111 in binary
while( true )
{
switch (gameState)
{
case GAMESTATE_STARTED:
kos_Pause(loopDelay);
bird.move();
//Adding new tubes
if ((tubeNumber == 1 || tubeNumber == 2) && (tubes[tubeNumber - 1].x < (WINDOW_WIDTH - WINDOW_WIDTH / 3)))
tubes[tubeNumber++].randomize();
//Processing all tubes
scoreChanged = false;
for (int i = 0; i < tubeNumber; ++i)
{
//Adding score
if (checkAddScore(tubes[i]))
{
++score;
scoreChanged = true;
}
//Check collision with bird
if (checkCollision(tubes[i]))
{
gameState = GAMESTATE_GAMEOVER;
continue;
}
//Move tube
tubes[i].move();
}
if (scoreChanged)
updateScoreString();
//Cheking the bird is too high or low
if (bird.y + bird.sizeY > WINDOW_HEIGHT - (BORDER_TOP + BORDER_DOWN - 1) || bird.y < 0)
{
gameState = GAMESTATE_GAMEOVER;
continue;
}
redrawGameWindow();
switch (kos_CheckForEvent())
{
case 1:
drawGameWindow();
break;
case 2: // key pressed
Byte keyCode;
kos_GetKey(keyCode);
if (keyCode == 32) //if pressed key is spacebar
bird.jump();
break;
case 3: // button pressed; we have only one button, close
kos_ExitApp();
}
break;
case GAMESTATE_GAMEOVER:
drawGameoverWindow();
switch (kos_WaitForEvent())
{
case 1:
drawGameoverWindow();
break;
case 2:
startGame();
break;
case 3:
kos_ExitApp();
}
break;
case GAMESTATE_MENU:
switch (kos_WaitForEvent())
{
case 1:
drawMenuWindow();
break;
case 2:
Byte keyCode;
kos_GetKey(keyCode);
if (keyCode == 0x31 || keyCode == 0x61) //1 or NumPad1
{
loopDelay = 1;
startGame();
}
else if (keyCode == 0x32 || keyCode == 0x62) //2 or NumPad2
{
loopDelay = 2;
startGame();
}
break;
case 3:
kos_ExitApp();
case 6:
Dword result;
__asm {
push 37 //Function 37 - work with mouse
pop eax
mov ebx, 3 //Subfunction 3 - states and events of the mouse buttons
int 0x40
mov result, eax
}
result &= 0x100; //bit 8 is set = left button is pressed
if ( result )
{
Dword coordinates;
__asm {
push 37 //Function 37 - work with mouse
pop eax
mov ebx, 1 //Subfunction 1 - coordinates of the mouse relative to the window
int 0x40
mov coordinates, eax
}
int clickX = coordinates >> 16;
int clickY = coordinates & 0xFFFF;
if (clickX >= 100 && clickX < 390 && clickY >= 170 && clickY < 230)
{
loopDelay = 1;
startGame();
}
else if (clickX >= 100 && clickX < 390 && clickY >= 270 && clickY < 330)
{
loopDelay = 2;
startGame();
}
}
break;
}
break;
}
}
}
void drawGameWindow()
{
kos_DefineAndDrawWindow(windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT, 0x33, 0x00FFFF, 0, 0, (Dword)HEADER_STRING);
bird.draw();
for (int i = 0; i < tubeNumber; ++i)
tubes[i].draw();
kos_WriteTextToWindow(10, 10, 0x81, 0x000000, scoreString, 0);
kos_WriteTextToWindow(10, 30, 0x81, 0x000000, CONTROL_STRING, 0);
}
void redrawGameWindow()
{
//cleaning the screen
if (scoreChanged)
kos_DrawBar(80, 10, 50, 15, 0x00FFFF);
if (bird.y > bird.prev_y)
kos_DrawBar(bird.x, bird.prev_y, bird.sizeX, bird.y - bird.prev_y, 0x00FFFF);
else
kos_DrawBar(bird.x, bird.y + bird.sizeY, bird.sizeX, bird.prev_y - bird.y, 0x00FFFF);
bird.draw();
for (int i = 0; i < tubeNumber; ++i)
tubes[i].draw();
kos_WriteTextToWindow(10, 10, 0x81, 0x000000, scoreString, 0);
kos_WriteTextToWindow(10, 30, 0x81, 0x000000, CONTROL_STRING, 0);
}
void drawGameoverWindow()
{
kos_DefineAndDrawWindow(windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT, 0x33, 0x000000, 0, 0, (Dword)HEADER_STRING);
kos_WriteTextToWindow(125, 50, 0x82, 0xFFFFFF, GAMEOVER_STRING, 0);
kos_WriteTextToWindow(135, 100, 0x81, 0xFFFFFF, scoreString, 0);
kos_WriteTextToWindow(50, 150, 0x81, 0xFFFFFF, ANY_KEY_STRING, 0);
}
void WriteBorderedText(Word x, Word y, Byte fontType, Dword textColor, const char *textPtr, Dword textLen, Dword borderColor, int borderSize)
{
kos_WriteTextToWindow(x - borderSize, y - borderSize, fontType, borderColor, textPtr, textLen);
kos_WriteTextToWindow(x - borderSize, y + borderSize, fontType, borderColor, textPtr, textLen);
kos_WriteTextToWindow(x + borderSize, y - borderSize, fontType, borderColor, textPtr, textLen);
kos_WriteTextToWindow(x + borderSize, y + borderSize, fontType, borderColor, textPtr, textLen);
kos_WriteTextToWindow(x, y, fontType, textColor, textPtr, textLen);
}
void drawMenuWindow()
{
kos_DefineAndDrawWindow(windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT, 0x33, 0x00FFFF, 0, 0, (Dword)HEADER_STRING);
WriteBorderedText(85, 40, 0x4, 0xFFFFFF, HEADER_STRING, 6, 0x000000, 2);
WriteBorderedText(185, 80, 0x84, 0xFFFFFF, HEADER_STRING + 7, 0, 0x000000, 2);
RGB* pos = &tubeHeadImage[0];
for (int x = 100 - 1; x >= 100 - Tube::headHeight; --x)
for (int y = 170; y < 170 + Tube::width; ++y)
{
kos_PutPixel(x, y, (pos->r << 16) + (pos->g << 8) + (pos->b)); //first tube
kos_PutPixel(x, y+100, (pos->r << 16) + (pos->g << 8) + (pos->b)); //second tube
++pos;
}
//First button
for(int x = 100; x < WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1); ++x)
kos_PutImage(tubeBodyImage, 1, Tube::width, x, 170);
WriteBorderedText(140, 185, 0x82, 0x000000, FAST_STRING, 0, 0xFFFFFF, 1);
//Second button
for (int x = 100; x < WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1); ++x)
kos_PutImage(tubeBodyImage, 1, Tube::width, x, 270);
WriteBorderedText(140, 285, 0x82, 0x000000, SLOW_STRING, 0, 0xFFFFFF, 1);
}
inline bool checkCollision(Tube tube)
{
return ((tube.x <= (bird.x + bird.sizeX) && tube.x + tube.width >= bird.x)
&& (bird.y <= tube.gapY || bird.y + bird.sizeY >= tube.gapY + tube.gapHeight));
}
inline bool checkAddScore(Tube tube)
{
//int diff = bird.x - (tube.x + tube.width);
//return diff == 0 || diff == 1;
return ((bird.x - (tube.x + tube.width)) >> 1) == 0;
}
void updateScoreString()
{
int temp = score;
int index = 9;
do {
scoreString[index--] = temp % 10 + '0';
temp /= 10;
} while (temp > 0);
}