-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAINPROG.CPP
926 lines (848 loc) · 18.1 KB
/
MAINPROG.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<iostream.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<fstream.h>
#define BODYBCOLOR YELLOW
#define BODYCOLOR YELLOW
#define EYEBCOLOR BLACK
#define EYECOLOR BLACK
#define WINGBCOLOR RED
#define WINGCOLOR RED
#define XR 30
#define YR 15
#define EYERAD 5
#define WINGRADIUS 10
#define EOFFSET 15
#define WOFFSET 10
#define GRAV 300
#define PIPESPEED 10
#define BSPEED 150
#define TIME 90
#define LOWERLIMIT 400
#define UPPERLIMIT 40
#define LEFTLIMIT 0
#define RIGHTLIMIT 400
#define LANDSCAPECOLOR BROWN
#define LINELO 440
#define LINEUP 25
#define JUMPTIME 3
#define MAXBULLETS 30
#define BULLETLENGTH 20
#define BULLETBREADTH 10
#define BULLETCOLOR RED
#define BULLETBCOLOR RED
#define BULLETX 400
#define BIRDTIME 10
#define MINBIRDTIME 1
#define MAXBIRDS 10
#define BSPEEDRANGE 30
#define BSPEEDMIN 10
#define GPATH "C:\\TC\\BGI" //This is the path to the BGI folder within Turbo C++
#define X 100
#define Y 200
#define AL .001
#define ROFFSET 25
#define LOFFSET 5
#define TOFFSET 18
#define BOFFSET 10
class Object
{
float x,y;
protected:
float lm,rm,tm,bm;
int color, bcolor;
public:
void setXY(float a,float b)
{
x=a;
y=b;
}
void setColors(int col, int bcol)
{
color = col;
bcolor = bcol;
}
float returnX()
{
return(x);
}
float returnY()
{
return(y);
}
float returnL()
{
return(lm);
}
float returnR()
{
return(rm);
}
float returnT()
{
return(tm);
}
float returnB()
{
return(bm);
}
void moveVert(float a)
{
y = y + a;
}
void moveHor(float a)
{
x = x + a;
}
int inContact(Object* obj)
{
if((abs(tm - obj->returnB()) - abs(bm-tm+obj->returnB()-obj->returnT()))<0
&&(abs(rm - obj->returnL()) - abs(rm-lm+obj->returnR()-obj->returnL()))<0
)
return 1;
return 0;
}
};
class Circle : public Object
{
int rad;
public:
void setPoints()
{
lm = returnX()-rad;
rm = returnX()+rad;
tm = returnY()-rad;
bm = returnY()+rad;
}
void setRadius(int r)
{
rad = r;
}
void draw()
{
setcolor(color);
setfillstyle(1,bcolor);
circle(returnX(),returnY(),rad);
floodfill(returnX(),returnY(),color);
}
};
class Ellipse : public Object
{
int stangle,endangle, xrad, yrad;
public:
void setPoints()
{
lm = returnX()-xrad + LOFFSET;
rm = returnX()+xrad - ROFFSET;
tm = returnY()-yrad + TOFFSET;
bm = returnY()+yrad + BOFFSET;
}
void setStangle(int r)
{
stangle = r;
}
void setEndangle(int r)
{
endangle = r;
}
void setXRadius(int r)
{
xrad = r;
}
void setYRadius(int r)
{
yrad = r;
}
void draw()
{
setcolor(color);
setfillstyle(1,bcolor);
ellipse(returnX(),returnY(),stangle,endangle,xrad,yrad);
floodfill(returnX(),returnY(),color);
}
};
class PieSlice : public Object
{
int stangle,endangle, radius;
public:
void setStangle(int a)
{
stangle = a;
}
void setEndangle(int a)
{
endangle = a;
}
void setRadius(int a)
{
radius = a;
}
void draw()
{
setcolor(color);
setfillstyle(1,bcolor);
pieslice(returnX(),returnY(),stangle,endangle,radius);
floodfill(returnX(),returnY(),color);
}
};
class Rectangle : public Object
{
float length, breadth;
public:
void setPoints()
{
lm = returnX() - length/2;
rm = lm + length;
tm = returnY() - breadth/2;
bm = tm + breadth;
}
void setRect(float len, float br)
{
length = len;
breadth = br;
setPoints();
}
void draw()
{
setcolor(color);
setfillstyle(1,bcolor);
rectangle(lm,tm,rm,bm);
floodfill(returnX(),returnY(), color);
}
};
class Bird: public Object
{
Ellipse body;
Circle eye;
PieSlice wing;
int EOffset;
int WOffset;
public:
int numBird;
float xVelocity, yVelocity;
void setYVel(float vel)
{
yVelocity = vel;
}
void setXVel(float vel)
{
xVelocity = vel;
}
void setPoints()
{
body.setPoints();
lm = body.returnL();
rm = body.returnR();
tm = body.returnT();
bm = body.returnB();
}
void setOffset(int eoff, int woff)
{
EOffset = eoff;
WOffset = woff;
}
void setBody(int col , int bcol, int st, int end,int xr,int yr)
{
body.setColors(col,bcol);
body.setStangle(st);
body.setEndangle(end);
body.setXRadius(xr);
body.setYRadius(yr);
}
void setEye(int col, int bcol, int rad)
{
eye.setColors(col,bcol);
eye.setRadius(rad);
}
void setWing(int col , int bcol, int st, int end,int r)
{
wing.setColors(col,bcol);
wing.setStangle(st);
wing.setEndangle(end);
wing.setRadius(r);
}
void draw()
{
setCoords();
body.draw();
eye.draw();
wing.draw();
}
void setCoords()
{
body.setXY(returnX(),returnY());
eye.setXY(returnX()+EOffset,returnY());
wing.setXY(returnX()-WOffset,returnY());
setPoints();
}
void setBird()
{
setXY(X,Y);
xVelocity = yVelocity = 0;
setBody(BODYCOLOR,BODYBCOLOR,0,360,XR,YR);
setEye(EYECOLOR,EYEBCOLOR,EYERAD);
setWing(WINGCOLOR,WINGBCOLOR,180,360,WINGRADIUS);
setOffset(EOFFSET,WOFFSET);
setPoints();
}
void accelerateY(float amt)
{
yVelocity = yVelocity - amt;
}
void accelerateX(float amt)
{
xVelocity = yVelocity - amt;
}
Bird()
{
setXY(X,Y);
xVelocity = 0;
yVelocity = 0;
setBird();
}
};
class Bullet: public Object
{
Rectangle body;
int length, breadth;
public:
int numBullet;
float xVelocity, yVelocity;
void setPoints()
{
body.setPoints();
lm = body.returnL();
rm = body.returnR();
tm = body.returnT();
bm = body.returnB();
}
void setBody(int col, int bcol)
{
body.setColors(col,bcol);
body.setXY(returnX(),returnY());
body.setRect(length,breadth);
}
void setBullet()
{
setBody(BULLETCOLOR, BULLETBCOLOR);
setPoints();
}
void draw()
{
setCoords();
body.draw();
}
void setCoords()
{
body.setXY(returnX(),returnY());
setPoints();
}
void initBullet(float x, float y)
{
setXY(x,y);
yVelocity = 0;
xVelocity = BULLETX;
length = BULLETLENGTH;
breadth = BULLETBREADTH;
setBullet();
}
};
// Struct that deals with user data
struct player
{
long int score; // Current score
char userId[50]; // User ID number
char name[50]; // Name of the user
int flag; //Indicates whether a record for the current player has been created or not
};
void splash();
void createUser(player*);
void userId(player*);
void setPlayer(player*);
void move(Bird*, Bullet**, Bird**);
void updateScore(player*);
void gameOver(char*, Bullet*);
void runGame(Bird*,player*);
void help();
void leaderboard(player*);
void highscores(player*);
void credits();
void main()
{
int gd = DETECT, gm;
Bird flappy; // Creates bird object for player to play with
Bird* fpoint; // Pointer to above mentioned bird object
player p; // Current player
player* pp = &p; // Address to above player data storage
fpoint = &flappy;
int ch; // User choice holder
fstream x("scores.dat",ios::in|ios::binary); // File to store all relevant data
x.close();
splash();
do{
clrscr();
gotoxy(30,6);
cout<<"1.Create a new User ID";
gotoxy(30,8);
cout<<"2. Use existing User ID";
gotoxy(30,10);
cout<<"3. Exit";
gotoxy(30,12);
cout<<"Enter your choice\t";
cin>>ch;
switch(ch)
{
case 1:
createUser(pp);
break;
case 2:
userId(pp);
break;
case 3:
clrscr();
credits();
exit(0);
break;
}
}while(ch!=1 && ch!=2 && ch!=3);
fpoint->setBird();
/*
Displaying Menu until user decides to quit
*/
do
{
clrscr();
gotoxy(30,6);
cout<<"Bird Jump";
gotoxy(30,8);
cout<<"1. Play";
gotoxy(30,10);
cout<<"2. Help";
gotoxy(30,12);
cout<<"3. Leaderboard";
gotoxy(30,14);
cout<<"4. Your Highscores";
gotoxy(30,16);
cout<<"5. Switch to different User ID";
gotoxy(30,18);
cout<<"6. Exit";
gotoxy(30,20);
cout<<"Enter your choice: \t";
cin>>ch;
switch(ch)
{
case 1:
initgraph(&gd,&gm,GPATH);
pp->score = 0;
runGame(fpoint,pp);
closegraph();
break;
case 3:
clrscr();
leaderboard(pp);
break;
case 2:
help();
break;
case 4:
highscores(pp);
break;
case 5:
userId(pp);
break;
case 6:
credits();
exit(0);
}
}while(ch!=6);
getch();
free(fpoint);
}
/* Displays the initial splash/intro screen */
void splash()
{
int gd = DETECT, gm;
clrscr();
initgraph(&gd,&gm,GPATH);
setcolor(WHITE);
settextstyle(DEFAULT_FONT,0,4);
outtextxy(150,60,"Bird Jump");
Bird fl;
fl.setBird();
fl.setXY(100,250);
fl.draw();
Bullet b1,b2;
b1.initBullet(300,200);
b2.initBullet(400,300);
b1.draw();
b2.draw();
getch();
cleardevice();
closegraph();
}
/*Creates a new user with a new User ID*/
void createUser(player* pp)
{
int i, flag = 0;
player p;
fstream x("scores.dat",ios::in | ios::binary);
do
{
flag = 0;
clrscr();
gotoxy(30,6);
cout<<"Enter your name:\t";
cin>>pp->name;
clrscr();
gotoxy(30,8);
cout<<"\n\nEnter your userid:\t";
cin>>pp->userId;
x.close();
fstream x("scores.dat",ios::in | ios::binary);
while(x.read((char*)&p,sizeof(player)))
{
if(strcmp(p.userId,pp->userId) == 0)
{
clrscr();
gotoxy(20,4);
cout<<"User ID Already Exists";
flag = 1;
getch();
break;
}
}
}while(flag == 1);
pp->score = 0;
pp->flag = 1;
setPlayer(pp);
getch();
x.close();
}
/*Displays information about how to play the game */
void help()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,GPATH);
outtextxy(150,150,"Press any button to make the bird pulse up.");
outtextxy(150,170,"Avoid the red bullets. You lose if you get hit.");
outtextxy(150,190,"Do not go above a certain limit.");
outtextxy(150,210,"Do not go below a certain limit.");
outtextxy(150,230,"Horizontal lines mark the boundary.");
outtextxy(150,250,"Get the highest score possible.");
outtextxy(150,290,"Press any button to go back to main menu.");
getch();
cleardevice();
closegraph();
}
/*Used to switch to a different user ID*/
void userId(player* pp)
{
clrscr();
player p;
fstream x("scores.dat",ios::in | ios::binary);
int flag = 0;
do{
clrscr();
gotoxy(20,10);
cout<<"Enter a valid userid:\t";
cin>>pp->userId;
x.close();
fstream x("scores.dat",ios::in | ios::binary);
while(x.read((char*)&p,sizeof(player)))
{
if(strcmp(p.userId,pp->userId) == 0)
{
flag = 1;
break;
}
}
}while(flag == 0);
pp->score = 0;
x.close();
}
/*
Creates a record for the user
at the end of the file
It creates the file if the file is not already created
*/
void setPlayer(player* pp)
{
pp->score = 0;
fstream x("scores.dat",ios::out|ios::binary|ios::app);
x.write((char*)pp,sizeof(player));
x.close();
}
/*
Makes all the objects on the screen move
*/
void move(Bird* fp, Bullet** bullets)
{
fp->moveVert(-1*fp->yVelocity*TIME*.001);
fp->accelerateY(GRAV*TIME*.001);
for(int i =0; i<MAXBULLETS ;i++)
{
if(bullets[i] != NULL)
{
bullets[i]->moveHor(-1*bullets[i]->xVelocity * TIME * .001);
}
}
}
/*
Displays message on screen when game is over
*/
void gameOver(char* scpr, Bullet* bullets)
{
settextstyle(DEFAULT_FONT,0,2);
for (int i = 0; i<MAXBULLETS; i++)
{
if (bullets[i] != NULL)
{
free(bullets[i]);
bullets[i] = NULL;
}
}
outtextxy(100,100,"GAME OVER!!!");
outtextxy(100,125,scpr);
outtextxy(100,150,"Press any key to go to menu.");
}
/*
Function that makes the game run
Controls the parameters associated with that
session of the game
*/
void runGame(Bird* fp, player* pp)
{
if(pp->flag == 1)
pp->flag = 0;
else
setPlayer(pp);
fp->setBird();
int num = 0;
int count = JUMPTIME;
int bulletCount = 0;
int bulletTime = 0;
int bulletFlag = 1;
int birdNum = 0;
int i;
long int score = 0;
char sc[5];
char scpr[15];
scpr[0] = '\0';
Bullet* bullets[MAXBULLETS];
for(i=0;i<MAXBULLETS;i++)
bullets[i] = NULL;
srand(time(NULL));
while(fp->returnY() <= LOWERLIMIT && fp->returnY()>=UPPERLIMIT)
{
/*
Checking if the bird is in conttact with
anything else, i.e, if game is over
*/
for(int j=0;j<MAXBULLETS;j++)
{
if(bullets[j]!=NULL)
{
if(fp->inContact(bullets[j]))
{
pp->score = score;
updateScore(pp);
gameOver(scpr, bullets);
getch();
return;
}
}
}
/*
Randomly setting bulletTime for the
next bullet
*/
if(bulletFlag == 1)
{
bulletFlag = 0;
bulletTime = rand() % BIRDTIME + MINBIRDTIME;
bulletCount = bulletTime;
}
/*
Updating score by checking if
bullet has crossed
*/
for(int i=0; i<MAXBULLETS;i++)
{
if(bullets[i] != NULL && bullets[i]->returnX() <= LEFTLIMIT)
{
free(bullets[i]);
bullets[i] = NULL;
score++;
strcpy(scpr,"Score:");
strcat(scpr,ltoa(score,sc,10));
}
}
/* When something is pressed
*/
if(kbhit() && count <= 0)
{
fp->setYVel(BSPEED);
/*
Ensuring minimum jump time
is taken to jump again
*/
while(kbhit())
getch();
count = JUMPTIME;
}
count--;
/* Creating the bullet
after bulletTime has ellapsed
*/
if(bulletCount <=0)
{
bulletFlag = 1;
float x = getmaxx();
float y = rand()%LOWERLIMIT + UPPERLIMIT;
Bullet* bu = (Bullet*)malloc(sizeof(Bullet));
bu->initBullet(x,y);
i=0;
while(i<MAXBULLETS && bullets[i]!=NULL)
i++;
bu->numBullet = i;
bullets[i] = bu;
num++;
}
bulletCount--;
/*
Moving the objects on the screen
*/
move(fp,bullets);
/*
Drawing the objects
*/
cleardevice();
fp->draw();
for(i=0;i<MAXBULLETS;i++)
if(bullets[i]!=NULL)
bullets[i]->draw();
setcolor(LANDSCAPECOLOR);
line(LEFTLIMIT,LINELO,getmaxx(),LINELO);
line(LEFTLIMIT,LINEUP,getmaxx(),LINEUP);
outtextxy(100,50,scpr);
delay(TIME);
}
pp->score = score;
fp->setBird();
gameOver(scpr, bullets);
updateScore(pp);
getch();
cleardevice();
}
/*
Displays the top 10 scores gotten if available.
Displays all the scores if the total
number of scores is less than 10
*/
void leaderboard(player* pl)
{
clrscr();
int flag = 0;
player p;
fstream x;
x.open("scores.dat",ios::in|ios::binary);
cout<<"\t\t\t\tLeaderboard\n";
cout<<"\t\tUser Id\t\t\t\tScore\n\n";
int i =0;
while(x.read((char*)&p,sizeof(player)) && i<10)
{
cout<<"\t\t"<<p.userId<<"\t\t\t\t"<<p.score<<"\n\n";
i++;
}
x.close();
getch();
return;
}
/*
Displays the top 10 scores gotten by the current player if available.
Displays all the scores if the total
number of scores is less than 10
*/
void highscores(player* pl)
{
clrscr();
int flag = 0;
player p;
fstream x;
x.open("scores.dat",ios::in|ios::binary);
cout<<"\t\t\t\tYour Highscores\n";
cout<<"\t\tUser Id\t\t\t\tScore\n\n";
int i =0;
while(x.read((char*)&p,sizeof(player)) && i<10)
{
if(strcmp(p.userId,pl->userId) == 0)
{
cout<<"\t\t"<<p.userId<<"\t\t\t\t"<<p.score<<"\n\n";
i++;
}
}
x.close();
getch();
return;
}
/*
Inserts the record with userId and score
into the list of scores, keeping the
order based on score intact
*/
void updateScore(player* pp)
{
int i=0;
player p;
player t = *pp;
int flag = 0;
fstream x("scores.dat",ios::in|ios::out|ios::binary);
while(x.read((char*)&p,sizeof(player)))
{
if(p.score <= pp->score)
{
flag = 1;
x.seekp(sizeof(player)*i,ios::beg);
x.seekg(sizeof(player)*i,ios::beg);
break;
}
i++;
}
x.write((char*)&t,sizeof(player));
t=p;
i++;
x.seekg(sizeof(player)*i,ios::beg);
x.seekp(sizeof(player)*i,ios::beg);
while(x.read((char*)&p,sizeof(player)))
{
x.seekp(sizeof(player)*i,ios::beg);
x.write((char*)&t,sizeof(player));
t=p;
i++;
x.seekg(sizeof(player)*i,ios::beg);
}
x.seekp(sizeof(player)*i,ios::beg);
x.write((char*)&t,sizeof(player));
x.close();
}
/*Displays the screen with the names of the
students wwho made this project, along with
their class and section */
void credits()
{
int gd = DETECT , gm;
initgraph(&gd,&gm,GPATH);
cleardevice();
settextstyle(DEFAULT_FONT,0,2);
outtextxy(100,60,"Thanks for playing!");
getch();
cleardevice();
closegraph();
}
/* End of Code*/