forked from emrahyldrm/flappy-bird
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappybird.cpp
More file actions
404 lines (357 loc) · 11.7 KB
/
flappybird.cpp
File metadata and controls
404 lines (357 loc) · 11.7 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
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
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <iostream>
using namespace std;
#define GET_NAME(playerName) getstr(playerName)
#define FILE_NAME "bestScore.bin"
#define NAME_SIZE 100
#define JUMP 4
#define CRACK_SIZE 4
#define WAIT_BIG 140000
#define WAIT_LIT 110000
#define DOUBLE 2
void writeInfo(int row, int col);
void drawPipe(int begin, int end, int pipeCol, int row);
void drawStarting(int row, int col);
void readBest(int * bestScore, char bestPlayerName[]);
void writeBest(int bestScore, const char bestPlayerName[]);
void getPlayerName(char playerName);
void getNewPipeValue(int * crackStart, int * crackFinish, int row);
int controlCollision(int pipeCol, int birdCol, int birdRow, int crackStart,
int crackFinish);
int main() {
srand(time(NULL));
int score = 0, isScore = 0, bestScore = 0;
int isOver = 0, flag = -1;
;
int crackStart1, crackFinish1, crackStart2, crackFinish2;
int row, col, birdRow, birdCol, pipeCol1, pipeCol2;
int pipeCounter, collision;
int wait = WAIT_LIT;
int command;
char playerName[NAME_SIZE], bestPlayerName[NAME_SIZE];
char bird = '@', ch;
readBest(&bestScore, bestPlayerName);
initscr();
curs_set(0);
keypad(stdscr, true);
start_color();
int mode = rand() % 2;
// RANDOM FOREGROUND AND BACKGROUND COLORS
if (mode == 0) {
assume_default_colors(COLOR_BLACK, COLOR_CYAN); // DAY MODE
} else {
assume_default_colors(COLOR_WHITE, COLOR_BLACK); // NIGHT MODE
}
init_pair(1, COLOR_BLACK, COLOR_GREEN); // PIPE COLORS
int birdType = rand() % 3;
// RANDOM BIRD COLORS
if (birdType == 0) {
init_pair(2, COLOR_BLACK, COLOR_YELLOW);
if (mode == 0) {
init_pair(7, COLOR_YELLOW, COLOR_CYAN);
} else {
init_pair(7, COLOR_YELLOW, COLOR_BLACK);
}
} else if (birdType == 1) {
init_pair(2, COLOR_BLACK, COLOR_RED);
if (mode == 0) {
init_pair(7, COLOR_RED, COLOR_CYAN);
} else {
init_pair(7, COLOR_RED, COLOR_BLACK);
}
} else if (birdType == 2) {
init_pair(2, COLOR_BLACK, COLOR_BLUE);
if (mode == 0) {
init_pair(7, COLOR_BLUE, COLOR_CYAN);
} else {
init_pair(7, COLOR_BLUE, COLOR_BLACK);
}
}
init_pair(3, COLOR_YELLOW, COLOR_YELLOW); // UNDERGROUND COLORS
init_pair(4, COLOR_BLACK, COLOR_WHITE); // LOGO COLORS
init_pair(5, COLOR_WHITE, COLOR_RED); // GAME OVER COLORS
init_pair(6, COLOR_WHITE,
COLOR_GREEN); // GET READY AND GROUND BORDER COLORS
getmaxyx(stdscr, row, col);
if (row < 24 || col < 75) {
wait = WAIT_BIG;
}
writeInfo(row, col);
GET_NAME(playerName);
drawStarting(row, col);
refresh();
noecho();
timeout(true);
birdRow = row / 2;
birdCol = col / 4;
pipeCol1 = col;
pipeCol2 = col;
pipeCounter = col;
getNewPipeValue(&crackStart1, &crackFinish1, row);
getNewPipeValue(&crackStart2, &crackFinish2, row);
while (!isOver) {
clear();
mvaddch(birdRow, birdCol, bird | COLOR_PAIR(2));
if (score % 8 == 0) {
mvprintw(1, col / 2 - 20, "SCORE : %d", score / 8);
}
mvprintw(0, col / 2 - 20, "%s", playerName);
mvprintw(0, col / 2 + 13, "%s", bestPlayerName);
mvprintw(1, col / 2 + 13, "BEST : %d", bestScore);
attron(COLOR_PAIR(1));
drawPipe(crackStart1, crackFinish1, pipeCol1, row);
if (pipeCounter < col / 2) {
drawPipe(crackStart2, crackFinish2, pipeCol2, row);
pipeCol2--;
}
pipeCol1--;
attroff(COLOR_PAIR(1));
for (int i = 0; i < col; i++) {
attron(COLOR_PAIR(3));
mvprintw(row - 1, i, "#");
mvprintw(row - 2, i, "#");
attroff(COLOR_PAIR(3));
attron(COLOR_PAIR(6));
mvprintw(row - 3, i, "/");
attroff(COLOR_PAIR(6));
// mvprintw(2, i, "#");
}
if (birdRow < row - 1) {
birdRow++;
}
command = getch();
if (command == ' ') {
flag++;
birdRow -= JUMP;
} else if (command == KEY_F(5)) {
wait -= 10000;
}
if (birdRow < 2) {
birdRow = 2;
}
collision = controlCollision(pipeCol1, birdCol, birdRow, crackStart1,
crackFinish1);
if (collision) {
if (collision == DOUBLE) {
isOver = true;
} else if (isScore) {
score++;
if (score / 8 > bestScore) {
bestScore = score / 8;
strcpy(bestPlayerName, playerName);
}
}
}
collision = controlCollision(pipeCol2, birdCol, birdRow, crackStart2,
crackFinish2);
if (collision) {
if (collision == DOUBLE) {
isOver = true;
} else if (isScore) {
score++;
if (score / 8 > bestScore) {
bestScore = score / 8;
strcpy(bestPlayerName, playerName);
}
}
}
if (birdRow > row - 4 /*|| birdRow == 2*/) {
// TOUCH THE GROUND
isOver = true;
}
if (pipeCol1 == 0) {
getNewPipeValue(&crackStart1, &crackFinish1, row);
pipeCol1 = col;
}
if (pipeCol2 == 0) {
getNewPipeValue(&crackStart2, &crackFinish2, row);
pipeCol2 = col;
pipeCounter = col / 2;
}
isScore++;
pipeCounter--;
refresh();
usleep(wait);
}
writeBest(bestScore, bestPlayerName);
clear();
/*
____ ___
/ ___| __ _ _ __ ___ ___ / _ \__ _____ _ __
| | _ / _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__|
| |_| | (_| | | | | | | __/ | |_| |\ V / __/ |
\____|\__,_|_| |_| |_|\___| \___/ \_/ \___|_|
*/
attron(A_BOLD | COLOR_PAIR(5));
mvprintw(row / 2 - 10, (col - 51) / 2,
" ____ ___ ");
mvprintw(row / 2 - 9, (col - 51) / 2,
" / ___| __ _ _ __ ___ ___ / _ \\__ _____ _ __ ");
mvprintw(row / 2 - 8, (col - 51) / 2,
"| | _ / _` | '_ ` _ \\ / _ \\ | | | \\ \\ / / _ \\ '__|");
mvprintw(row / 2 - 7, (col - 51) / 2,
"| |_| | (_| | | | | | | __/ | |_| |\\ V / __/ | ");
mvprintw(row / 2 - 6, (col - 51) / 2,
" \\____|\\__,_|_| |_| |_|\\___| \\___/ \\_/ \\___|_| ");
attroff(A_BOLD | COLOR_PAIR(5));
mvprintw(row / 2 - 1, col / 2 - 20, "SCORE : %d", score / 8);
mvprintw(row / 2 - 2, col / 2 - 20, "%s", playerName);
mvprintw(row / 2 - 2, col / 2 + 10, "%s", bestPlayerName);
mvprintw(row / 2 - 1, col / 2 + 10, "BEST : %d", bestScore);
mvprintw(row / 2 + 1, (col - 39) / 2, "Best score was saved to \"%s\"",
FILE_NAME);
refresh();
getchar();
endwin();
return 0;
}
void writeInfo(int row, int col) {
/*
_____ _ ____ _ _
| ___| | __ _ _ __ _ __ _ _| __ )(_)_ __ __| |
| |_ | |/ _` | '_ \| '_ \| | | | _ \| | '__/ _` |
| _| | | (_| | |_) | |_) | |_| | |_) | | | | (_| |
|_| |_|\__,_| .__/| .__/ \__, |____/|_|_| \__,_|
|_| |_| |___/
*/
attron(A_BOLD | COLOR_PAIR(4));
mvprintw(row / 2 - 10, (col - 50) / 2,
" _____ _ ____ _ _ ");
mvprintw(row / 2 - 9, (col - 50) / 2,
"| ___| | __ _ _ __ _ __ _ _| __ )(_)_ __ __| |");
mvprintw(row / 2 - 8, (col - 50) / 2,
"| |_ | |/ _` | '_ \\| '_ \\| | | | _ \\| | '__/ _` |");
mvprintw(row / 2 - 7, (col - 50) / 2,
"| _| | | (_| | |_) | |_) | |_| | |_) | | | | (_| |");
mvprintw(row / 2 - 6, (col - 50) / 2,
"|_| |_|\\__,_| .__/| .__/ \\__, |____/|_|_| \\__,_|");
mvprintw(row / 2 - 5, (col - 50) / 2,
" |_| |_| |___/ ");
attroff(A_BOLD | COLOR_PAIR(4));
/*
[][][][][][]
[][] [] []
[] [] []
[][][][] [] [] []
[] [] [] [] []
[] [] [] []
[] [] [][][][][][]
[] [] [] []
[][][] [] [][][][][][]
[] [] []
[][] [][][][][]
[][][][][]
*/
attron(COLOR_PAIR(7));
mvprintw(row / 2 - 3, (col - 34) / 2, " [][][][][][] ");
mvprintw(row / 2 - 2, (col - 34) / 2, " [][] [] [] ");
mvprintw(row / 2 - 1, (col - 34) / 2, " [] [] [] ");
mvprintw(row / 2 + 0, (col - 34) / 2, " [][][][] [] [] [] ");
mvprintw(row / 2 + 1, (col - 34) / 2, "[] [] [] [] [] ");
mvprintw(row / 2 + 2, (col - 34) / 2, "[] [] [] [] ");
mvprintw(row / 2 + 3, (col - 34) / 2, "[] [] [][][][][][] ");
mvprintw(row / 2 + 4, (col - 34) / 2, " [] [] [] []");
mvprintw(row / 2 + 5, (col - 34) / 2, " [][][] [] [][][][][][] ");
mvprintw(row / 2 + 6, (col - 34) / 2, " [] [] [] ");
mvprintw(row / 2 + 7, (col - 34) / 2, " [][] [][][][][] ");
mvprintw(row / 2 + 8, (col - 34) / 2, " [][][][][] ");
attroff(COLOR_PAIR(7));
mvprintw(row / 2 + 10, (col - 24) / 2, "Enter Your Name: ");
}
int controlCollision(int pipeCol, int birdCol, int birdRow, int crackStart,
int crackFinish) {
int status = false;
if (pipeCol - 8 == birdCol || pipeCol - 7 == birdCol ||
pipeCol - 6 == birdCol || pipeCol - 5 == birdCol ||
pipeCol - 4 == birdCol || pipeCol - 3 == birdCol ||
pipeCol - 2 == birdCol || pipeCol - 1 == birdCol) {
status++;
if (birdRow < crackStart || birdRow > crackFinish) {
status++;
}
}
return status;
}
void drawPipe(int begin, int end, int pipeCol, int row) {
for (int i = 0; i < row - 3; i++) {
if (i < begin) {
if (i == begin - 1 || i == begin - 2) {
mvprintw(i, pipeCol - 9, " ");
} else if (i == begin - 3) {
mvprintw(i, pipeCol - 8, "________");
} else {
// 9876543210
mvprintw(i, pipeCol - 8, " ");
}
}
if (i > end) {
if (i == end + 1) {
mvprintw(i, pipeCol - 9, " ");
} else if (i == end + 2) {
mvprintw(i, pipeCol - 9, " ________ ");
} else {
mvprintw(i, pipeCol - 8, " ");
}
}
}
}
void drawStarting(int row, int col) {
clear();
/*
____ _ ____ _ _
/ ___| ___| |_ | _ \ ___ __ _ __| |_ _| |
| | _ / _ \ __| | |_) / _ \/ _` |/ _` | | | | |
| |_| | __/ |_ | _ < __/ (_| | (_| | |_| |_|
\____|\___|\__| |_| \_\___|\__,_|\__,_|\__, (_)
|___/
*/
attron(A_BOLD | COLOR_PAIR(6));
mvprintw(row / 2 - 10, (col - 48) / 2,
" ____ _ ____ _ _ ");
mvprintw(row / 2 - 9, (col - 48) / 2,
" / ___| ___| |_ | _ \\ ___ __ _ __| |_ _| |");
mvprintw(row / 2 - 8, (col - 48) / 2,
"| | _ / _ \\ __| | |_) / _ \\/ _` |/ _` | | | | |");
mvprintw(row / 2 - 7, (col - 48) / 2,
"| |_| | __/ |_ | _ < __/ (_| | (_| | |_| |_|");
mvprintw(row / 2 - 6, (col - 48) / 2,
" \\____|\\___|\\__| |_| \\_\\___|\\__,_|\\__,_|\\__, (_)");
mvprintw(row / 2 - 5, (col - 48) / 2,
" |___/ ");
attroff(A_BOLD | COLOR_PAIR(6));
mvprintw(row / 2 + 1, (col - 5) / 2 + 3, "]");
mvprintw(row / 2 + 1, (col - 5) / 2 - 1, "[");
mvprintw(row / 2 + 3, (col - 34) / 2,
"Press \"SPACE BAR\" to flap the bird");
for (int j = 1; j < 4; j++) {
for (int i = 0; i < j; i++) {
mvprintw(row / 2 + 1, (col - 5) / 2 - 1 + j, "#");
refresh();
}
sleep(1);
}
}
void readBest(int * bestScore, char bestPlayerName[]) {
FILE * bestFilePtr;
bestFilePtr = fopen(FILE_NAME, "rb");
if (bestFilePtr != NULL) {
fread(bestScore, sizeof(int), 1, bestFilePtr);
fread(bestPlayerName, sizeof(char) * NAME_SIZE, 1, bestFilePtr);
fclose(bestFilePtr);
}
}
void writeBest(int bestScore, const char bestPlayerName[]) {
FILE * bestFilePtr;
bestFilePtr = fopen(FILE_NAME, "wb");
fwrite(&bestScore, sizeof(int), 1, bestFilePtr);
fwrite(bestPlayerName, sizeof(char) * NAME_SIZE, 1, bestFilePtr);
fclose(bestFilePtr);
}
void getNewPipeValue(int * crackStart, int * crackFinish, int row) {
*crackStart = rand() % row / 2 + 3;
*crackFinish = *crackStart + CRACK_SIZE;
}