-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollosion game.cpp.cpp
More file actions
780 lines (621 loc) · 15.7 KB
/
Collosion game.cpp.cpp
File metadata and controls
780 lines (621 loc) · 15.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
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
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<GL/glut.h>
//Function initialization
void drawBall(void);
void drawBanners(void);
void changeBallPosition(void);
void drawRedBox(int);
void drawOrangeBox(int);
void drawBlueBox(int);
void printText(void);
void generateObstacles(void);
void checkCollision(void);
void displayScore(void);
void startScreen(void);
void display2();
void startScreen1(void);
void startScreen2(void);
//Global variable declaration
float ballSpeed = -13.0;
float ballDeltaY = 0.0;
float boxDeltaX = 0.0;
float boxSpeed = 10.0;
float boxXPos = 500.0;
float ballYPos = 400.0;
float ballRadius = 25.0;
float boxYPos = 400.0;
int life=3;
struct ballPointFront
{
float x,y;
}ballFront;
struct ballPointUp
{
float x,y;
}ballUp;
struct ballPointDown
{
float x,y;
}ballDown;
struct boxPointsUp
{
float x1,y1,x2,y2;
}boxUp;
struct boxPointsDown
{
float x1,y1,x2,y2;
}boxDown;
struct boxPointsFront
{
float x1,y1,x2,y2;
}boxFront;
int score = 0;
int randNum = 0;
int randNum1=0;
bool flag = false;
bool flag1=false;
bool endGame = false;
bool firstScreen = true;
bool level2=false;
void startScreen1()
{
drawBanners();
int x = 230;
int y = 400;
char line16[15]="level1";
glRasterPos2f(x,y);
int len = (int) strlen(line16);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line16[i]);
}
void display1()
{
//glClear(GL_COLOR_BUFFER_BIT);
if(firstScreen == true)
startScreen();
else {
if(endGame == false && life!=0)
{
startScreen1();
drawBanners();
drawBall();
changeBallPosition();
generateObstacles();
printText();
}
else
displayScore();
//glutSwapBuffers();
}
}
void generateObstacles()
{
if(flag == false)
{
randNum = rand() % 8;
flag = true;
}
if(flag1 == false)
{
randNum1 = rand() % 20;
flag1 = true;
}
if(score<5)
{
drawRedBox(randNum);
}
else if(score>=5)
{
level2=true;
drawRedBox(randNum);
drawBlueBox(randNum1);
}
}
void drawRedBox(int i)
{
if(flag == true)
{
if(i == 0) boxYPos = 150;
if(i == 1) boxYPos = 200;
if(i == 2) boxYPos = 250;
if(i == 3) boxYPos = 300;
if(i == 4) boxYPos = 350;
if(i == 5) boxYPos = 400;
if(i == 6) boxYPos = 450;
if(i == 7) boxYPos = 500;
}
//1x1 box - RED
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(650-boxDeltaX,boxYPos);
glVertex2f(600-boxDeltaX,boxYPos);
glVertex2f(600-boxDeltaX,boxYPos-50);
glVertex2f(650-boxDeltaX,boxYPos-50);
glEnd();
//Top-right
boxUp.x1 = 650 - boxDeltaX;
boxUp.y1 = boxYPos;
//Top-left
boxUp.x2 = 600 - boxDeltaX;
boxUp.y2 = boxYPos;
//Bottom-right
boxDown.x1 = 650 - boxDeltaX;
boxDown.y1 = boxYPos - 50;
//Bottom-left
boxDown.x2 = 600 - boxDeltaX;
boxDown.y2 = boxYPos - 50;
//Top-left
boxFront.x1 = 600 - boxDeltaX;
boxFront.y1 = boxYPos;
//Bottom-left
boxFront.x2 = 600 - boxDeltaX;
boxFront.y2 = boxYPos - 50;
checkCollision();
boxDeltaX += boxSpeed;
if(600-boxDeltaX+50.0 <= 0)
{
flag = false;
boxDeltaX = 0.0;
}
}
void drawOrangeBox(int i)
{
if(flag == true)
{
if(i == 0) boxYPos = 150;
if(i == 1) boxYPos = 200;
if(i == 2) boxYPos = 250;
if(i == 3) boxYPos = 300;
if(i == 4) boxYPos = 350;
if(i == 5) boxYPos = 400;
if(i == 6) boxYPos = 450;
if(i == 7) boxYPos = 500;
}
//1x2 box - ORANGE
glColor3f(1.0,0.5,0.0);
glBegin(GL_QUADS);
glVertex2f(600-boxDeltaX,boxYPos-100);
glVertex2f(650-boxDeltaX,boxYPos-100);
glVertex2f(650-boxDeltaX,boxYPos);
glVertex2f(600-boxDeltaX,boxYPos);
glEnd();
//Top-right
boxUp.x1 = 650 - boxDeltaX;
boxUp.y1 = boxYPos;
//Top-left
boxUp.x2 = 600 - boxDeltaX;
boxUp.y2 = boxYPos;
//Bottom-right
boxDown.x1 = 650 - boxDeltaX;
boxDown.y1 = boxYPos - 50;
//Bottom-left
boxDown.x2 = 600 - boxDeltaX;
boxDown.y2 = boxYPos - 50;
//Top-left
boxFront.x1 = 600 - boxDeltaX;
boxFront.y1 = boxYPos;
//Bottom-left
boxFront.x2 = 600 - boxDeltaX;
boxFront.y2 = boxYPos - 50;
checkCollision();
boxDeltaX += boxSpeed;
if(600-boxDeltaX+50.0 <= 0)
{
flag = false;
boxDeltaX = 0.0;
}
}
void drawBlueBox(int i)
{
if(flag == true)
{
if(i == 0) boxYPos = 150;
if(i == 1) boxYPos = 200;
if(i == 2) boxYPos = 250;
if(i == 3) boxYPos = 300;
if(i == 4) boxYPos = 350;
if(i == 5) boxYPos = 400;
if(i == 6) boxYPos = 450;
if(i == 7) boxYPos = 500;
}
//1x1 box - RED
glColor3f(0.0,0.0,1.0);
glBegin(GL_QUADS);
glVertex2f(650-boxDeltaX,boxYPos);
glVertex2f(600-boxDeltaX,boxYPos);
glVertex2f(600-boxDeltaX,boxYPos-50);
glVertex2f(650-boxDeltaX,boxYPos-50);
glEnd();
//Top-right
boxUp.x1 = 650 - boxDeltaX;
boxUp.y1 = boxYPos;
//Top-left
boxUp.x2 = 600 - boxDeltaX;
boxUp.y2 = boxYPos;
//Bottom-right
boxDown.x1 = 650 - boxDeltaX;
boxDown.y1 = boxYPos - 50;
//Bottom-left
boxDown.x2 = 600 - boxDeltaX;
boxDown.y2 = boxYPos - 50;
//Top-left
boxFront.x1 = 600 - boxDeltaX;
boxFront.y1 = boxYPos;
//Bottom-left
boxFront.x2 = 600 - boxDeltaX;
boxFront.y2 = boxYPos - 50;
checkCollision();
boxDeltaX += boxSpeed;
if(600-boxDeltaX+50.0 <= 0)
{
flag = false;
boxDeltaX = 0.0;
}
}
void checkCollision()
{
if(boxUp.x2 <= 100 && boxUp.x1 >= 50)
{
//Check for ball front collision
for(float i=boxFront.y1;i>=boxFront.y2;i--)
{
if(ballUp.x == i && ((ballUp.y >= boxDown.y1) && (boxDown.y1 >= ballDown.y)))
life--;
}
}
if(boxDown.x2 <= 100 && boxUp.x1 >= 50){
for(float i=boxDown.x2;i<=boxDown.x1;i++)
{
if(ballUp.x == i && ((ballUp.y >= boxDown.y1) && (boxDown.y1 >= ballDown.y)))
life--;
}
for(float i=boxUp.x2;i<=boxUp.x1;i++)
{
if(ballDown.x == i && ((ballDown.y <= boxUp.y1) && (boxUp.y1 <= ballDown.y)))
life--;
}
if(life==0)
endGame = true;
}
}
void startScreen()
{
drawBanners();
char line1[50] = "INSTRUCTIONS";
char line2[100] = "1.Press down the left mouse button to slow down the diamond ball";
char line3[100] = "2.Press down the right mouse button to fasten the diamond ball";
char line4[50] = " and release to resume normal speed";
char line5[75] = "3.Avoid collision between diamond ball and obstacle";
char line6[50] = "4.The score increases when the ball reaches";
char line7[50] = " the top/bottom edge of the screen";
char line8[50] = "5. After score 5, you reach level 2";
char line9[50]="6. You have 3 lives ";
char line10[70]="7. After reaching a score of 20 the game will end";
char line11[70] = "PRESS LEFT MOUSE BUTTON TO CONTINUE. ALL THE BEST!!";
char title[25] = "COLLISION GAME";
char names[20] = "- Anusha and Sunand";
int x = 230;
int y = 400;
glRasterPos2f(x,y);
int len = (int) strlen(line1);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line1[i]);
x = 40;
y = 350;
glRasterPos2f(x,y);
len = (int) strlen(line2);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line2[i]);
x = 40;
y = 320;
glRasterPos2f(x,y);
len = (int) strlen(line3);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line3[i]);
x = 40;
y = 290;
glRasterPos2f(x,y);
len = (int) strlen(line4);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line4[i]);
x = 40;
y = 260;
glRasterPos2f(x,y);
len = (int) strlen(line5);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line5[i]);
x = 40;
y = 230;
glRasterPos2f(x,y);
len = (int) strlen(line6);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line6[i]);
x = 40;
y = 200;
glRasterPos2f(x,y);
len = (int) strlen(line7);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line7[i]);
x = 40;
y = 170;
glRasterPos2f(x,y);
len = (int) strlen(line8);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line8[i]);
x = 40;
y = 140;
glRasterPos2f(x,y);
len = (int) strlen(line9);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line9[i]);
x = 40;
y = 110;
glRasterPos2f(x,y);
len = (int) strlen(line10);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line10[i]);
x = 40;
y = 80;
glRasterPos2f(x,y);
len = (int) strlen(line11);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line11[i]);
x = 250;
y = 500;
glRasterPos2f(x,y);
len = (int) strlen(title);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,title[i]);
x = 400;
y = 480;
glRasterPos2f(x,y);
len = (int) strlen(names);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,names[i]);
}
void displayScore()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.1,0.1,0.1,0.9);
glColor3f( 0.412, 0.412, 0.412);
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(0.0,600.0);
glVertex2f(600.0,600.0);
glVertex2f(600.0,0.0);
glEnd();
glColor3f(0.502, 0.000, 0.000);
char string[4] = " ";
itoa(score,string,10);
int x = 200;
int y = 300;
//set the position of the text in the window using the x and y coordinates
glRasterPos2f(x,y);
char txt[100] = "Your score is ";
strcat(txt, string);
//get the length of the string to display
int len = (int) strlen(txt);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,txt[i]);
}
void changeBallPosition()
{
//Check if banner is reached
if((ballYPos+ballDeltaY-ballRadius) <= 60.0 || (ballYPos+ballDeltaY+ballRadius) >= 530.0)
{
ballSpeed = -ballSpeed;
score++;
}
ballDeltaY += ballSpeed;
}
void drawBanners()
{
//Guide Lines
glColor3f(0.1,0.1,0.1);
glBegin(GL_LINES);
//Horizontal guide lines
glVertex2f(0.0,100.0);
glVertex2f(600.0,100.0);
glVertex2f(0.0,150.0);
glVertex2f(600.0,150.0);
glVertex2f(0.0,200.0);
glVertex2f(600.0,200.0);
glVertex2f(0.0,250.0);
glVertex2f(600.0,250.0);
glVertex2f(0.0,300.0);
glVertex2f(600.0,300.0);
glVertex2f(0.0,350.0);
glVertex2f(600.0,350.0);
glVertex2f(0.0,400.0);
glVertex2f(600.0,400.0);
glVertex2f(0.0,450.0);
glVertex2f(600.0,450.0);
glVertex2f(0.0,500.0);
glVertex2f(600.0,500.0);
//Vertical gudie lines
glVertex2f(100.0,50.0);
glVertex2f(100.0,550.0);
glVertex2f(50.0,50.0);
glVertex2f(50.0,550.0);
glEnd();
//Top Banner
glColor3f(0.66,0.0,0.33);
glBegin(GL_QUADS);
glVertex2f(0.0,600.0);
glVertex2f(600.0,600.0);
glVertex2f(600.,530.0);//550
glVertex2f(0.0,530.0);//550
glEnd();
//Bottom Banner
glColor3f(0.66,0.0,0.33);
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(600.0,0.0);
glVertex2f(600.0,60.0);//50
glVertex2f(0.0,60.0);//50
glEnd();
}
void drawBall()
{
/*
//Circular Ball
glColor3f(0.0,1.0,0.498);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(75, ballYPos + ballDeltaY); // center of circle
for(int i = 0; i <= 2000;i++)
//X pos + rad*cos(angle),Y pos + rad*sin(angle)
glVertex2f(75 + (25 * cos(i * 2.0 * PI / 2000)), (ballYPos+ballDeltaY) + (25 * sin(i * 2.0 * PI / 2000)));
glEnd();*/
//Diamond Ball
glColor3f(0.0,1.0,0.3);
glBegin(GL_QUADS);
glVertex2f(75, ballYPos + ballDeltaY + 25);
glVertex2f(100, ballYPos + ballDeltaY);
glVertex2f(75, ballYPos + ballDeltaY - 25);
glVertex2f(50, ballYPos + ballDeltaY);
glEnd();
//Ball Front
ballFront.x = 90;
ballFront.y = ballYPos + ballDeltaY;
//Ball Up
ballUp.x = 75;
ballUp.y = ballYPos + ballDeltaY + 25;
//Ball Down
ballDown.x = 75;
ballDown.y = ballYPos + ballDeltaY - 25;
}
void mouse(int btn,int state,int x,int y)
{
if(firstScreen == true && btn == GLUT_LEFT_BUTTON)
firstScreen = false;
//Slow down the ball
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN && ((ballYPos+ballDeltaY) > 100 && (ballYPos+ballDeltaY) < 500))
{
if(ballSpeed == 13.0)
ballSpeed = 5.0;
if(ballSpeed == -13.0)
ballSpeed = -5.0;
}
//Speed up the ball
if(btn == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
if(ballSpeed == 5.0)
ballSpeed = 13.0;
if(ballSpeed == -5.0)
ballSpeed = -13.0;
}
//fasten the ball
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN && ((ballYPos+ballDeltaY) > 100 && (ballYPos+ballDeltaY) < 500))
{
if(ballSpeed == 13.0)
ballSpeed = 25.0;
if(ballSpeed == -13.0)
ballSpeed = -25.0;
}
//Speed up the ball
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
if(ballSpeed == 25.0)
ballSpeed = 13.0;
if(ballSpeed == -5.0)
ballSpeed = -25.0;
}
}
void printText()
{
char string[4] = " ";
itoa(score,string,10);
int x = 550;
int y = 570;
//set the position of the text in the window using the x and y coordinates
glRasterPos2f(x,y);
//get the length of the string to display
int len = (int) strlen(string);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,string[i]);
}
void Timer(int i)
{
glutPostRedisplay();
glutTimerFunc(60,Timer,0);
}
void startScreen2()
{
drawBanners();
int x = 230;
int y = 400;
char line15[15]="level2";
glRasterPos2f(x,y);
int len = (int) strlen(line15);
//loop to display character by character
for (int i = 0; i < len; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,line15[i]);
}
void display2()
{
if(firstScreen == false && endGame==false && life!=0)
{
startScreen2();
drawBanners();
drawBall();
changeBallPosition();
generateObstacles();
printText();
}
else
if(life==0)
displayScore();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if(level2==false)
display1();
else
display2();
//glutSwapBuffers();
glutSwapBuffers();
}
void mykey(unsigned char key,int x,int y)
{
if(key == 'q' or key == 'Q' )
exit(0);
}
int main(int argc,char **argv)
{
printf("Press ANY key to start\n");
getch();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(50,50);
glutInitWindowSize(600,600);
glutCreateWindow("Collision Game");
glutDisplayFunc(display);
glClearColor(0.1,0.1,0.1,0.9); //Background Color GREY
//glClearColor(1.0,1.0,1.0,1.0); //Background Color WHITE
gluOrtho2D(0.0,600.0,0.0,600.0);
glutKeyboardFunc(mykey);
glutMouseFunc(mouse);
Timer(0);
glutMainLoop();
}