-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
731 lines (605 loc) · 21.3 KB
/
main.c
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
#include <GL/glut.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Global variables
float wheelPosition = 1.0f; // Current position of the wheel on the road
float wheelSpeed = -0.01f; // Speed of the wheel movement
float roadPosition = 0.0f; // Current position of dashes on the road
float dashSpeed = 0.01f; // Speed of the moving dashes
float wheelRotation = 0.0f; // Current rotation angle of the wheel
float wheelRotationSpeed = -1.0f; // Speed of the wheel rotation
float hydrantPosition = 1.0f; // Current position of the fire hydrant
float hydrantSpeed = -0.01f; // Speed of the fire hydrant movement
bool wheelActive = true; // Flag indicating if the wheel is active
bool hydrantActive = false; // Flag indicating if the fire hydrant is active
int elapsedTime = 0; // Elapsed time in milliseconds
float catJumpHeight = 0.0f;
int score = 0;
char scoreString[100]; // Adjust the size according to your needs
double threshold = 0.1; // Adjust the value according to your needs
int timeSinceHydrant = 0;
int windowWidth = 800;
int windowHeight = 600;
bool isGameOver = false;
// Number of stars
#define NUM_STARS 100
// RGB values for VIBGYOR colors
float colors[][3] = {
{0.0, 0.0, 1.0}, // indigo
{0.0, 1.0, 0.0}, // Green
{1.0,1.0, 0.0}, // Yellow
{0.0, 1.0, 1.0}, // blue
{0.6, 0.55, 0.0}, // Orange
{1.0, 0.0, 1.0}, // Violet
{1.0, 0.0, 0.0}, // Red
};
int colorIndex = 0; // Current color index
// Structure to hold star information
typedef struct {
float x;
float y;
float size;
float alpha;
float speed;
} Star;
Star stars[NUM_STARS];
// Initialize stars with random values
void initStars() {
int i;
for (i = 0; i < NUM_STARS; i++) {
stars[i].x = ((float)rand() / RAND_MAX) * 2.0 - 1.0; // Random x position between -1.0 and 1.0
stars[i].y = ((float)rand() / RAND_MAX) * 0.55 + 0.4; // Random y position between 0.45 and 1.0
stars[i].size = ((float)rand() / RAND_MAX) * 10.0 + 10.0; // Random size between 1.0 and 5.0
stars[i].alpha = ((float)rand() / RAND_MAX) * 0.5 + 0.5; // Random alpha between 0.5 and 1.0
stars[i].speed = ((float)rand() / RAND_MAX) * 0.01 + 0.005; // Random speed between 0.005 and 0.015
}
}
// Update stars' positions
void updateStars() {
int i;
for (i = 0; i < NUM_STARS; i++) {
stars[i].x -= stars[i].speed;
if (stars[i].x < -1.0) {
stars[i].x = 1.0;
}
}
}
void makeHydrantReappear(int value) {
hydrantActive = true; // Make the fire hydrant reappear
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
float exitxL = -0.5; // Example value for exitxL
float exityL = 0.5; // Example value for exityL
float exityU = -0.5; // Example value for exityU
float bx = (exitxL * 0.5)-0.35; // Half the length along the x-axis
float by = exityL+0.09;
float w = 0.014;
float step = (exityL - exityU) / 5.0;
float bh = step * 5.0;
float bw = bh * 0.80952 * 0.5; // Half the length along the x-axis
float round = 0.024;
float th = 0.05;
float ts = 0.01;
float ty = by - bh * 0.8;
// Background
glBegin(GL_QUADS);
// Bottom-left corner (purple)
glColor3f(0.2, 0.0, 0.2);
glVertex2f(-1.0, -1.0);
// Top-left corner (purple)
glColor3f(0.2, 0.0, 0.2);
glVertex2f(-1.0, 1.0);
// Top-right corner (black)
glColor3f(0.0, 0.0, 0.0);
glVertex2f(1.0, 1.0);
// Bottom-right corner (black)
glColor3f(0.0, 0.0, 0.0);
glVertex2f(1.0, -1.0);
glEnd();
/*// Set the color for the moving dashes on the road
glColor3f(1.0f, 1.0f, 1.0f);
// Draw the moving dashes on the road
glBegin(GL_QUADS);
for (float x = -0.8f + roadPosition; x < 0.8f; x += 0.2f) {
glVertex2f(x, -0.42f);
glVertex2f(x, -0.44f);
glVertex2f(x + 0.1f, -0.44f);
glVertex2f(x + 0.1f, -0.42f);
}
glEnd();*/
// Set the color for the moving dashes on the road
glColor3f(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2]);
// Draw the moving dashes on the road
/*glBegin(GL_QUADS);
for (float x = -0.8f + roadPosition; x < 0.8f; x += 0.2f) {
glVertex2f(x, -0.42f);
glVertex2f(x, -0.44f);
glVertex2f(x + 0.1f, -0.44f);
glVertex2f(x + 0.1f, -0.42f);
}
glEnd();*/
glColor3f(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2]);
glBegin(GL_QUADS);
glVertex2f(-1.0, -0.42f);
glVertex2f(1.0, -0.42f);
glVertex2f(1.0, -0.44f);
glVertex2f(-1.0, -0.44f);
glEnd();
glColor3f(colors[colorIndex][1], colors[colorIndex][2], colors[colorIndex][0]);
glBegin(GL_QUADS);
glVertex2f(-1.0, -0.46f);
glVertex2f(1.0, -0.46f);
glVertex2f(1.0, -0.48f);
glVertex2f(-1.0, -0.48f);
glEnd();
glColor3f(colors[colorIndex][2], colors[colorIndex][0], colors[colorIndex][1]);
glBegin(GL_QUADS);
glVertex2f(-1.0, -0.50f);
glVertex2f(1.0, -0.50f);
glVertex2f(1.0, -0.52f);
glVertex2f(-1.0, -0.52f);
glEnd();
// Set the color for the wheel
glPushMatrix();
glTranslatef(wheelPosition, -0.32f, 0.0f); // Adjust the y-coordinate for the wheel
glRotatef(wheelRotation, 0.0f, 0.0f, 1.0f);
// Draw the wheel as a circle with spokes
int numSpokes = 12;
float radius = 0.08f; // Modify this value to change the radius of the wheel
float spokeWidth = 0.01f;
float spokeAngle = 360.0f / numSpokes;
// Draw the body the wheel
glColor3f(0.75f, 0.0f, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.0f);
for (float angle = 0.0f; angle <= 360.0f; angle += 10.0f) {
float theta = angle * 3.14159f / 180.0f;
float x = radius * cos(theta);
float y = radius * sin(theta);
glVertex2f(x, y);
}
glEnd();
// Draw the spokes
glColor3f(1.0f, 1.0f, 0.0f);
glLineWidth(2.0f);
glBegin(GL_LINES);
for (int i = 0; i < numSpokes; i++) {
float angle = i * spokeAngle;
float theta = angle * 3.14159f / 180.0f;
float x1 = (radius - spokeWidth) * cos(theta);
float y1 = (radius - spokeWidth) * sin(theta);
float x2 = (radius + spokeWidth) * cos(theta);
float y2 = (radius + spokeWidth) * sin(theta);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
}
glEnd();
glPopMatrix();
// Set the color for the fire hydrant
if (hydrantActive)
{
glPushMatrix();
glTranslatef(hydrantPosition, -0.42f, 0.0f); // Adjust the y-coordinate for the fire hydrant
// Draw the fire hydrant
glColor3f(0.0f, 0.75f, 0.75f);
// Draw the main body of the fire hydrant
glBegin(GL_QUADS);
glVertex2f(-0.03f, 0.0f);
glVertex2f(-0.03f, 0.2f);
glVertex2f(0.03f, 0.2f);
glVertex2f(0.03f, 0.0f);
glEnd();
// Draw the top part of the fire hydrant
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.3f);
for (float angle = 0.0f; angle <= 360.0f; angle += 10.0f) {
float theta = angle * 3.14159f / 180.0f;
float x = 0.03f * cos(theta);
float y = 0.3f + 0.03f * sin(theta);
glVertex2f(x, y);
}
glEnd();
// Draw the nozzle of the fire hydrant
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(-0.02f, 0.2f);
glVertex2f(-0.02f, 0.3f);
glVertex2f(0.02f, 0.3f);
glVertex2f(0.02f, 0.2f);
glEnd();
/* // Draw the base of the fire hydrant
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(-0.05f, 0.0f);
glVertex2f(-0.05f, -0.1f);
glVertex2f(0.05f, -0.1f);
glVertex2f(0.05f, 0.0f);
glEnd(); */
glPopMatrix();
}
/*if (hydrantActive)
{
score += 5;
}*/
//Tail
glColor3f(0.6, 0.6, 0.6);
glBegin(GL_QUAD_STRIP);
glVertex2f(bx, ty+ catJumpHeight);
glVertex2f(bx, ty+ catJumpHeight + th);
ty += ts * 2;
glVertex2f(bx - ts, ty+ catJumpHeight);
glVertex2f(bx - ts, ty + th+ catJumpHeight);
ty += ts * 1;
glVertex2f(bx - ts * 2, ty+ catJumpHeight);
glVertex2f(bx - ts * 2, ty + th+ catJumpHeight);
ty += ts * 2;
glVertex2f(bx - ts * 3, ty+ catJumpHeight);
glVertex2f(bx - ts * 3, ty + th+ catJumpHeight);
ty += ts * 3;
glVertex2f(bx - ts * 4, ty+ catJumpHeight);
glVertex2f(bx - ts * 4, ty + th+ catJumpHeight);
glEnd();
// Dark grey color for the entire cat shape
glBegin(GL_POLYGON);
/* glColor3f(0.2, 0.2, 0.2); // Dark grey color
glVertex2f(bx, by - 0.75);
glVertex2f(bx, by + round - 0.75);
glVertex2f(bx + bw, by + round - 0.75);
glVertex2f(bx + bw, by - 0.75);
// Reduce the height to half (divide by 2)
glVertex2f(bx + bw, (by - bh / 4) - 0.75);
glVertex2f(bx + bw - round, (by - bh / 4) - 0.75);
glVertex2f(bx + round, (by - bh / 4 - round) - 0.75);
glVertex2f(bx + round, (by - bh / 4) - 0.75); */
glColor3f(0.2, 0.2, 0.2); // Dark grey color
glVertex2f(bx, by - 0.75 + catJumpHeight);
glVertex2f(bx, by + round - 0.75 + catJumpHeight);
glVertex2f(bx + bw, by + round - 0.75 + catJumpHeight);
glVertex2f(bx + bw, by - 0.75 + catJumpHeight);
glVertex2f(bx + bw, (by - bh / 4) - 0.75 + catJumpHeight);
glVertex2f(bx + bw - round, (by - bh / 4) - 0.75 + catJumpHeight);
glVertex2f(bx + round, (by - bh / 4 - round) - 0.75 + catJumpHeight);
glVertex2f(bx + round, (by - bh / 4) - 0.75 + catJumpHeight);
glEnd();
// Light grey color for the inner rectangular polygon with rounded edges
glBegin(GL_POLYGON);
glColor3f(0.35, 0.35, 0.35); // Light grey color
float innerRound = round * 0.8; // Adjust the inner round value to make it smaller than the outer round value
glVertex2f(bx + round, by - 0.75+ catJumpHeight);
glVertex2f(bx + round, (by - bh / 4 - round) - 0.75+ catJumpHeight);
glVertex2f(bx + bw - round, (by - bh / 4 - round) - 0.75+ catJumpHeight);
glVertex2f(bx + bw - round, by - 0.75+ catJumpHeight);
glVertex2f(bx + bw - innerRound, by - 0.75+ catJumpHeight);
glVertex2f(bx + bw - innerRound, (by - bh / 4 - innerRound) - 0.75+ catJumpHeight);
glVertex2f(bx + innerRound, (by - bh / 4 - innerRound) - 0.75+ catJumpHeight);
glVertex2f(bx + innerRound, by - 0.75+ catJumpHeight);
glEnd();
// Four paws
float fr = 0.02;
float fx = bx + bw -0.23;
float fy = by - bh - fr;
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.6, 0.6, 0.6);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + fr * cos(i), fy+ catJumpHeight + fr * sin(i));
}
glEnd();
fx -= bw-0.26;
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.6, 0.6, 0.6);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + fr * cos(i), fy + fr * sin(i)+ catJumpHeight);
}
glEnd();
fx = fx + bw * 0.8;
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.6, 0.6, 0.6);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + fr * cos(i), fy + fr * sin(i)+ catJumpHeight);
}
glEnd();
fx = (fx * 0.6)-0.05;
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.6, 0.6, 0.6);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + fr * cos(i), fy + fr * sin(i)+ catJumpHeight);
}
glEnd();
// Face
fx = bx + bw;
fy = by - bh * 0.8;
fr = 0.07;
glColor3f(0.6, 0.6, 0.6);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + fr * cos(i), fy + fr * sin(i)+ catJumpHeight);
}
glEnd();
// Ear
float ew = fr * 0.9;
glBegin(GL_TRIANGLES);
// Right ear
glVertex2f(fx, fy+ catJumpHeight);
glVertex2f(fx + ew, fy+ catJumpHeight);
glVertex2f(fx + ew, fy + catJumpHeight+ ew * 2);
// Left ear
glVertex2f(fx, fy+ catJumpHeight);
glVertex2f(fx - ew, fy+ catJumpHeight);
glVertex2f(fx - ew, fy + catJumpHeight+ ew * 2);
glEnd();
// Left eye
fr = 0.01;
fx -= fr * 2.5;
//fy += 0.05; // Move up by 0.25
glColor3f(0, 0, 0);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + fr * cos(i), fy + catJumpHeight+ fr * sin(i));
}
glEnd();
// Gleam
fr = 0.0025;
glColor3f(1, 1, 1);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx - fr * 2 + fr * cos(i), fy + catJumpHeight+ fr * 2 + fr * sin(i));
}
glEnd();
// Right eye
fr = 0.01;
fx += fr * 4.5;
//fy += 0.05; // Move up by 0.25
glColor3f(0, 0, 0);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx - fr * cos(i), fy + catJumpHeight+ fr * sin(i));
}
glEnd();
// Gleam
fr = 0.0025;
glColor3f(1, 1, 1);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx - fr * 2 + fr * cos(i), fy + catJumpHeight+ fr * 2 + fr * sin(i));
}
glEnd();
// Nose
fr = 0.005; // Radius of the nose
fx = bx + bw;
//fy *= 0.9;
fy -= 0.0000001;
glColor3f(0, 0, 0);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx - fr * cos(i), fy + catJumpHeight+ fr * sin(i));
}
glEnd();
/*Mouth*/
//fy *= 0.8;
fy -= 0.04; // Reduce the space between mouth and eyes by adjusting fy
float mw = 5; // Mouth width measured in nose widths
float mh = 2; // Mouth height measured in nose widths
glLineWidth(2);
glBegin(GL_LINE_STRIP);
glVertex2f(fx - fr * mw, fy + catJumpHeight+ fr * mh);
glVertex2f(fx - fr * mw, fy+ catJumpHeight);
glVertex2f(fx, fy+ catJumpHeight);
glVertex2f(fx, fy + catJumpHeight+ fr * mh);
glVertex2f(fx, fy+ catJumpHeight);
glVertex2f(fx + fr * mw, fy+ catJumpHeight);
glVertex2f(fx + fr * mw, fy+ catJumpHeight + fr * mh);
glEnd();
fr *= 1.4;
glColor3f(1, 0.6, 0.6);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx + mw * 1.2 * fr - fr * cos(i), fy + catJumpHeight+ fr * sin(i) + fr * mh);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 200; i += 6) {
glVertex2f(fx - mw * 1.2 * fr - fr * cos(i), fy+ catJumpHeight + fr * sin(i) + fr * mh);
}
glEnd();
// Draw the scoreboard
// Display the score using OpenGL functions
// You can choose the position and other properties of the text based on your preferences.
// Example code to display the score at the top-left corner of the window:
char scoreString[100]; // Adjust the size according to your needs
snprintf(scoreString, sizeof(scoreString), "Score: %d", score);
glColor3f(1.0f, 1.0f, 1.0f); // Set text color to white
glRasterPos2f(-0.85f, 0.75f); // Position the text at the top-left corner
int len = strlen(scoreString);
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, scoreString[i]);
}
// INSERT HERE
// Generate a new fire hydrant after a certain time interval
if (!hydrantActive && timeSinceHydrant >= 3000) {
hydrantActive = true;
hydrantPosition = 1.0f;
timeSinceHydrant = 0;
}
// Check for collision between cat and wheel
int collisionDetected = 0;
if (fabs(wheelPosition - bx) < threshold && !collisionDetected && catJumpHeight != 0.6f)
//if (((wheelPosition - fx) < 0.0 || (wheelPosition - bx) < 0.0 ) && catJumpHeight != 0.6f)
{
collisionDetected = true;
isGameOver=true;
wheelSpeed = 0.0f; // Stop the wheel movement
dashSpeed = 0.0f; // Stop the moving dashes
hydrantSpeed = 0.0f; // Stop the fire hydrant movement
wheelRotationSpeed = 0.0f; // Stop the wheel rotation
// Display "GAME OVER! PRESS -> TO PLAY AGAIN" message
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, windowWidth, 0, windowHeight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f); // Set text color to white
glRasterPos2f(windowWidth / 2 - 150, windowHeight - 50); // Position the text at the top center of the window
char* gameOverMessage = "GAME OVER! Press -> to play again";
int len = strlen(gameOverMessage);
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, gameOverMessage[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
//Check for collision between cat and fire hydrant
if (((hydrantPosition - fx) < -0.0 || (hydrantPosition - bx) < 0.0) && catJumpHeight != 0.6f)
{
collisionDetected = true;
hydrantActive = false; // Make the fire hydrant disappear
score += 5; // Add 5 points to the score
int reappearDelay = 500;
glutTimerFunc(reappearDelay, makeHydrantReappear, 0);
}
// If the wheel is not active (i.e., cat touched the wheel), do not update positions
if (wheelActive) {
// Update wheel position and rotation
wheelPosition += wheelSpeed;
wheelRotation += wheelRotationSpeed;
// If the wheel goes beyond the screen, reset its position
if (wheelPosition < -1.0f)
wheelPosition = 1.0f;
}
int i;
//glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for (i = 0; i < NUM_STARS; i++) {
glColor4f(1.0, 1.0, 1.0, stars[i].alpha);
glPointSize(stars[i].size);
glVertex2f(stars[i].x, stars[i].y);
}
glEnd();
glFlush();
glutSwapBuffers();
}
void update(int value) {
// Update the position of the wheel
wheelPosition += wheelSpeed;
// Wrap the wheel position when it reaches the left edge
if (wheelPosition < -1.2f)
wheelPosition = 1.0f;
// Update the rotation angle of the wheel
wheelRotation += wheelRotationSpeed;
// Wrap the wheel rotation when it completes a full rotation
if (wheelRotation > 360.0f)
wheelRotation -= 360.0f;
// Update the position of the dashes
roadPosition -= dashSpeed;
// Wrap the dashes around when they reach the left edge of the road
if (roadPosition < -0.2f)
roadPosition = 0.0f;
// Update the position of the fire hydrant if it is active
if (hydrantActive) {
hydrantPosition += hydrantSpeed;
// Deactivate the fire hydrant if it moves beyond the left edge
if (hydrantPosition < -1.2f) {
hydrantActive = false;
}
}
// Activate the fire hydrant after a 5-second delay
if (elapsedTime >= 5000 && !hydrantActive) {
hydrantActive = true;
hydrantPosition = 1.0f;
}
elapsedTime += 16; // Update elapsed time
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
// Function to reset the game variables and state
void restartGame() {
wheelPosition = 1.0f; // Current position of the wheel on the road
wheelSpeed = -0.01f; // Speed of the wheel movement
roadPosition = 0.0f; // Current position of dashes on the road
dashSpeed = 0.01f; // Speed of the moving dashes
wheelRotation = 0.0f; // Current rotation angle of the wheel
wheelRotationSpeed = -1.0f; // Speed of the wheel rotation
hydrantPosition = 1.0f; // Current position of the fire hydrant
hydrantSpeed = -0.01f; // Speed of the fire hydrant movement
wheelActive = true; // Flag indicating if the wheel is active
hydrantActive = false; // Flag indicating if the fire hydrant is active
elapsedTime = 0; // Elapsed time in milliseconds
catJumpHeight = 0.0f;
score = 0;
scoreString[100]; // Adjust the size according to your needs
threshold = 0.1; // Adjust the value according to your needs
timeSinceHydrant = 0;
windowWidth = 800;
windowHeight = 600;
isGameOver = false;
}
void specialKeyPressed(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
// Jump the cat by adjusting the catJumpHeight
catJumpHeight = 0.6f;
break;
case GLUT_KEY_RIGHT:
restartGame();
// Add more cases for other arrow keys if needed
}
}
void specialKeyReleased(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
// Reset the catJumpHeight when the up arrow key is released
catJumpHeight = 0.0f;
break;
// Add more cases for other arrow keys if needed
}
}
// Timer callback function
void timer(int value) {
updateStars();
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
// Reshape callback function
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void updateColor(int val){
colorIndex = (colorIndex + 1) % 7; // Increment color index
glutPostRedisplay();
glutTimerFunc(95, updateColor, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1200, 1000);
glutCreateWindow("Cat-astrophe Escape");
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
// Register the keyboard callback functions
glutSpecialFunc(specialKeyPressed);
glutSpecialUpFunc(specialKeyReleased);
glPointSize(2.0);
initStars();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutTimerFunc(0, update, 0);
glutTimerFunc(95, updateColor, 0);
glutMainLoop();
return 0;
}