-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
438 lines (317 loc) · 7.61 KB
/
main.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
#include<windows.h>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <math.h>
#include<GL/glu.h>
#include <GL/glut.h>
#define UP 1
#define DOWN 2
#define RIGHT -1
#define LEFT -2
using namespace std;
//using namespace std;
int sd ;
int c = 0;
int x, y;
bool gameover = false;
float random;
int score;
string b = "Score";
string a=" ";
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 0.0;
float _cameraAngle = 0.0;
float _ang_tri = 0.0;
float angx = 1;
float angy = 0;
float angz = 0;
float zoom = 1;
int countkey = 0;
float rl = 0.0;
float ud = -2.3;
float ab = 5;
float boxlr = 0.0;
float speed = 0.12;
int red = 255;
int green = 0;
int blue = 0;
string Result;
int shape = 1;
int obstacle =1;
int decider_1=1;
int decider_2=1;
float currentspeed = 0.12;
////////////////////////////////////////////////////////////
void drawText(char ch[],float xpos, float ypos,float zpos)//draw the text for score and game over
{
int numofchar = strlen(ch);
glLoadIdentity ();
glRasterPos3f( xpos , ypos, zpos);
for (int i = 0; i <= numofchar - 1; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ch[i]);//font used here, may use other font also
}
}
///////////////////END//////////////////////////////////////
//Draws the 3D scene
void game() //last black screen with gameover text
{
glBegin(GL_QUADS);
glColor3f(0.000, 0.000, 0.000);
glVertex3f(-5,-3.9,2);
glVertex3f(5,-3.9,2);
glVertex3f(5,4.1,2);
glVertex3f(-5,4.1,2);
glEnd();
glFlush();
}
void text() //gameover text
{
glColor3f(1.0, 1.0, 1.0);
drawText("GAME OVER. Press SPACE to Play Again. ESC to Quit.", -0.1,0,-1);
glRasterPos3d(-5,2.5,0.0);
glColor3f(0.000, 1.000, 1.000);
for(int i=0;b[i] != '\0';i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,b[i]);
glRasterPos3d(-4.5,2.5,0.0);
for(int i=0;Result[i] != '\0';i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,Result[i]);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
//
glTranslatef(1.0, 0.0, -7.0); //Move forward 5 units
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,0 );
glPushMatrix(); //Save the transformations performed thus far
///the cube starts
glPushMatrix();
glTranslatef( ab,-2.3, 0.0);//down up -(a+i)
glScalef(.5,0.5,0.5);
glColor3ub(255,160,160);
//glutSolidCube(1);
if (decider_1 % 2 == 0){
if(decider_2%2==0)
{
glutSolidCube(1.2);
obstacle=2;
}
else
{
glutSolidSphere(.7,20,20);
obstacle =1;
}
}
else
{
if(decider_2%2==0)
{
glutSolidTorus(.25,.55,20,40);
obstacle=3;
}
else
{
glPushMatrix();
glScalef(.3,.26,0);
glTranslatef(0,-3.5,0);
glLineWidth(20);
glBegin(GL_LINE_LOOP);
glVertex3i(1.0,1.0,0);
glVertex3i(5,5,0);
glVertex3i(1.0,5,0);
glVertex3i(5,1.0,0);
glVertex3i(3,7,0);
glEnd();
glPopMatrix();
obstacle=4;
}
}
///the cube ends
glPopMatrix();
///the cube ends
///the dot starts
glTranslatef(-4, ud, 0.0); //Move to the center of the trapezoid
glScalef(zoom,zoom,zoom);
glRotated(angx,1.0, 0.0, 0.0);
glRotated(angy,0.0, 1.0, 0.0);
glRotated(angz,0.0, 0.0, 1.0);
glPushMatrix();
glColor3ub(145, 187, 228);
if(shape == 1){
glutSolidSphere(0.32,30,30);
}
else if(shape == 2){
glutSolidCube(0.5);
}
else if(shape == 3){
glutSolidTorus(.06,.28,20,40);
}
else if(shape == 4){
glPushMatrix();
glScalef(.15,.12,0);
glTranslatef(-3,-3.5,0);
glLineWidth(20);
glBegin(GL_LINE_LOOP);
glVertex3i(1.0,1.0,0);
glVertex3i(5,5,0);
glVertex3i(1.0,5,0);
glVertex3i(5,1.0,0);
glVertex3i(3,7,0);
glEnd();
glPopMatrix();
}
switch (sd)
{
case RIGHT:
glutSolidSphere(.2+2,50+1,50);
break;
case LEFT:
break;
}
glPopMatrix();
///the dot ends
glPopMatrix();
glColor3ub(120, 0, 220);
glRasterPos3d(-5,-2.0,0.0);
for(int i=0;a[i] != '\0';i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,a[i]);
if(gameover)
{
game();
text();
}
stringstream convert; // stringstream used for the conversion
convert << score;//add the value of Number to the characters in the stream
Result = convert.str();//set Result to the content of the stream
glRasterPos3d(-1.3,0,0.0);
for(int i=0;b[i] != '\0';i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,b[i]);
glRasterPos3d(-0.80,0,0.0);
for(int i=0;Result[i] != '\0';i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,Result[i]);
glutSwapBuffers();
}
void update(int value)
{
ab -=speed;
if(ab < -4 && ab > -4.3)
{
if(shape != obstacle)
{
gameover = true;
}
}
if (ab < -8) {
ab = 6;
speed +=0.005;
currentspeed = speed;
score = score +1;
decider_1 = rand();
decider_2 = rand();
}
if((boxlr != rl && (rl > boxlr+.6 || rl < boxlr-.6) && ab <= -3.2 ))
{
//gameover = true;
}
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(1, update, 0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
case 'A':
shape =1;
break;
case 'd':
case 'D':
shape =2;
break;
case 'w':
case 'W':
shape =3;
break;
case 's':
case 'S':
shape =4;
break;
case 32:
ab =6;
speed=0.12;
score =0;
gameover=false;
break;
case 'p':
case 'P':
speed = 0;
break;
case 'r':
case 'R':
speed = currentspeed;
break;
case 27:
exit(0);
break;
}
}
void input(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT:
if (sd != RIGHT)
shape = 2;
if(rl <5)
rl = rl + 0.8;
break;
case GLUT_KEY_LEFT:
if (sd != LEFT)
shape = 1;
break;
case GLUT_KEY_UP:
if (sd != UP)
shape = 3;
break;
case GLUT_KEY_DOWN:
if (sd != DOWN)
shape = 4;
}
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0,0);
glutInitWindowSize(1360, 760);
//glColor3f(1.0,0.0,0.0);
//glColor3f(1.0,0.0,0.0);
//Create the window
glutCreateWindow("3d");
//gluOrtho2D(-800, 800, -800,800);
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutKeyboardFunc(keyboard);
glutSpecialFunc(input);
glutTimerFunc(10, update, 0); //Add a timer
glutFullScreen();
glutMainLoop();
return 0;
}