-
Notifications
You must be signed in to change notification settings - Fork 4
/
floppy_bird.cpp
459 lines (415 loc) · 9.76 KB
/
floppy_bird.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include<stdio.h>//hello
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define SBORDERx 10 //Start Border X coordinate
#define SBORDERy 5 //Start Border Y coordinate
#define TBORDERx 70 //TERMINATING Border X coordinate
#define TBORDERy 20 //TERMINATING Border Y coordinate
#define NOP 6 //Number of pipes
#define EASY 3 //i.e Number of Gaps
#define MEDIUM 2
#define HARD 1
#define LIVES 1
void gotoxy(int,int); //goto x and y coordinate of screen
void cls(); //clear screen
void read_records(); //from file
void print_scores(); //and print them to file
void respawn(int); //after brd crash
void modify(int); //modify scores and lives after checking the conditions
void scores(); //scores and life printer
void border(); // Border [==]
void jump(); //All bird functions and other function calling order is present here
void pipes(); //hooh! after toooooo much calculations I've created that. so Handle with care
int getrand(); //get random number
void gameover(); //after game is overed
void intro(); //the first script
int is1sttime=1;
int called_from_main;
char name[20]="Anonumous";
int _near=0;
int life=LIVES;
int offset[NOP];
int gap=2; //will be multiplied by 2 after
int _score=0;
struct info{
int x;
int y;
};
typedef struct info coordinates;
coordinates bird;
coordinates pipe[NOP]; //NOP=number of pipes
int main()
{
if(is1sttime)
{
_score=0;
strcpy(name,"");
life=LIVES;
intro();
}
is1sttime=0;
bird.x=18;
bird.y=11;
fflush(stdin);
for(int i=0;i<NOP;i++)
{
offset[i]=getrand();
pipe[i].x=TBORDERx+(10*i);
}
called_from_main=1;
jump();
}
void jump()
{
cls();
border();
scores();
pipes();
bird.y-=2;
modify(_near);
gotoxy(bird.x,bird.y);
printf("%c",258);
Sleep(151);
if(called_from_main==1)
getch();
while(!kbhit())
{
cls();
bird.y++;
border();
scores();
pipes();
gotoxy(bird.x,bird.y);
printf("%c",258);
Sleep(40);
modify(_near);
}
char ch=getch();
if(ch==' '||ch=='j')
{
called_from_main=0;
jump();
}
else
{
printf("PAUSED");
getch();
cls();
jump();
}
}
void border()
{
for(int i=SBORDERx;i<=TBORDERx;i++)
{
gotoxy(i,SBORDERy-1);
printf("%c",219);
gotoxy(i,TBORDERy+1);
printf("%c",219);
}
for(int i=SBORDERy;i<=TBORDERy;i++)
{
gotoxy(SBORDERx,i);
printf("%c",219);
gotoxy(TBORDERx,i);
printf("%c",219);
}
}
void scores()
{
gotoxy(0,0);
printf("%20s PLAYING SCORE =%d LIVES=%d",name,_score,life);
}
void pipes()
{
for(int i=0;i<NOP;i++)
{
pipe[i].x--;
if(pipe[i].x<=20)
_near=i;
if(pipe[i].x==SBORDERx)
{
offset[i]=getrand();
pipe[i].x=TBORDERx-2;
}
for(int thadla=SBORDERy+(offset[i]+gap);thadla<=TBORDERy;thadla++)
{
if(pipe[i].x<=TBORDERx)
{
gotoxy(pipe[i].x,thadla);
printf("%c%c",219,219);
}
}
for(int uparla=SBORDERy+offset[i]-gap;uparla>=SBORDERy;uparla--)
{
if(pipe[i].x<=TBORDERx)
{
gotoxy(pipe[i].x,uparla);
printf("%c%c",219,219);
}
}
}
}
int getrand()
{
srand(time(0));
int num=rand()%11;
if(num<3)
return 5;
else
return num;
}
void modify(int i)
{
if(((bird.x==pipe[i].x)||(bird.x==pipe[i].x+1))&&((bird.y>=(SBORDERy+offset[i]+gap))||(bird.y<=(SBORDERy+offset[i]-gap))))
{
life--;
gotoxy(bird.x,bird.y);
printf("%c",219);
while(bird.y++)
{
gotoxy(bird.x,bird.y);
printf("%c",235);
Sleep(100);
if(bird.y==TBORDERy+1)
break;
if(((bird.y>=(SBORDERy+offset[i]+gap))||(bird.y<=(SBORDERy+offset[i]-gap))))
{
gotoxy(bird.x,bird.y);
printf("%c",219);
}
else
{
gotoxy(bird.x,bird.y);
printf("%c",' ');
}
}
if(life>=0)
respawn(1);
else
gameover();
}
if(((bird.x==pipe[i].x+1))&&!((bird.y>=(SBORDERy+offset[i]+gap))||(bird.y<=(SBORDERy+offset[i]-gap)))) //area b/w pipes
{ printf("%c",263);
_score++;
if(_score%10==0)
life++;
}
if(bird.y>TBORDERy||bird.y<SBORDERy)
{
life--;
gotoxy(bird.x,bird.y);
printf("%c",219);
while(bird.y++)
{
gotoxy(bird.x,bird.y);
printf("%c",235);
Sleep(100);
if(bird.y>=TBORDERy)
break;
gotoxy(bird.x,bird.y);
printf("%c",' ');
}
getch();
if(life>=0)
respawn(3);
else
gameover();
}
}
void respawn(int after_s)
{
for(int i=after_s;i>=0;i--)
{
cls();
border();
gotoxy(20,8);
printf("DO NOT PRESS ANY KEY"); //OtherWise The keys will be Recorded in Stdin resulting in unwanted Jumps of Bird
gotoxy(20,10);
printf("RESPAWNING AFTER %ds\n",i);
Sleep(1000);
fflush(stdin);
}
fflush(stdin);
main();
}
void gameover()
{ cls();
printf("\n\n ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦\n ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦\n ¦ ¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦ \n ¦¦¦¦¦¦¦¦¦¦ ¦¦ ¦¦ ¦¦ ¦¦\n ¦¦¦¦¦¦¦¦¦¦ ¦¦ ¦¦¦ ¦¦ ¦ ¦¦ \n ¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦ ¦ ¦¦¦¦¦ ¦ \n ¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦ ¦ ¦¦¦¦¦¦ ¦ \n ¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦ ¦ ¦¦¦¦¦¦ ¦\n ¦¦¦¦¦¦¦¦¦¦ ¦¦ ¦¦¦ ¦¦ ¦ ¦¦¦ ¦¦ \n ¦¦ ¦¦¦¦¦¦¦ ¦¦¦ ¦¦¦ ¦¦¦¦¦¦¦¦ \n ¦¦ ¦¦¦¦¦¦ ¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦ \n ¦¦¦ ¦¦¦¦¦ ¦¦¦ ¦ \n ¦¦¦¦ ¦¦¦ ¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ \n ¦¦¦¦ ¦ ¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦\n ¦¦¦¦¦ ¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ \n ¦¦¦¦¦¦ ¦¦¦¦¦¦ \n ¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ \n ¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ \n ¦ ¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦ \n ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ \n ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ \n ");
printf("\n\t\t\t\tGAME OVER");
print_scores();
getch();
game_over:
cls();
border();
gotoxy(20,10);
printf("GAME OVER");
getch();
border();
gotoxy(20,10);
printf(".SELECT. ");
gotoxy(20,11);
printf("1.READ RECORDS");
gotoxy(20,12);
printf("2.PLAY AGAIN");
gotoxy(20,13);
printf("3.EXIT");
gotoxy(20,20);
char choice=getch();
switch(choice){
case '1':
read_records();
case '2':
is1sttime=1;
main();
case '3':
cls();
printf("HOPE YOU ENJOYED!");
exit(0);
default:
goto game_over;
}
}
void intro()
{
cls();
printf("\t\t\tFLAPPY_BIRD_IN_C_v1.0\n");
printf("\n\n\tI was playing that game on Android from last few days on\n\t 4/Jan/2016 [My Winter Vacations were ongoing].\n");
printf("\n\n\tSo as I know little much \'C\',I created that GAME \n\n\t\t\t\t\t\t--Charanjit_Singh\n\n\n");
system("pause");
cls();
gotoxy(20,3);
printf("--------ENTER_YOUR_NAME------------\n");
fflush(stdin);
gotoxy(20,5);
gets(name);
if(strcmp(name,"")==0||strcmp(name," ")==0)
strcpy(name,"Anonymous");
select_color:
fflush(stdin);
cls();
border();
gotoxy(20,6);
printf("------SELECT_COLOR-------");
gotoxy(20,7);
printf("1. Black N White");
gotoxy(20,8);
printf("2. White N Black");
gotoxy(20,9);
printf("3. Red N White");
gotoxy(20,10);
printf("4. Yellow N Black");
gotoxy(20,13);
printf("PRESS [SPACEBAR] when Selected.");
gotoxy(20,15);
char choice=getch();
switch(choice)
{
case '1':
system("color 07");
goto select_color;
case '2':
system("color f0");
goto select_color;
case '3':
system("color 4f");
goto select_color;
case '4':
system("color e0");
goto select_color;
case ' ':
break;
default:
goto select_color;
}
select_difficulty:
cls();
printf("\n\n\t\tSelect Difficulty..\n");
printf("\t1.HARD\n\t2.MEDIUM\n\t3.EASY\n");
gap=getch();
gap-=48;
if(gap>3||gap<1)
goto select_difficulty;
int loading_temp=0;
char loading_string[6][300]={
"All Done.." ,
"Building HQ-3D Bird..",
"Loading Fonts..",
"Loading World..",
"Loading Pipes..",
"Creating List of files to be Loaded"};
for(int sec=5;sec>=0;sec--)
{
cls();
printf("\n\n\t\tPress [SPACEBAR] or 'j' to JUMP\n\n\t\t\n\t\t\tGame will Start in %ds\n\n\t\t%25s",sec,loading_string[sec]);
gotoxy(20,10);
for(int i=0;i<(5-sec)*(!!loading_temp);i++) //Loading Animation Algo-
{
printf("%c%c%c%c%c",177,177,177,177,177);
}
gotoxy(20+loading_temp,10);
for(int i=0;i<5;i++)
{
printf("%c",177);
loading_temp++;
Sleep(200);
}
}
}
void print_scores()
{
char level[7]="";
if(gap==1)
strcpy(level,"HARD");
if(gap==2)
strcpy(level,"MEDIUM");
if(gap==3)
strcpy(level,"EASY");
open_again:
FILE *fp;
fp=fopen("flappy_scores.txt","r");
if(fp==NULL)
{
fp=fopen("flappy_scores.txt","w");
fprintf(fp," <<<<<<<FLAPPY SCORES>>>>>\n\n");
goto open_again;
}
fclose(fp);
fp=fopen("flappy_scores.txt","a");
fprintf(fp,"------------------------------------------\n");
fprintf(fp,"%20s scored %3d in %s Difficulty.\n\n",name,_score,level);
time_t mytime;
mytime = time(NULL);
fprintf(fp," Played Date:%s",ctime(&mytime));
fprintf(fp,"------------------------------------------\n");
fclose(fp);
}
void read_records()
{
FILE *fp;
fp=fopen("flappy_scores.txt","r");
while(1)
{
char rea=fgetc(fp);
if(rea==EOF)
break;
else
printf("%c",rea);
}
fclose(fp);
getch();
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void cls()
{
system("cls");
}