-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_Car_Racing_ComputerGraphics.cpp
861 lines (748 loc) · 22.4 KB
/
2D_Car_Racing_ComputerGraphics.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
#include<bits/stdc++.h>
#include <GL/glut.h>
using namespace std;
GLvoid obstracule(GLdouble x, GLdouble y);
///function prototype for drawing text
void drawText(string str, int xpos, int ypos);
void drawTextRed(string str, int xpos, int ypos);
///draw score
char buffer[10];
void drawTextNum(string ch, int xpos, int ypos);
///take bool type variable for controlling game over and score
bool gameover = false;
int score = -1;
float tx = 0, ty = 0, y = 0, yy = 0;///for draw_all
float cx = 0, cy = 0;///for car
void init(void)
{
glClearColor(0.420, 0.557, 0.137, 0.0);
glOrtho(0, 100, 0, 100, -1.0, 1.0); //describes a transformation that produces a parallel projection.
}
char *itoa(long i, char* s, int dummy_radix) {
sprintf(s, "%ld", i);
return s;
}
GLvoid drawCircle(GLdouble xc, GLdouble yc, GLdouble rad)///function for drawing circle
{
GLfloat i;
glPointSize(3);
glBegin(GL_POLYGON);
for (i = 0; i <= 7; i += .01)
glVertex2f(xc + rad * cos(i), yc + rad * sin(i));
glEnd();
}
GLvoid tree_l(GLdouble x, GLdouble y)///function for drawing left side tree
{
glBegin(GL_QUADS);
glColor3f(.75, 0, 0);
glVertex2f(x, y);
glVertex2f(x - 10, y + 5);
glVertex2f(x - 10, y + 8);
glVertex2f(x, y + 3);
glEnd();
glColor3f(0, 1, 0);
drawCircle(x - 10, y + 5, 5);
drawCircle(x - 10, y + 11, 5);
drawCircle(x - 5, y + 8, 5);
}
///level 1 case
GLvoid tree_r(GLdouble x, GLdouble y)///function for drawing right side tree
{
glBegin(GL_QUADS);
glColor3f(.75, 0, 0);
glVertex2f(x, y);
glVertex2f(x + 10, y + 5);
glVertex2f(x + 10, y + 8);
glVertex2f(x, y + 3);
glEnd();
glColor3f(0, 1, 0);
drawCircle(x + 10, y + 5, 5);
drawCircle(x + 10, y + 11, 5);
drawCircle(x + 5, y + 8, 5);
}
///level 2 case
GLvoid tree_l2(GLdouble x, GLdouble y)///function for drawing left side tree
{
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex2f(x, y);
glVertex2f(x - 10, y + 5);
glVertex2f(x - 10, y + 8);
glVertex2f(x, y + 3);
glEnd();
glColor3f(0.75, 0.75, 0);
drawCircle(x - 10, y + 5, 5);
drawCircle(x - 10, y + 11, 5);
drawCircle(x - 5, y + 8, 5);
}
GLvoid tree_r2(GLdouble x, GLdouble y)///function for drawing right side tree
{
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex2f(x, y);
glVertex2f(x + 10, y + 5);
glVertex2f(x + 10, y + 8);
glVertex2f(x, y + 3);
glEnd();
glColor3f(0.75, 0.75, 0);
drawCircle(x + 10, y + 5, 5);
drawCircle(x + 10, y + 11, 5);
drawCircle(x + 5, y + 8, 5);
}
/// level 3 Case
GLvoid tree_l3(GLdouble x, GLdouble y)///function for drawing left side tree
{
glBegin(GL_QUADS);
glColor3f(0, 0, 1);
glVertex2f(x, y);
glVertex2f(x - 10, y + 5);
glVertex2f(x - 10, y + 8);
glVertex2f(x, y + 3);
glEnd();
glColor3f(1, 1, 0);
drawCircle(x - 10, y + 5, 5);
drawCircle(x - 10, y + 11, 5);
drawCircle(x - 5, y + 8, 5);
}
GLvoid tree_r3(GLdouble x, GLdouble y)///function for drawing right side tree
{
glBegin(GL_QUADS);
glColor3f(0, 0, 1);
glVertex2f(x, y);
glVertex2f(x + 10, y + 5);
glVertex2f(x + 10, y + 8);
glVertex2f(x, y + 3);
glEnd();
glColor3f(1, 1, 0);
drawCircle(x + 10, y + 5, 5);
drawCircle(x + 10, y + 11, 5);
drawCircle(x + 5, y + 8, 5);
}
GLvoid draw_all(GLdouble x, GLdouble y)///function for drawing everything except car
{
tree_l(x + 20, y + 0);//left side tree
tree_l(x + 20, y + 10);
tree_l(x + 20, y + 30);
tree_l(x + 20, y + 50);
tree_l(x + 20, y + 60);
tree_l(x + 20, y + 70);
tree_l(x + 20, y + 90);
tree_r(x + 80, y + 0);//right side tree
tree_r(x + 80, y + 10);
tree_r(x + 80, y + 30);
tree_r(x + 80, y + 50);
tree_r(x + 80, y + 60);
tree_r(x + 80, y + 70);
tree_r(x + 80, y + 90);
glColor3f(0.561, 0.737, 0.561);
glBegin(GL_POLYGON);//main road
glVertex2f(x + 30, y + 0);
glVertex2f(x + 70, y + 0);
glVertex2f(x + 70, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);//yellow line left
glVertex2f(x + 30, y + 0);
glVertex2f(x + 32, y + 0);
glVertex2f(x + 32, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);//yellow line right
glVertex2f(x + 70, y + 0);
glVertex2f(x + 68, y + 0);
glVertex2f(x + 68, y + 100);
glVertex2f(x + 70, y + 100);
glEnd();
glColor3f(0.741, 0.718, 0.420);
glBegin(GL_POLYGON);//left footpath
glVertex2f(x + 30, y + 0);
glVertex2f(x + 25, y + 0);
glVertex2f(x + 25, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(0.741, 0.718, 0.420);
glBegin(GL_POLYGON);//right footpath
glVertex2f(x + 70, y + 0);
glVertex2f(x + 75, y + 0);
glVertex2f(x + 75, y + 100);
glVertex2f(x + 70, y + 100);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);//zebra lines starts
glVertex2f(x + 49, y + 100);
glVertex2f(x + 49, y + 90);
glVertex2f(x + 51, y + 90);
glVertex2f(x + 51, y + 100);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 80);
glVertex2f(x + 49, y + 70);
glVertex2f(x + 51, y + 70);
glVertex2f(x + 51, y + 80);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 60);
glVertex2f(x + 49, y + 50);
glVertex2f(x + 51, y + 50);
glVertex2f(x + 51, y + 60);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 40);
glVertex2f(x + 49, y + 30);
glVertex2f(x + 51, y + 30);
glVertex2f(x + 51, y + 40);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);//zebra lines finishes
glVertex2f(x + 49, y + 20);
glVertex2f(x + 49, y + 10);
glVertex2f(x + 51, y + 10);
glVertex2f(x + 51, y + 20);
glEnd();
}
GLvoid draw_all_level2(GLdouble x, GLdouble y)///function for drawing everything except car
{
tree_l2(x + 20, y + 0);//left side tree
tree_l2(x + 20, y + 10);
tree_l2(x + 20, y + 30);
tree_l2(x + 20, y + 50);
tree_l2(x + 20, y + 60);
tree_l2(x + 20, y + 70);
tree_l2(x + 20, y + 90);
tree_r2(x + 80, y + 0);//right side tree
tree_r2(x + 80, y + 10);
tree_r2(x + 80, y + 30);
tree_r2(x + 80, y + 50);
tree_r2(x + 80, y + 60);
tree_r2(x + 80, y + 70);
tree_r2(x + 80, y + 90);
glColor3f(0.561, 0.561, 0.561);
glBegin(GL_POLYGON);//main road
glVertex2f(x + 30, y + 0);
glVertex2f(x + 70, y + 0);
glVertex2f(x + 70, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);//yellow line left
glVertex2f(x + 30, y + 0);
glVertex2f(x + 32, y + 0);
glVertex2f(x + 32, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);//yellow line right
glVertex2f(x + 70, y + 0);
glVertex2f(x + 68, y + 0);
glVertex2f(x + 68, y + 100);
glVertex2f(x + 70, y + 100);
glEnd();
glColor3f(0.741, 0.718, 0.420);
glBegin(GL_POLYGON);//left footpath
glVertex2f(x + 30, y + 0);
glVertex2f(x + 25, y + 0);
glVertex2f(x + 25, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(0.741, 0.718, 0.420);
glBegin(GL_POLYGON);//right footpath
glVertex2f(x + 70, y + 0);
glVertex2f(x + 75, y + 0);
glVertex2f(x + 75, y + 100);
glVertex2f(x + 70, y + 100);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);//zebra lines starts
glVertex2f(x + 49, y + 100);
glVertex2f(x + 49, y + 90);
glVertex2f(x + 51, y + 90);
glVertex2f(x + 51, y + 100);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 80);
glVertex2f(x + 49, y + 70);
glVertex2f(x + 51, y + 70);
glVertex2f(x + 51, y + 80);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 60);
glVertex2f(x + 49, y + 50);
glVertex2f(x + 51, y + 50);
glVertex2f(x + 51, y + 60);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 40);
glVertex2f(x + 49, y + 30);
glVertex2f(x + 51, y + 30);
glVertex2f(x + 51, y + 40);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);//zebra lines finishes
glVertex2f(x + 49, y + 20);
glVertex2f(x + 49, y + 10);
glVertex2f(x + 51, y + 10);
glVertex2f(x + 51, y + 20);
glEnd();
}
GLvoid draw_all_level3(GLdouble x, GLdouble y)///function for drawing everything except car
{
tree_l3(x + 20, y + 0);//left side tree
tree_l3(x + 20, y + 10);
tree_l3(x + 20, y + 30);
tree_l3(x + 20, y + 50);
tree_l3(x + 20, y + 60);
tree_l3(x + 20, y + 70);
tree_l3(x + 20, y + 90);
tree_r3(x + 80, y + 0);//right side tree
tree_r3(x + 80, y + 10);
tree_r3(x + 80, y + 30);
tree_r3(x + 80, y + 50);
tree_r3(x + 80, y + 60);
tree_r3(x + 80, y + 70);
tree_r3(x + 80, y + 90);
glColor3f(0.2, 0.2, 0.2);
glBegin(GL_POLYGON);//main road
glVertex2f(x + 30, y + 0);
glVertex2f(x + 70, y + 0);
glVertex2f(x + 70, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);//yellow line left
glVertex2f(x + 30, y + 0);
glVertex2f(x + 32, y + 0);
glVertex2f(x + 32, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);//yellow line right
glVertex2f(x + 70, y + 0);
glVertex2f(x + 68, y + 0);
glVertex2f(x + 68, y + 100);
glVertex2f(x + 70, y + 100);
glEnd();
glColor3f(0.741, 0.718, 0.420);
glBegin(GL_POLYGON);//left footpath
glVertex2f(x + 30, y + 0);
glVertex2f(x + 25, y + 0);
glVertex2f(x + 25, y + 100);
glVertex2f(x + 30, y + 100);
glEnd();
glColor3f(0.741, 0.718, 0.420);
glBegin(GL_POLYGON);//right footpath
glVertex2f(x + 70, y + 0);
glVertex2f(x + 75, y + 0);
glVertex2f(x + 75, y + 100);
glVertex2f(x + 70, y + 100);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);//zebra lines starts
glVertex2f(x + 49, y + 100);
glVertex2f(x + 49, y + 90);
glVertex2f(x + 51, y + 90);
glVertex2f(x + 51, y + 100);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 80);
glVertex2f(x + 49, y + 70);
glVertex2f(x + 51, y + 70);
glVertex2f(x + 51, y + 80);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 60);
glVertex2f(x + 49, y + 50);
glVertex2f(x + 51, y + 50);
glVertex2f(x + 51, y + 60);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);
glVertex2f(x + 49, y + 40);
glVertex2f(x + 49, y + 30);
glVertex2f(x + 51, y + 30);
glVertex2f(x + 51, y + 40);
glEnd();
glColor3f(1, 1, 1);
glBegin(GL_POLYGON);//zebra lines finishes
glVertex2f(x + 49, y + 20);
glVertex2f(x + 49, y + 10);
glVertex2f(x + 51, y + 10);
glVertex2f(x + 51, y + 20);
glEnd();
}
GLvoid obstracule(GLdouble x, GLdouble y)///function for drawing obstacle
{
glColor3f(0.545, 0.000, 0.000);
glBegin(GL_POLYGON);//obstracules
glVertex2f(x + 33, y + 50);
glVertex2f(x + 48, y + 50);
glVertex2f(x + 48, y + 53);
glVertex2f(x + 33, y + 53);
glEnd();
}
///obstacle green color for level 3
GLvoid obstracule3(GLdouble x, GLdouble y)///function for drawing obstacle
{
glColor3f(0.34, 1, 0);
glBegin(GL_POLYGON);//obstracules
glVertex2f(x + 33, y + 50);
glVertex2f(x + 48, y + 50);
glVertex2f(x + 48, y + 53);
glVertex2f(x + 33, y + 53);
glEnd();
}
GLvoid car(GLdouble x, GLdouble y)///function for drawing car
{
glColor3f(1, 0, 0);
glBegin(GL_POLYGON);//player car body
glVertex2f(x + 40, y + 5);
glVertex2f(x + 44, y + 5);
glVertex2f(x + 46, y + 8);
glVertex2f(x + 47, y + 24);
glVertex2f(x + 46, y + 28);
glVertex2f(x + 44, y + 32);
glVertex2f(x + 40, y + 32);
glVertex2f(x + 38, y + 28);
glVertex2f(x + 37, y + 24);
glVertex2f(x + 38, y + 8);
glVertex2f(x + 40, y + 5);
glEnd();
glColor3f(0, 0, 0);//car inside
glBegin(GL_POLYGON);
glVertex2f(x + 38, y + 8);
glVertex2f(x + 46, y + 8);
glVertex2f(x + 46, y + 24);
glVertex2f(x + 38, y + 24);
glVertex2f(x + 38, y + 8);
glEnd();
glColor3f(1, 0, 0);//car roof
glBegin(GL_POLYGON);
glVertex2f(x + 40, y + 10);
glVertex2f(x + 44, y + 10);
glVertex2f(x + 44, y + 20);
glVertex2f(x + 40, y + 20);
glVertex2f(x + 40, y + 10);
glEnd();
glColor3f(1, 0, 0);//up right roof connector
glBegin(GL_POLYGON);
glVertex2f(x + 44, y + 20);
glVertex2f(x + 44, y + 19.5);
glVertex2f(x + 46, y + 23.5);
glVertex2f(x + 46, y + 24);
glVertex2f(x + 44, y + 20);
glEnd();
glColor3f(1, 0, 0);//up left roof connector
glBegin(GL_POLYGON);
glVertex2f(x + 40, y + 20);
glVertex2f(x + 40, y + 19.5);
glVertex2f(x + 38, y + 23.5);
glVertex2f(x + 38, y + 24);
glVertex2f(x + 40, y + 20);
glEnd();
glColor3f(1, 0, 0);//bottom right roof connector
glBegin(GL_POLYGON);
glVertex2f(x + 44, y + 10);
glVertex2f(x + 44, y + 10.5);
glVertex2f(x + 46, y + 8.5);
glVertex2f(x + 46, y + 8);
glVertex2f(x + 44, y + 10);
glEnd();
glColor3f(1, 0, 0);//bottom left roof connector
glBegin(GL_POLYGON);
glVertex2f(x + 40, y + 10);
glVertex2f(x + 40, y + 10.5);
glVertex2f(x + 38, y + 8.5);
glVertex2f(x + 38, y + 8);
glVertex2f(x + 40, y + 10);
glEnd();
}
void display()
{
///for clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
///1st window main drawing start from origin x=0 y=0
///translate window's component that means changing position of component
glPushMatrix(); // push and pop the current matrix stack
glTranslated(tx, ty, 0);
draw_all(0, 0);
glPopMatrix(); ///end of 1st draw_all() function
///2nd window drawing of all components x remain same but y increased by 100
///that will draw all components outside of top window
glPushMatrix();
glTranslated(tx, ty, 0);
draw_all(0, 100);
glPopMatrix();///end of 2nd draw_all() function for animation
///translating 1st(left side) obstacle (x axis = tx) & (y axis = y)
/// y axis need not any translation because
glPushMatrix();
glTranslated(tx, y, 0);
obstracule(0, 50);
glPopMatrix(); ///1st(left) obstacle translation ends
///translating 2nd(right side) obstacle (x axis = tx) & (y axis = yy)
glPushMatrix();
glTranslated(tx, yy, 0);
obstracule(19, 130);
glPopMatrix(); ///2nd (right) obstacle translation ends
///translating Car (x axis = cx) & (y axis = cy)
glPushMatrix();
glTranslated(cx, cy, 0);
car(0, 0);
glPopMatrix(); ///car translate ends
///live score
score = score + 1;
glColor3f(1, 1, 1);
drawText("Score:", 41, 95);
itoa(score, buffer, 10);
drawTextNum(buffer, 52, 95);
if (gameover == true)
{
drawTextRed("Game Over", 45, 55);
drawTextRed("Press UP Arrow Key to play again", 33, 50);
score = -1;
glutSwapBuffers(); //swaps the buffers of the current window if double buffered
}
///end of live score
glFlush();
}
void display_level2()
{
///for clear all pixels
glClear(GL_COLOR_BUFFER_BIT); //clear buffers to preset values
///1st window main drawing start from origin x=0 y=0
///translate window's component that means changing position of component
glPushMatrix();
glTranslated(tx, ty, 0);
draw_all_level2(0, 0);
glPopMatrix(); ///end of 1st draw_all() function
///2nd window drawing of all components x remain same but y increased by 100
///that will draw all components outside of top window
glPushMatrix();
glTranslated(tx, ty, 0);
draw_all_level2(0, 100);
glPopMatrix();///end of 2nd draw_all() function for animation
///translating 1st(left side) obstacle (x axis = tx) & (y axis = y)
/// y axis need not any translation because
glPushMatrix();
glTranslated(tx, y, 0);
obstracule(0, 50);
glPopMatrix(); ///1st(left) obstacle translation ends
///translating 2nd(right side) obstacle (x axis = tx) & (y axis = yy)
glPushMatrix();
glTranslated(tx, yy, 0);
obstracule(19, 130);
glPopMatrix(); ///2nd (right) obstacle translation ends
///translating Car (x axis = cx) & (y axis = cy)
glPushMatrix();
glTranslated(cx, cy, 0);
car(0, 0);
glPopMatrix(); ///car translate ends
///live score
score = score + 1;
glColor3f(1, 1, 1);
drawText("Score:", 41, 95);
itoa(score, buffer, 10);
drawTextNum(buffer, 52, 95);
if (gameover == true)
{
drawTextRed("Game Over", 45, 55);
drawTextRed("Press UP Arrow Key to play again", 33, 50);
score = -1;
glutSwapBuffers();
}
///end of live score
glFlush();
}
void display_level3()
{
///for clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
///1st window main drawing start from origin x=0 y=0
///translate window's component that means changing position of component
glPushMatrix();
glTranslated(tx, ty, 0);
draw_all_level3(0, 0);
glPopMatrix(); ///end of 1st draw_all() function
///2nd window drawing of all components x remain same but y increased by 100
///that will draw all components outside of top window
glPushMatrix();
glTranslated(tx, ty, 0);
draw_all_level3(0, 100);
glPopMatrix();///end of 2nd draw_all() function for animation
///translating 1st(left side) obstacle (x axis = tx) & (y axis = y)
/// y axis need not any translation because
glPushMatrix();
glTranslated(tx, y, 0);
obstracule3(0, 50);
glPopMatrix(); ///1st(left) obstacle translation ends
///translating 2nd(right side) obstacle (x axis = tx) & (y axis = yy)
glPushMatrix();
glTranslated(tx, yy, 0);
obstracule3(19, 130);
glPopMatrix(); ///2nd (right) obstacle translation ends
///translating Car (x axis = cx) & (y axis = cy)
glPushMatrix();
glTranslated(cx, cy, 0);
car(0, 0);
glPopMatrix(); ///car translate ends
///live score
score = score + 1;
glColor3f(1, 1, 1);
drawText("Score:", 41, 95);
itoa(score, buffer, 10);
drawTextNum(buffer, 52, 95);
if (gameover == true)
{
drawTextRed("Game Over", 45, 55);
drawTextRed("Press UP Arrow Key to play again", 33, 50);
score = -1;
glutSwapBuffers();
}
///end of live score
glFlush();
}
///draw text by passing parameter
void drawText(string ch, int xpos, int ypos)//draw the text for score and game over
{
int numofchar = ch.length();
int k;
k = 0;
glColor3f(1.0, 1.0, 1.0);
glRasterPos2f(xpos, ypos); //Specifies the raster position for pixel operations.
for (int i = 0; i <= numofchar - 2; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ch[i]);//font used here, may use other font also
}
}
void drawTextRed(string ch, int xpos, int ypos)//draw the text for score and game over
{
int numofchar = ch.length();
int k;
k = 0;
glColor3f(1.0, 0.0, 0.0);
glRasterPos2f(xpos, ypos);
for (int i = 0; i <= numofchar - 1; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ch[i]);//font used here, may use other font also
}
}
///draw score int type variable
void drawTextNum(string ch, int xpos, int ypos)//counting the score
{
int len;
int k;
k = 0;
len = ch.length();
glRasterPos2f(xpos, ypos);
for (int i = 0; i <= len - 1; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ch[k++]);
}
}
///function for controlling all the things with obstacle except car.
void controlAllexceptCar()
{
///checking 1st obstacle touch the car or not.
///if y(1st obstacle y axis) less than -67 then 2nd obstacle y axis(yy) must be greater than -97 hote hobe
///otherwise car will stop if y less than -67.
if ((y <= -67 && yy >= -97) && (cx >= -5 && cx <= 5))
{
glutIdleFunc(NULL);///infinity loop will stop because of NULL value
gameover = true;
}
///checking 2nd obstacle touch the car or not.
else if ((yy <= -147 && yy >= -177) && (cx >= 10 && cx <= 17))
{
glutIdleFunc(NULL);
gameover = true;
}
///control 1st and 2nd window animation(moving)
///1st window goes down and 2nd window appearing(repeating again and again)
///when ty-(y axis of draw_all() function) -
///- less than -100 then it set the value of(ty) to 0 for repeating this moving
/// 1st window ty=0 and 2nd window ty=0(where 1st window ty=100)
if (ty < -100) {
ty = 0;
}
else if (score < 500) {
glutDisplayFunc(display);
ty -= 0.10;
glutPostRedisplay();
}
else if (score < 1500) {
glutDisplayFunc(display_level2);
ty -= 2.5;
glutPostRedisplay();
}
else {
glutDisplayFunc(display_level3);
///decreasing value of ty that means windows goes down
///if the value is less than -100 then it will not Redisplay, go to if condition
ty -= 4.5000;
glutPostRedisplay();
}
///end of controlling 1st & 2nd window moving
///controlling 1st & 2nd obstacle
///if y axis(of 1st obstacle is less than -180(50+130) than y && yy will reset)
if (y < -180) {
yy = 0;
y = 0;
}
else {
y -= 1;
yy -= 1;
glutPostRedisplay();
}
///end of obstacle controlling
///end of controlAllexceptCar() function
}
void spe_key(int key, int x, int y)
{ ///controlling car with up left right
switch (key) {
case GLUT_KEY_UP:
gameover = false;
///set the global ideal callback
glutIdleFunc(controlAllexceptCar);
break;
///start controlling car moving
///left side move
case GLUT_KEY_LEFT:
if (cx > 0) {
cx -= 16;
glutPostRedisplay();
}
break;
///right side move
case GLUT_KEY_RIGHT:
if (cx < 16) {
cx += 16;
glutPostRedisplay();
}
break;
///End of car moving
default:
break;
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 700);
glutInitWindowPosition(300, 0);
glutCreateWindow("2-D RACING CAR");
init();
glutGetModifiers();
glutDisplayFunc(display);
glutSpecialFunc(spe_key);
glutMainLoop();
return 0;
}