-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDyno_Game.cpp
292 lines (267 loc) · 7.39 KB
/
Dyno_Game.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
#include <GLUT/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void drawGround();
void myTimer(int);
void jumpFunc();
void resetGame();
void drawString();
void Draw_Figure();
void drawTree();
#define PI 3.1415
#define theta 90.0f
bool rst=0;
bool startGame = false;
bool gameRunning = false;
int max_score=0;
float gameSpeed = 1;
int timerTime = 1;
int score = 0;
float mX = 0;
float dinoVerticalPosition = 0;
float dinoPoints[2][33] = {{6, 6, 7, 7, 5, 5, 3, 3, 4, 4, 2, 2, 1.5, -1, 2, 3, 5, 7, 7, 12, 12, 9, 9, 11, 11, 8, 8, 10, 10, 9, 9, 8, 8},
{3, 1, 1, 0, 0, 3, 3, 1, 1, 0, 0, 3, 6, 10, 9, 8, 8, 10, 14.7, 15, 11, 11, 10, 10, 9, 9, 8, 8, 6, 6, 7, 7, 5}};
float tempDinoPoints[2][33] = {{6, 6, 7, 7, 5, 5, 3, 3, 4, 4, 2, 2, 1.5, -1, 2, 3, 5, 7, 7, 12, 12, 9, 9, 11, 11, 8, 8, 10, 10, 9, 9, 8, 8},
{3, 1, 1, 0, 0, 3, 3, 1, 1, 0, 0, 3, 6, 10, 9, 8, 8, 10, 14.7, 15, 11, 11, 10, 10, 9, 9, 8, 8, 6, 6, 7, 7, 5}};
float dinoEye[2] = {8, 12};
float tempDinoEye[2] = {8, 12};
int treeLastPointPosition = 128;
float treePoints[2][16] = {{125, 125, 122, 122, 123, 123, 125, 125, 128, 128, 130, 130, 131, 131, 128, 128},
{0, 8, 8, 12, 12, 9, 9, 13, 13, 8, 8, 11, 11, 7, 7, 0}};
int leg_stay_count=0;
float dinoRunningLegs = false;
int V = 22;
int gravity = -10;
float VX = V * cos(theta * PI / 180);
float VY = V * sin(theta * PI / 180);
float time = 0.1;
void myTimerDoNothing(int){
}
void dontDoAnythingIdle(){
}
void drawString(float x, float y, float z, char *string)
{
glRasterPos3f(x, y, z);
for (char *c = string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); // Updates the position
}
}
void myIdleFunc()
{
if (startGame)
{
jumpFunc();
glutPostRedisplay();
}
}
bool jump = false;
void jumpFunc()
{
if (jump)
{
dinoVerticalPosition = VY * time + 0.5 * (gravity)*time * time;
if (dinoVerticalPosition >= 0)
{
time = time + (0.1) * (gameSpeed);
for (int i = 0; i < 33; i++)
tempDinoPoints[1][i] = dinoPoints[1][i] + dinoVerticalPosition;
tempDinoEye[1] = dinoEye[1] + dinoVerticalPosition;
}
else
{
memcpy(tempDinoPoints, dinoPoints, sizeof tempDinoPoints);
memcpy(tempDinoEye, dinoEye, sizeof tempDinoEye);
time = 0.1;
dinoVerticalPosition = 0;
jump = false;
}
}
}
void myTimer(int value)
{
if (startGame)
{
jumpFunc();
glutPostRedisplay();
if (value % 10 == 0)
{
score += gameSpeed * gameSpeed;
max_score = fmax(max_score,score);
}
if (value % 100 == 0)
{
gameSpeed += 0.1;
}
}
glutTimerFunc(timerTime, myTimer, ++value);
}
void keyDown(int key, int x, int y)
{
if (key == GLUT_KEY_UP)
{
if(rst){
resetGame();
rst=0;
}
else if (startGame)
jump = true;
else
{
startGame = true;
gameRunning = true;
}
}
}
void Draw_Figure()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
// Render the text
char snum[10];
char scoreString2[30] = "Max Score :: ";
snprintf(snum, sizeof(snum), "%d", max_score);
strcat(scoreString2, snum);
drawString(70.0, 90.0, 0.0, scoreString2);
char scoreString[30] = "Score :: ";
snprintf(snum, sizeof(snum), "%d", score);
strcat(scoreString, snum);
drawString(70.0, 80.0, 0.0, scoreString);
if (!startGame)
{
char *p = (char *)"Press Up Arrow to start game";
char *u = (char *)"Made By : Jagrit Nokwal, Gaurav Baweja, Mukul Tayal";
drawString(30.0, 40.0, 0.0, p);
drawString(30.0, 5.0, 0.0, u);
rst=0;
}
//Draw Dragon
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 33; i++)
glVertex2f(tempDinoPoints[0][i], tempDinoPoints[1][i]);
glEnd();
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2d(tempDinoEye[0], tempDinoEye[1]);
glEnd();
if(leg_stay_count>1e9)leg_stay_count=0;
leg_stay_count++;
if (startGame && dinoVerticalPosition <= 0 && dinoRunningLegs)
{
glBegin(GL_POLYGON);
glVertex2f(5, 1);
glVertex2f(7, 1);
glVertex2f(7, 0);
glVertex2f(5, 0);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(2, 1);
glVertex2f(4, 1);
glVertex2f(4, 0);
glVertex2f(2, 0);
glEnd();
dinoRunningLegs = false;
}
else
dinoRunningLegs = true;
}
float groundDust[2][3] = {{100, 130, 170}, {-2, -2, -2}};
void drawGround()
{
glPointSize(5.0);
glBegin(GL_LINES);
glVertex2f(-10, 0);
glVertex2f(100, 0);
glEnd();
if (groundDust[0][0] < -10)
{
for (int i = 0; i < 2; i++)
{
groundDust[0][i] = groundDust[0][i + 1];
}
groundDust[0][2] = 100;
}
glBegin(GL_POINTS);
for (int i = 0; i < 3; i++)
{
glVertex2f(groundDust[0][i], groundDust[1][i]);
glVertex2f(groundDust[0][i] + 2, groundDust[1][i]);
glVertex2f(groundDust[0][i] + 1, groundDust[1][i] - 1);
groundDust[0][i] += -gameSpeed;
}
glEnd();
}
void drawTree()
{
if (treePoints[0][12] < -10)
{
for (int i = 0; i < 16; i++)
treePoints[0][i] += 128;
treeLastPointPosition = 100;
}
glBegin(GL_POLYGON);
glColor3f(0.2, 0.415, 0.184);
for (int i = 0; i < 16; i++)
{
glVertex2f(treePoints[0][i], treePoints[1][i]);
treePoints[0][i] += -gameSpeed;
}
treeLastPointPosition--;
glEnd();
glColor3f(0.0, 0.0, 0.0);
//Collision
if (treePoints[0][7] <= 13 && treePoints[0][7] >= 0 && dinoVerticalPosition < 13)
{
char *p = (char *)"Game Over";
char *u = (char *)"Made By : Jagrit Nokwal, Gaurav Baweja, Mukul Tayal";
drawString(30.0, 50.0, 0.0, p);
drawString(30.0, 5.0, 0.0, u);
glutTimerFunc(0, myTimerDoNothing, 0);
gameRunning = false;
startGame = false;
rst = 1;
//resetGame();
}
}
void resetGame()
{
memcpy(tempDinoPoints, dinoPoints, sizeof tempDinoPoints);
float tempTreePoints[2][16] = {{125, 125, 122, 122, 123, 123, 125, 125, 128, 128, 130, 130, 131, 131, 128, 128},
{0, 8, 8, 12, 12, 9, 9, 13, 13, 8, 8, 11, 11, 7, 7, 0}};
memcpy(treePoints, tempTreePoints, sizeof treePoints);
memcpy(tempDinoEye, dinoEye, sizeof tempDinoEye);
score = 0;
gameSpeed = 1;
}
void display()
{
Draw_Figure();
drawGround();
drawTree();
glutSwapBuffers();
}
void myinit() //set attributes
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10, 100, -10, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // GLUT_DOUBLE
glutInitWindowSize(900, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("T.Rex Run");
glutDisplayFunc(display);
glutTimerFunc(timerTime, myTimer, 0); // mili seconds , myTimer(int value), value
glutSpecialFunc(keyDown); // keyDown(int key , int x , int y)
myinit();
glutMainLoop();
return 0;
}