-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
415 lines (355 loc) · 10 KB
/
Source.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
#include <stdio.h>
#include <string>
#include <cstring>
#include <fstream>
#include <GLFW/glfw3.h>
#include <GL/glut.h>
#pragma warning(disable : 4996)
// Define the polygon vertices
GLdouble p1 = rand() % 700 + 1;
GLdouble p2 = rand() % 700 + 1;
GLdouble x1[] = { p1, p2 };
GLdouble x2[] = { p1 + 150, p2 };
GLdouble x3[] = { p1 + 150, p2 + 150 };
GLdouble x4[] = { p1, p2 + 150 };
GLint direction = 1;
GLdouble speed = 5;
GLboolean movingRight = true;
GLboolean movingTop = false;
GLint cubeSize = 150;
GLint score = 0;
GLint chancesLeft = 20;
GLboolean check = false;
GLint x, y;
GLint blink = 0;
// Regenerate a new Cube at a new position
void generateNewCube() {
cubeSize -= 2;
p1 = rand() % 700 + 1;
p2 = rand() % 700 + 1;
x1[0] = p1;
x1[1] = p2;
x2[0] = p1 + cubeSize;
x2[1] = p2;
x3[0] = p1 + cubeSize;
x3[1] = p2 + cubeSize;
x4[0] = p1;
x4[1] = p2 + cubeSize;
direction = rand() % 2 + 1;
}
// Set up the projection and modelview matrices
void myinit() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 700, 0, 700);
glMatrixMode(GL_MODELVIEW);
}
bool checkIfInside(int x1, int y1, int x2, int y2, int x, int y) {
if (x > x1 and x < x2 and y > y1 and y < y2)
return true;
return false;
}
void mouse(int button, int state, int mx, int my) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
check = true;
x = mx;
y = 700 - my;
GLint highScore;
std::ifstream file("data.txt");
if (file.is_open()) {
file >> highScore;
file.close();
}
else {
printf("Failed to open the file.\n");
return;
}
printf("%d, %d\n", x, y);
if (checkIfInside(x1[0], x1[1], x3[0], x3[1], x, y)) {
score++;
if (score > highScore) {
highScore = score;
std::ofstream outFile("data.txt");
if (outFile.is_open()) {
outFile << highScore;
outFile.close();
printf("Value updated and stored successfully.\n");
}
else {
printf("Failed to open the file.\n");
return;
}
}
printf("Catched!!\n");
if (score % 10 == 0) {
speed += 1;
}
generateNewCube();
}
else {
chancesLeft--;
printf("Missed\n");
if (chancesLeft == 0) {
exit(0);
}
}
}
glutPostRedisplay();
}
// Set up a timer to control the movement speed
void timerFunc(int value) {
if (direction == 1) {
if (movingRight) {
//Right Direction
x1[0] += speed;
x2[0] += speed;
x3[0] += speed;
x4[0] += speed;
}
else {
//Left Direction
x1[0] -= speed;
x2[0] -= speed;
x3[0] -= speed;
x4[0] -= speed;
}
}
else {
if (movingTop) {
//Top Direction
x1[1] += speed;
x2[1] += speed;
x3[1] += speed;
x4[1] += speed;
}
else {
//Bottom Direction
x1[1] -= speed;
x2[1] -= speed;
x3[1] -= speed;
x4[1] -= speed;
}
}
if (x1[0] > 700) {
movingRight = false;
generateNewCube();
}
if (x2[0] < 0) {
movingRight = true;
generateNewCube();
}
if (x2[1] > 700) {
movingTop = false;
generateNewCube();
}
if (x3[1] < 0) {
movingTop = true;
generateNewCube();
}
glutPostRedisplay();
glutMouseFunc(mouse);
glutTimerFunc(30, timerFunc, 0);
}
void dispFunc() {
glClearColor(0.65f, 0.45f, 0.45f, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(610, 680);
const char* text = "Score: ";
for (const char* c = text; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *c);
}
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(667, 679);
std::string s = std::to_string(score);
for (const char& c : s) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
GLint highScore;
std::ifstream file("data.txt");
if (file.is_open()) {
file >> highScore;
file.close();
}
else {
printf("Failed to open the file.\n");
return;
}
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(285, 680);
const char* text2 = "High Score: ";
for (const char* c = text2; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *c);
}
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(390, 679);
std::string s1 = std::to_string(highScore);
for (const char& c : s1) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
if (chancesLeft <= 5) {
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(10, 680);
const char* text = "Chances Left: ";
for (const char* c = text; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *c);
}
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(129, 679);
std::string s = std::to_string(chancesLeft);
for (const char& c : s) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
}
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2dv(x1);
glVertex2dv(x2);
glVertex2dv(x3);
glVertex2dv(x4);
glEnd();
glFlush();
}
void menu(int option) {
switch (option) {
case 1:
speed = 5;
break;
case 2:
speed = 10;
break;
case 3:
speed = 20;
break;
case 4:
exit(0);
break;
default:
break;
}
}
void mouseClick(int button, int state, int x, int y) {
// Check if the "Start" button was clicked
if (x >= 275 && x <= 425 && y >= 300 && y <= 350 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
printf("Clicked: %d, %d\n", x, y);
int m1 = glutCreateMenu(menu);
glutAddMenuEntry("Easy", 1);
glutAddMenuEntry("Medium", 2);
glutAddMenuEntry("Hard", 3);
glutCreateMenu(menu);
glutAddSubMenu("Speed", m1);
glutAddMenuEntry("Quit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(dispFunc);
glutTimerFunc(0, timerFunc, 0);
glutPostRedisplay();
}
// Check if the "Quit" button was clicked
if (x >= 275 && x <= 425 && y >= 400 && y <= 450 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
exit(0);
}
}
void timerFuncStartPage(int value) {
glutPostRedisplay();
glutTimerFunc(500, timerFuncStartPage, 0);
}
void drawStartMenu() {
glClearColor(0.185f, 0.243f, 0.252f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//College Heading
glColor3f(0.655f, 0.646f, 0.491f);
glRasterPos2i(160, 650);
std::string title2 = "RNS INSTITUTE OF TECHNOLOGY";
for (const char& c : title2) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
glColor3f(0.655f, 0.646f, 0.491f);
glRasterPos2i(190, 600);
std::string title3 = "Computer Graphics Mini Project";
for (const char& c : title3) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
// Draw a title label
glColor3f(0.655f, 0.646f, 0.491f);
glRasterPos2i(260, 500);
std::string title = "CATCH ME GAME";
for (const char& c : title) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
if (blink % 2 == 0) {
glColor3f(0.955f, 0.346f, 0.391f);
glRasterPos2i(200, 450);
std::string title2 = "You have 20 Chances to miss the cube";
for (const char& c : title2) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
}
blink++;
// "Start" button
glColor3f(0.30f, 0.81f, 0.40f);
glRecti(275, 350, 425, 400);
glColor3f(0.0f, 0.0f, 0.0f);
glRasterPos2i(325, 370);
std::string start_label = "Start";
for (const char& c : start_label) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);
}
// "Quit" button
glColor3f(0.18f, 0.559f, 0.81f);
glRecti(275, 250, 425, 300);
glColor3f(0.0f, 0.0f, 0.0f);
glRasterPos2i(325, 270);
std::string quit_label = "Quit";
for (const char& c : quit_label) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);
}
//Students details
glColor3f(0.955f, 0.346f, 0.291f);
glRasterPos2i(290, 150);
std::string title4 = "Submitted by: ";
for (const char& c : title4) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
glColor3f(0.855f, 0.846f, 0.991f);
glRasterPos2i(190, 100);
std::string title5 = "1RN20CS036 CHATRAKI AMITH";
for (const char& c : title5) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
glColor3f(0.855f, 0.846f, 0.991f);
glRasterPos2i(190, 70);
std::string title6 = "1RN20CS023 ANURAG N";
for (const char& c : title6) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
glutWireTeapot(1);
glFlush();
glutSwapBuffers();
}
void toggleGlutWindowMaximizeBox(char* WindowTitle)
{
long dwStyle;
HWND hwndGlut;
wchar_t wtext[20];
mbstowcs(wtext, WindowTitle, strlen(WindowTitle) + 1);//Plus null
LPCWSTR WindowName = wtext;
hwndGlut = FindWindow(NULL, WindowName);
dwStyle = GetWindowLong(hwndGlut, GWL_STYLE);
dwStyle ^= WS_MAXIMIZEBOX;
SetWindowLong(hwndGlut, GWL_STYLE, dwStyle);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(700, 700);
glutInitWindowPosition(350, 50);
char WindowTitle[] = "CATCH ME";
glutCreateWindow(WindowTitle);
toggleGlutWindowMaximizeBox(WindowTitle);
myinit();
glutDisplayFunc(drawStartMenu);
glutMouseFunc(mouseClick);
glutTimerFunc(0, timerFuncStartPage, 0);
glutMainLoop();
return 0;
}