-
Notifications
You must be signed in to change notification settings - Fork 26
/
spaceship.cpp
863 lines (768 loc) · 22.5 KB
/
spaceship.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
/*
*******************************************************************************************************************
** COMPUTER GRAPHICS PROJECT -> THE SPACESHIP SHOOTING GAME
**
** BY
**
** Kaushik Jeyaraman & Nikilesh Iyer
**
** for any queries
** e-mail me : [email protected]
*******************************************************************************************************************
*******************************************************************************************************************
** G A M E D E S C R I P T I O N S
**
** keybord controls= w,a,s,d
** mouse left click to fire
**
** INSTRUCTIONS:
** Dodge the objects and shoot them down
**
** OBJECTIVE : Beat the high score.
** Score +1 point for shooting each object
** Score +50 for lvl up
*******************************************************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <math.h>
#include<string.h>
#define PI 3.14159
#define GAME_SCREEN 0 //Constant to identify background color
#define MENU_SCREEN 4
#define MAX_STONES 100
#define MAX_STONE_TYPES 5
#define stoneRotationSpeed 20
#define SPACESHIP_SPEED 20
int stoneTranslationSpeed=5;
GLint m_viewport[4];
GLint CI=0;
int x,y;
int i;
int randomStoneIndices[100];
int index;
int Score=0;
int alienLife=100;
int GameLvl= 1;
float mouseX ,mouseY ; //Cursor coordinates;
float LaserAngle=0 ,stoneAngle =0,lineWidth = 1;
float xOne=0,yOne=0; //Spaceship coordinates
float xStone[MAX_STONES] ,yStone[MAX_STONES];//coordinates of stones
float xStart = 1200; //Health bar starting coodinate
GLint stoneAlive[MAX_STONES]; //check to see if stone is killed
bool mButtonPressed= false,startGame=false,gameOver=false; //boolean values to check state of the game
bool startScreen = true ,nextScreen=false,previousScreen=false;
bool gameQuit = false,instructionsGame = false, optionsGame = false;
GLfloat a[][2]={0,-50, 70,-50, 70,70, -70,70};
GLfloat LightColor[][3]={1,1,0, 0,1,1, 0,1,0};
GLfloat AlienBody[][2]={{-4,9}, {-6,0}, {0,0}, {0.5,9}, {0.15,12}, {-14,18}, {-19,10}, {-20,0},{-6,0}};
GLfloat AlienCollar[][2]={{-9,10.5}, {-6,11}, {-5,12}, {6,18}, {10,20}, {13,23}, {16,30}, {19,39}, {16,38},
{10,37}, {-13,39}, {-18,41}, {-20,43}, {-20.5,42}, {-21,30}, {-19.5,23}, {-19,20},
{-14,16}, {-15,17},{-13,13}, {-9,10.5}};
GLfloat ALienFace[][2]={{-6,11}, {-4.5,18}, {0.5,20}, {0.,20.5}, {0.1,19.5}, {1.8,19}, {5,20}, {7,23}, {9,29},
{6,29.5}, {5,28}, {7,30}, {10,38},{11,38}, {11,40}, {11.5,48}, {10,50.5},{8.5,51}, {6,52},
{1,51}, {-3,50},{-1,51}, {-3,52}, {-5,52.5}, {-6,52}, {-9,51}, {-10.5,50}, {-12,49}, {-12.5,47},
{-12,43}, {-13,40}, {-12,38.5}, {-13.5,33},{-15,38},{-14.5,32}, {-14,28}, {-13.5,33}, {-14,28},
{-13.8,24}, {-13,20}, {-11,19}, {-10.5,12}, {-6,11} } ;
GLfloat ALienBeak[][2]={{-6,21.5}, {-6.5,22}, {-9,21}, {-11,20.5}, {-20,20}, {-14,23}, {-9.5,28}, {-7,27}, {-6,26.5},
{-4.5,23}, {-4,21}, {-6,19.5}, {-8.5,19}, {-10,19.5}, {-11,20.5} };
char highScore[100],ch;
void display();
void StoneGenerate();
void displayRasterText(float x ,float y ,float z ,char *stringToDisplay) {
int length;
glRasterPos3f(x, y, z);
length = strlen(stringToDisplay);
for(int i=0 ;i<length ;i++){
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 ,stringToDisplay[i]);
}
}
void SetDisplayMode(int modeToDisplay) {
switch(modeToDisplay){
case GAME_SCREEN: glClearColor(0, 0, 0, 1);break;
case MENU_SCREEN : glClearColor(1, 0 , 0, 1);break;
}
}
void initializeStoneArray() {
//random stones index
for(int i = 0;i < MAX_STONES ;i++) {
randomStoneIndices[i]=rand()%MAX_STONE_TYPES;
stoneAlive[i]=true;
}
xStone[0] = -(200*MAX_STONES)-600; //START LINE for stone appearance
for(int i = 0;i<MAX_STONES ;i++) { //ramdom appearance yIndex for each stone
yStone[i]=rand()%600;
if(int(yStone[i])%2)
yStone[i]*=-1;
xStone[i+1] = xStone[i] + 200; //xIndex of stone aligned with 200 units gap
}
}
void DrawAlienBody()
{
glColor3f(0,1,0); //BODY color
glBegin(GL_POLYGON);
for(i=0;i<=8;i++)
glVertex2fv(AlienBody[i]);
glEnd();
glColor3f(0,0,0); //BODY Outline
glLineWidth(1);
glBegin(GL_LINE_STRIP);
for(i=0;i<=8;i++)
glVertex2fv(AlienBody[i]);
glEnd();
glBegin(GL_LINES); //BODY effect
glVertex2f(-13,11);
glVertex2f(-15,9);
glEnd();
}
void DrawAlienCollar()
{
glColor3f(1,0,0); //COLLAR
glBegin(GL_POLYGON);
for(i=0;i<=20 ;i++)
glVertex2fv(AlienCollar[i]);
glEnd();
glColor3f(0,0,0); //COLLAR outline
glBegin(GL_LINE_STRIP);
for(i=0;i<=20 ;i++)
glVertex2fv(AlienCollar[i]);
glEnd();
}
void DrawAlienFace()
{
//glColor3f(0.6,0.0,0.286); //FACE
//glColor3f(0.8,0.2,0.1);
//glColor3f(0,0.5,1);
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<=42 ;i++)
glVertex2fv(ALienFace[i]);
glEnd();
glColor3f(0,0,0); //FACE outline
glBegin(GL_LINE_STRIP);
for(i=0;i<=42 ;i++)
glVertex2fv(ALienFace[i]);
glEnd();
glBegin(GL_LINE_STRIP); //EAR effect
glVertex2f(3.3,22);
glVertex2f(4.4,23.5);
glVertex2f(6.3,26);
glEnd();
}
void DrawAlienBeak()
{
glColor3f(1,1,0); //BEAK color
glBegin(GL_POLYGON);
for(i=0;i<=14 ;i++)
glVertex2fv(ALienBeak[i]);
glEnd();
glColor3f(0,0,0); //BEAK outline
glBegin(GL_LINE_STRIP);
for(i=0;i<=14 ;i++)
glVertex2fv(ALienBeak[i]);
glEnd();
}
void DrawAlienEyes()
{
glColor3f(0,1,1);
glPushMatrix();
glRotated(-10,0,0,1);
glTranslated(-6,32.5,0); //Left eye
glScalef(2.5,4,0);
glutSolidSphere(1,20,30);
glPopMatrix();
glPushMatrix();
glRotated(-1,0,0,1);
glTranslated(-8,36,0); //Right eye
glScalef(2.5,4,0);
glutSolidSphere(1,100,100);
glPopMatrix();
}
void DrawAlien()
{
DrawAlienBody();
DrawAlienCollar();
DrawAlienFace();
DrawAlienBeak();
DrawAlienEyes();
}
void DrawSpaceshipBody()
{
glColor3f(1,0,0); //BASE
glPushMatrix();
glScalef(70,20,1);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix(); //LIGHTS
glScalef(3,3,1);
glTranslated(-20,0,0); //1
glColor3fv(LightColor[(CI+0)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //2
glColor3fv(LightColor[(CI+1)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //3
glColor3fv(LightColor[(CI+2)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //4
glColor3fv(LightColor[(CI+0)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //5
glColor3fv(LightColor[(CI+1)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //6
glColor3fv(LightColor[(CI+2)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //7
glColor3fv(LightColor[(CI+0)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //8
glColor3fv(LightColor[(CI+1)%3]);
glutSolidSphere(1,1000,1000);
glTranslated(5,0,0); //9
glColor3fv(LightColor[(CI+2)%3]);
glutSolidSphere(1,1000,1000);
glPopMatrix();
}
void DrawSteeringWheel()
{
glPushMatrix();
glLineWidth(3);
glColor3f(0.20,0.,0.20);
glScalef(7,4,1);
glTranslated(-1.9,5.5,0);
glutWireSphere(1,8,8);
glPopMatrix();
}
void DrawSpaceshipDoom()
{
glColor4f(0.7,1,1,0.0011);
glPushMatrix();
glTranslated(0,30,0);
glScalef(35,50,1);
glutSolidSphere(1,50,50);
glPopMatrix();
}
void DrawSpaceShipLazer() {
glColor3f(1, 0, 0);
glPushMatrix();
glBegin(GL_POLYGON); //Lazer stem
glVertex2f(-55 ,10);
glVertex2f(-55 ,30);
glVertex2f(-50, 30);
glVertex2f(-50 ,10);
glEnd();
float xMid =0,yMid =0;
//Mid point of the lazer horizontal
xMid = (55+50)/2.0;
yMid = (25+35)/2.0;
//Rotating about the point ,20
glTranslated(-xMid, yMid, 0);
glRotated(LaserAngle, 0, 0 ,1);
glTranslated(xMid , -yMid ,0);
//find mid point of top of lazer stem
float midPoint = -(55+50)/2.0;
glBegin(GL_POLYGON); //Lazer horizontal stem
glVertex2f(midPoint + 10 ,25);
glVertex2f(midPoint + 10 ,35);
glVertex2f(midPoint - 10 ,35);
glVertex2f(midPoint - 10 ,25);
glEnd();
glPopMatrix();
}
void DrawLazerBeam() {
float xMid = -(55+50)/2.0;
float yMid = (25+35)/2.0;
float mouseXEnd = -((- mouseX) + xOne);
float mouseYEnd = -((- mouseY) + yOne);
glLineWidth(5); //----Laser beam width
glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex2f(xMid ,yMid);
glVertex2f(mouseXEnd ,mouseYEnd);
glEnd();
glLineWidth(1);
}
void DrawStone(int StoneIndex)
{
glPushMatrix();
glLoadIdentity();
switch(StoneIndex) //CHANGE INDEX VALUE FOR DIFFERENT STONE VARIETY;
{
case 0:
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glColor3f(0.4f, 0.0f, 0.0f);
glScalef(35,35,1);
glutSolidSphere(1,9,50);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(60,10,1);
glutSolidSphere(1,5,50);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(10,60,1);
glutSolidSphere(1,5,50);
break;
case 1:
glColor3f(1.0f, 0.8f, 0.8f);
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(15,20,1);
glutSolidSphere(1,9,50);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(40,5,1);
glutSolidSphere(1,5,50);
break;
case 2:
glColor3f(0.2f, 0.2f, 0.0f);
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(60,25,1);
glutSolidSphere(1,9,50);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(25,60,1);
glutSolidSphere(1,9,50);
break;
case 3:
glColor3f(0.8f, 0.8f, 0.1f);
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(35,10,1);
glutSolidSphere(1,10,7);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(50,20,1);
glutSolidSphere(1,5,50);
break;
case 4:
glColor3f(0.26f, 0.26f, 0.26f);
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(10,55,1);
glutSolidSphere(1,9,50);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(20,10,1);
glutSolidSphere(1,9,50);
glLoadIdentity();
glTranslated(xStone[index] , yStone[index] ,0);
glRotatef(stoneAngle+45 ,0, 0, 1);
glTranslated(0, 0, 0);
glScalef(25,10,1);
glutSolidSphere(1,9,50);
break;
}
glPopMatrix();
}
bool checkIfSpaceShipIsSafe() {
for(int i =0 ;i<MAX_STONES ;i++) {
if(stoneAlive[i]&((xOne >= (xStone[i]/2 -70) && xOne <= (xStone[i]/2 + 70) && yOne >= (yStone[i]/2 -18 ) && yOne <= (yStone[i]/2 + 53)) || (yOne <= (yStone[i]/2 - 20) && yOne >= (yStone[i]/2 - 90) && xOne >= (xStone[i]/2 - 40) && xOne <= (xStone[i]/2 + 40))))
{
stoneAlive[i]=0;
return false;
}
}
return true;
}
void SpaceshipCreate(){
glPushMatrix();
glTranslated(xOne,yOne,0);
if(!checkIfSpaceShipIsSafe() && alienLife ){
alienLife-=10;
xStart -= 23;
}
DrawSpaceshipDoom();
glPushMatrix();
glTranslated(4,19,0);
DrawAlien();
glPopMatrix();
DrawSteeringWheel();
DrawSpaceshipBody();
DrawSpaceShipLazer();
if(mButtonPressed) {
DrawLazerBeam();
}
glEnd();
glPopMatrix();
}
void DisplayHealthBar() {
glColor3f(1 ,0 ,0);
glBegin(GL_POLYGON);
glVertex2f(-xStart ,700);
glVertex2f(1200 ,700);
glVertex2f(1200 ,670);
glVertex2f(-xStart, 670);
glEnd();
char temp[40];
glColor3f(0 ,0 ,1);
sprintf(temp,"SCORE = %d",Score);
displayRasterText(-1100 ,600 ,0.4 ,temp);//<---display variable score ?
sprintf(temp," LIFE = %d",alienLife);
displayRasterText(800 ,600 ,0.4 ,temp);
sprintf(temp," LEVEL : %d",GameLvl);
displayRasterText(-100 ,600 ,0.4 ,temp);
glColor3f(1 ,0 ,0);
}
void startScreenDisplay()
{
glLineWidth(50);
SetDisplayMode(MENU_SCREEN);
glColor3f(0,0,0);
glBegin(GL_LINE_LOOP); //Border
glVertex3f(-750 ,-500 ,0.5);
glVertex3f(-750 ,550 ,0.5);
glVertex3f(750 ,550 ,0.5);
glVertex3f(750 ,-500, 0.5);
glEnd();
glLineWidth(1);
glColor3f(1, 1, 0);
glBegin(GL_POLYGON); //START GAME PLOYGON
glVertex3f(-200 ,300 ,0.5);
glVertex3f(-200 ,400 ,0.5);
glVertex3f(200 ,400 ,0.5);
glVertex3f(200 ,300, 0.5);
glEnd();
glBegin(GL_POLYGON); //INSTRUCTIONS POLYGON
glVertex3f(-200, 50 ,0.5);
glVertex3f(-200 ,150 ,0.5);
glVertex3f(200 ,150 ,0.5);
glVertex3f(200 ,50, 0.5);
glEnd();
glBegin(GL_POLYGON); //QUIT POLYGON
glVertex3f(-200 ,-200 ,0.5);
glVertex3f(-200 ,-100 ,0.5);
glVertex3f(200, -100 ,0.5);
glVertex3f(200, -200 ,0.5);
glEnd();
if(mouseX>=-100 && mouseX<=100 && mouseY>=150 && mouseY<=200){
glColor3f(0 ,0 ,1) ;
if(mButtonPressed){
startGame = true ;
gameOver = false;
mButtonPressed = false;
}
} else
glColor3f(0 , 0, 0);
displayRasterText(-100 ,340 ,0.4 ,"Start Game");
if(mouseX>=-100 && mouseX<=100 && mouseY>=30 && mouseY<=80) {
glColor3f(0 ,0 ,1);
if(mButtonPressed){
instructionsGame = true ;
mButtonPressed = false;
}
} else
glColor3f(0 , 0, 0);
displayRasterText(-120 ,80 ,0.4 ,"Instructions");
if(mouseX>=-100 && mouseX<=100 && mouseY>=-90 && mouseY<=-40){
glColor3f(0 ,0 ,1);
if(mButtonPressed){
gameQuit = true ;
mButtonPressed = false;
}
}
else
glColor3f(0 , 0, 0);
displayRasterText(-100 ,-170 ,0.4 ," Quit");
}
void GameScreenDisplay()
{
SetDisplayMode(GAME_SCREEN);
DisplayHealthBar();
glScalef(2, 2 ,0);
if(alienLife){
SpaceshipCreate();
}
else {
gameOver=true;
instructionsGame = false;
startScreen = false;
} //<----------------------gameover screen
StoneGenerate();
}
void readFromFile() {
FILE *fp = fopen("HighScoreFile.txt" ,"r");
int i=0;
if(fp!= NULL){
while(fread(&ch,sizeof(char),1 ,fp)){
highScore[i++] = ch;
}
highScore[i] = '\0';
}
fclose(fp);
}
void writeIntoFile() { //To write high score on to file
FILE *fp = fopen("HighScoreFile.txt" ,"w");
int i=0;
char temp[40];
if(fp!= NULL){
int n= Score;
while(n){
ch = (n%10)+ '0';
n/=10;
temp[i++] = ch;
}
temp[i] = '\0';
strrev(temp);
puts(temp);
if(temp[0] == '\0')
temp[i++] = '0' ,temp[i++] = '\0';
fwrite(temp ,sizeof(char)*i ,i ,fp);
}
fclose(fp);
}
void GameOverScreen()
{
SetDisplayMode(MENU_SCREEN);
glColor3f(0,0,0);
glLineWidth(50);
glBegin(GL_LINE_LOOP); //Border
glVertex3f(-650 ,-500 ,0.5);
glVertex3f(-650 ,520 ,0.5);
glVertex3f(650 ,520 ,0.5);
glVertex3f(650 ,-500, 0.5);
glEnd();
glLineWidth(1);
stoneTranslationSpeed=5;
glColor3f(0, 1, 0);
glBegin(GL_POLYGON); //GAME OVER
glVertex3f(-550 ,810,0.5);
glVertex3f(-550 ,610 ,0.5);
glVertex3f(550 ,610 ,0.5);
glVertex3f(550 ,810, 0.5);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON); //RESTART POLYGON
glVertex3f(-200, 50 ,0.5);
glVertex3f(-200 ,150 ,0.5);
glVertex3f(200 ,150 ,0.5);
glVertex3f(200 ,50, 0.5);
glEnd();
glBegin(GL_POLYGON); //QUIT POLYGON
glVertex3f(-200 ,-200 ,0.5);
glVertex3f(-200 ,-100 ,0.5);
glVertex3f(200, -100 ,0.5);
glVertex3f(200, -200 ,0.5);
glEnd();
displayRasterText(-300 ,640 ,0.4 ,"G A M E O V E R ! ! !");
glColor3f(0 , 0, 0);
char temp[40];
sprintf(temp,"Score : %d",Score);
displayRasterText(-100 ,340 ,0.4 ,temp);
readFromFile();
char temp2[40];
if(atoi(highScore) < Score){
writeIntoFile();
sprintf(temp2 ,"Highest Score :%d" ,Score);
} else
sprintf(temp2 ,"Highest Score :%s" ,highScore);
displayRasterText(-250 ,400 ,0.4 ,temp2);
if(mouseX>=-100 && mouseX<=100 && mouseY>=25 && mouseY<=75){
glColor3f(0 ,0 ,1);
if(mButtonPressed){ //Reset game default values
startGame = true ;
gameOver=false;
mButtonPressed = false;
initializeStoneArray();
alienLife=100;
xStart=1200;
Score=0;
GameLvl=1;
GameScreenDisplay();
}
} else
glColor3f(0 , 0, 0);
displayRasterText(-70 ,80 ,0.4 ,"Restart");
if(mouseX>=-100 && mouseX<=100 && mouseY>=-100 && mouseY<=-50){
glColor3f(0 ,0 ,1);
if(mButtonPressed){
exit(0);
mButtonPressed = false;
}
}
else
glColor3f(0 , 0, 0);
displayRasterText(-100 ,-170 ,0.4 ," Quit");
}
void StoneGenerate(){
if(xStone[0]>=1200){ //If the last screen hits the end of screen then go to Nxt lvl
GameLvl++;
stoneTranslationSpeed+=3;
Score+=50;
initializeStoneArray();
GameScreenDisplay();
}
for(int i=0; i<MAX_STONES ;i++){
index = i;
if(mouseX <= (xStone[i]/2+20) && mouseX >=(xStone[i]/2-20) && mouseY >= (yStone[i]/2-20) && mouseY <= (yStone[i]/2+20) && mButtonPressed){
if(stoneAlive[i]){ // IF ALIVE KILL STONE
stoneAlive[i]=0;
Score++;
if(Score%3==0) {
stoneTranslationSpeed+=1; //<--------------Rate of increase of game speed
}
}
}
xStone[i] += stoneTranslationSpeed;
if(stoneAlive[i] ) //stone alive
DrawStone(randomStoneIndices[i]);
}
stoneAngle+=stoneRotationSpeed;
if(stoneAngle > 360) stoneAngle = 0;
}
void backButton() {
if(mouseX <= -450 && mouseX >= -500 && mouseY >= -275 && mouseY <= -250){
glColor3f(0, 0, 1);
if(mButtonPressed){
mButtonPressed = false;
instructionsGame = false;
startScreenDisplay();
}
}
else glColor3f(0, 0, 0);
displayRasterText(-1000 ,-550 ,0, "Back");
}
void InstructionsScreenDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetDisplayMode(MENU_SCREEN);
//colorBackground();
glColor3f(0, 0, 0);
displayRasterText(-900 ,400 ,0.4 ,"Key 'w' to move up.");
displayRasterText(-900 ,300 ,0.4 ,"Key 's' to move down.");
displayRasterText(-900 ,200 ,0.4 ,"Key 'd' to move right.");
displayRasterText(-900 ,100 ,0.4 ,"Key 'a' to move left.");
displayRasterText(-900 ,0.0 ,0.4 ,"Left mouse click to shoot laser");
//displayRasterText(-900 ,-100 ,0.4 ,"The packet can be placed only when 's' is pressed before.");
displayRasterText(-900 ,-200 ,0.4 ,"You Get 1 point for shooting each objet and 50 points for completing each lvl ");
displayRasterText(-900, -270,0.4,"The Objective is to score maximum points");
backButton();
if(previousScreen)
nextScreen = false ,previousScreen = false; //as set by backButton()
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,1200,700);
if(startGame && !gameOver)
GameScreenDisplay();
else if(instructionsGame)
InstructionsScreenDisplay();
else if(gameOver)
GameOverScreen();
//Make spaceship bigger
else if(startScreen){
startScreenDisplay();
if(gameQuit || startGame || optionsGame || instructionsGame){
//startScreen = false;
if(startGame){
SetDisplayMode(GAME_SCREEN);
startScreen = false;
} else if(gameQuit)
exit(0);
} else if(instructionsGame) {
SetDisplayMode(GAME_SCREEN);
InstructionsScreenDisplay();
}
}
//Reset Scaling values
glScalef(1/2 ,1/2 ,0);
glFlush();
glLoadIdentity();
glutSwapBuffers();
}
void somethingMovedRecalculateLaserAngle() {
float mouseXForTan = (-50 - mouseX) + xOne;
float mouseYForTan = (35 - mouseY) + yOne;
float LaserAngleInRadian = atan(mouseYForTan/mouseXForTan);
LaserAngle = (180/PI) * LaserAngleInRadian;
}
void keys(unsigned char key, int x, int y)
{
//if(key=='w' && key=='d' ){xOne+=0.5;yOne+=0.5;}
if(key == 'd') xOne+=SPACESHIP_SPEED;
if(key == 'a') xOne-=SPACESHIP_SPEED;
if(key == 'w') {yOne+=SPACESHIP_SPEED;}
if(key == 's') {yOne-=SPACESHIP_SPEED;}
if(key == 'd' || key == 'a' || key == 'w' || key == 's')
somethingMovedRecalculateLaserAngle();
display();
}
void myinit()
{
glClearColor(0.5,0.5,0.5,0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1200,1200,-700,700); //<-----CHANGE THIS TO GET EXTRA SPACE
// gluOrtho2D(-200,200,-200,200);
glMatrixMode(GL_MODELVIEW);
}
void passiveMotionFunc(int x,int y) {
//when mouse not clicked
mouseX = float(x)/(m_viewport[2]/1200.0)-600.0; //converting screen resolution to ortho 2d spec
mouseY = -(float(y)/(m_viewport[3]/700.0)-350.0);
//Do calculations to find value of LaserAngle
somethingMovedRecalculateLaserAngle();
display();
}
void mouseClick(int buttonPressed ,int state ,int x, int y) {
if(buttonPressed == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
mButtonPressed = true;
else
mButtonPressed = false;
display();
}
void UpdateColorIndexForSpaceshipLights(int value)
{
CI=(CI+1)%3; //Color Index swapping to have rotation effect
display();
glutTimerFunc(250,UpdateColorIndexForSpaceshipLights,0);
}
void idleCallBack() { //when no mouse or keybord pressed
display();
}
void main(int argc, char** argv) {
FILE *fp = fopen("HighScoreFile.txt" ,"r") ; //check if HighScoreFile.txt exist if not create
if(fp!=NULL)
fclose(fp);
else
writeIntoFile();
glutInit(&argc, argv);
glutInitWindowSize(1200,700);
glutInitWindowPosition(90 ,0);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutTimerFunc(50,UpdateColorIndexForSpaceshipLights,0);
glutCreateWindow("THE SPACESHIP SHOOTING GAME");
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glutPassiveMotionFunc(passiveMotionFunc);
glBlendFunc(GL_SRC_ALPHA ,GL_ONE_MINUS_SRC_ALPHA);
glutIdleFunc(idleCallBack);
glutMouseFunc(mouseClick);
glGetIntegerv(GL_VIEWPORT ,m_viewport);
myinit();
SetDisplayMode(GAME_SCREEN);
initializeStoneArray();
glutMainLoop();
}