-
Notifications
You must be signed in to change notification settings - Fork 0
/
The_Final_Draft.py
600 lines (489 loc) · 17.1 KB
/
The_Final_Draft.py
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
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import winsound
#from playsound import playsound
from math import sin, cos, pi
import random
#DRIVE UNTIL DEATH GAME(GROUP -10)
winsound.PlaySound("Running in the 90s.wav",
winsound.SND_ASYNC + winsound.SND_LOOP)
#window size
WINDOW_WIDTH = 700
WINDOW_HEIGHT = 700
#collision variables
collision = False
get = False
collision2 = False
collision3 = False
#points
point_x = random.randint(10, 80)
point_y = 105
y_position = 105
#tree
circle1_y = 105
circle2_y = 105
circle_x = random.randint(10, 80)
circle_speed = 0.5
trunk = 96
seq = random.randint(1, 5)
dis = False #obstacle dissappear
dis2 = False #diamond dissappear
dis3 = False
dis4 = False
speed = 0.38
diamond_speed = 0.45
obstacles_speed = 0.78
car_position = 51
car_movement_direction = 0
game_paused = False
game_reset = False
stop = True
p_speed = 0
p_cir_speed = 0
lives = 3
var = 1
p_pts_speed = 0
point_speed = 0.5
num_points = 5
points = []
score = 0
#Storing points coordinates
for i in range(num_points):
point_x = random.uniform(23, 80)
point_y = random.uniform(150, 110)
points.append({'x': point_x, 'y': point_y, 'speed': point_speed})
class AABB:
x = 0
y = 0
w = 0
h = 0
def __init__(self, x, y, w, h):
self.x, self.y, self.w, self.h = x, y, w, h
def collides_with(self, other):
return (self.x < other.x + other.w and
self.x + self.w > other.x and
self.y < other.y + other.h and
self.y + self.h > other.y)
#obstacle collision condition
def collides_with_sq_obstacle(car_pos, car_width, car_height, obstacles):
return (car_pos - 5 < obstacles.x + obstacles.w and
car_pos + car_width > obstacles.x and
20 < obstacles.y + obstacles.h and
20 + car_height > obstacles.y)
def collides_with_car(car_pos, car_width, car_height, point):
car_aabb = AABB(car_pos - car_width / 2, 20 - car_height / 2, car_width, car_height)
point_aabb = AABB(point['x'], point['y'], 1, 1)
return car_aabb.collides_with(point_aabb)
def collides_with_point(car_pos, car_width, car_height, pts):
return (car_pos - 5 == pts.x + pts.w and
car_pos + car_width > pts.x and
20 < pts.y + pts.h and
20 + car_height > pts.y)
#Midpoint Line
def draw_line(x1, y1, x2, y2):
glBegin(GL_POINTS)
dx = abs(x2 - x1)
dy = abs(y2 - y1)
if x1 < x2:
step_x = 1
else:
step_x = -1
if y1 < y2:
step_y = 1
else:
step_y = -1
x = x1
y = y1
cnt = 0
if dx > dy:
p = 2 * dy - dx
while dx > cnt:
glVertex2f(x, y)
x += step_x
if p >= 0:
y += step_y
p -= 2 * dx
p += 2 * dy
cnt += 1
else:
p = 2 * dx - dy
while dy > cnt:
glVertex2f(x, y)
y += step_y
if p >= 0:
x += step_x
p -= 2 * dy
p += 2 * dx
cnt += 1
glEnd()
#Midpoint Circle
def draw_circle(cx, cy, radius):
d = 1 - radius
x = 0
y = radius
circle_pts2(x, y, cx, cy)
while x < y:
if d < 0:
d = d + 2 * x + 3
else:
d = d + 2 * x - 2 * y + 5
y -= 1
x += 1
circle_pts2(x, y, cx, cy)
def mid_circle(cx, cy, radius):
d = 1 - radius
x = 0
y = radius
circle_pts(x, y, cx, cy)
while x < y:
if d < 0:
d = d + 2 * x + 3
else:
d = d + 2 * x - 2 * y + 5
y -= 1
x += 1
circle_pts(x, y, cx, cy)
def circle_pts(x, y, cx, cy):
glPointSize(50)
glBegin(GL_POINTS)
glVertex2f(x + cx, y + cy)
glVertex2f(y + cx, x + cy)
glVertex2f(y + cx, -x + cy)
glVertex2f(x + cx, -y + cy)
glVertex2f(-x + cx, -y + cy)
glVertex2f(-y + cx, -x + cy)
glVertex2f(-y + cx, x + cy)
glVertex2f(-x + cx, y + cy)
glEnd()
# def collides_with_sq_obstacle(car_pos,car_width,car_height, obstacles):
def draw_point(x, y):
glEnable(GL_POINT_SMOOTH)
glPointSize(18.0)
glBegin(GL_POINTS)
glVertex2f(x, y)
glEnd()
glDisable(GL_POINT_SMOOTH)
def circle_pts2(x, y, cx, cy):
num_segments = 100 # Increase this value for a smoother circle
glBegin(GL_POINTS)
for i in range(num_segments):
theta = 2.0 * pi * i / num_segments
glVertex2f(x * cos(theta) + cx, y * sin(theta) + cy )
glEnd()
def draw_car(car_position, car_width, car_height):
car_y = 20 # Fixed y-position for the car in the middle
# Draw wheels
wheel_radius = car_height / 4 # Adjust the wheel size as needed
wheel_y = car_y - car_height / 2 - wheel_radius # Place wheels at the bottom of the car
wheel_positions = [-car_width / 3, car_width / 3]
glColor3f(0.0, 0.0, 0.0) # Black color for the wheels
glBegin(GL_POINTS)
for wheel_position in wheel_positions:
draw_wheel(car_position + wheel_position, wheel_y, wheel_radius)
glEnd()
glColor3f(1.0, 0.0, 0.0) # Red color for the car
glPointSize(9.0)
glBegin(GL_POINTS)
glEnd()
# Draw car body
for x in range(int(car_position - car_width / 2), int(car_position + car_width / 2)):
for y in range(int(car_y - car_height / 2), int(car_y + car_height / 2)):
glPointSize(9)
glBegin(GL_POINTS)
glVertex2f(x, y)
glEnd()
def draw_wheel(cx, cy, radius):
for i in range(360):
angle = i * 3.14159 / 180
x = cx + radius * 0.2 * cos(angle)
y = cy + radius * 0.7 * sin(angle)
glVertex2f(x-0.95, y+11)
glVertex2f(x-0.95, y+3)
def draw_obstacles(obstacles):
glColor3f(1.0, 0.0, 0.0)
glPointSize(10)
glBegin(GL_POINTS)
glEnd()
draw_line(obstacles.x, obstacles.y, obstacles.x + obstacles.w , obstacles.y)
draw_line(obstacles.x, obstacles.y, obstacles.x , obstacles.y+obstacles.h)
draw_line(obstacles.x, obstacles.y+obstacles.h, obstacles.x + obstacles.w , obstacles.y+obstacles.h)
draw_line(obstacles.x+ obstacles.w, obstacles.y+obstacles.h, obstacles.x + obstacles.w , obstacles.y)
con = obstacles.x
for i in range(10):
con +=1
draw_line(con, obstacles.y, con, obstacles.y+obstacles.h)
def draw_solid_diamond(diamond):
glColor3f(0.0, 1.0, 0.0) # Set color to green
glBegin(GL_POINTS)
con = diamond.x
half_width = diamond.w // 2
half_height = diamond.h // 2
for i in range(half_height):
for j in range(half_width - i, half_width + i + 1):
# Draw two symmetrical points at a time
glVertex2f(con + j, diamond.y + half_height + i)
glVertex2f(con + j, diamond.y + half_height - i)
glEnd()
#text function
def render_text(x, y, text,r,g,b):
glColor3f(r, g, b) # Set text color to white
glRasterPos2f(x, y)
for character in text:
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord(character))
def display():
global y_position, score, speed, circle1_y, circle2_y, point_speed, circle_speed, diamond, circle_x, seq, car_position, game_paused, obstacles_speed, collision, var, dis, lives, point_y, point_x, trunk, points
glClear(GL_COLOR_BUFFER_BIT)
# Draw road
glColor3f(0.41, 0.41, 0.41)
draw_line(80, 10, 80, 100)
draw_line(80, 10, 80, 90)
draw_line(80, -5, 80, 80)
draw_line(20, 10, 20, 100)
draw_line(20, 10, 20, 90)
draw_line(20, -5, 20, 80)
glColor3f(0.41, 0.41, 0.41)
glPointSize(10)
glBegin(GL_POINTS)
for x in range(0, 110):
for y in range(20, 81):
glVertex2f(y, x)
glEnd()
# Draw circles with updated y-coordinates
glColor3f(0.0, 1.0, 0.0) # Green color for the circles
#glEnable(GL_POINT_SMOOTH)
m = 0
n = 0
for i in range(seq):
glColor3f(0.0, 1.0, 0.0)
mid_circle(10, circle1_y+m, 4)
glColor3f(0.545, 0.271, 0.075)
glPointSize(20)
draw_line(10, trunk+n, 10, trunk+7+n)
glColor3f(0.0, 1.0, 0.0)
mid_circle(90, circle1_y+m, 4)
glColor3f(0.545, 0.271, 0.075)
glPointSize(20)
draw_line(90, trunk+n, 90, trunk+7+n)
m+=20
n+=20
#draw_circle(circle_x, circle2_y, 2)
# Draw moving line
glColor3f(1.0, 1.0, 1.0)
glPointSize(7.5)
#draw_line(y_position, 50, y_position + 11, 50)
draw_line(50, y_position, 50, y_position + 30)
#draw obstacles
draw_obstacles(obstacles)
draw_obstacles(obstacles2)
draw_obstacles(obstacles3)
# Draw moving points
glColor3f(1.0, 0.843, 0.0) #golden color
for point in points:
draw_point(point['x'], point['y'])
# Check for collision with car
if collides_with_car(car_position, 5, 10, point):
print("Point collected!")
speed+=0.025
circle_speed+=0.005
point_speed+=0.005
score+=1
point['y'] = 105
point['x'] = random.uniform(23, 80)
#draw diamond
if game_paused == False and lives != 0:
draw_solid_diamond(diamond)
# Draw car
glPointSize(12)
draw_car(car_position, 5, 10)
# Draw "Lives" information text
glColor3f(1.0, 1.0, 1.0)
render_text(5, 10, f"Lives: {lives}",0.0, 0.0, 0.0)
render_text(5, 90, f"Score: {score}",0.0, 0.0, 0.0)
# Check if the game is over and display "Game Over" continuously
if lives == 0:
glColor3f(1.0, 0.0, 0.0) # Set text color to white
render_text(43, 50, "Game Over",0.0, 1.0, 1.0)
glColor3f(1.0, 1.0, 1.0)
render_text(43, 45, f"Your Final Score: {score}!",1.0, 1.0, 1.0)
glutSwapBuffers()
def animation():
global y_position,stop, speed, point_speed,diamond_speed, circle1_y, circle2_y, circle_speed, circle_x, seq, car_position, dis3, dis4, game_paused, obstacles_speed, dis2, collision, collision2, collision3, var, dis, lives, point_y, point_x, trunk
if stop:
if game_paused == False: # Check if the game is not paused
circle1_y -= circle_speed
circle2_y -= circle_speed
trunk -= circle_speed
# Reset circles to the top if they reach the bottom
if trunk < -100:
trunk = 105
if circle1_y < -100:
circle1_y = 105
seq = random.randint(1, 5)
if circle2_y < -300:
circle2_y = 105
circle_x = random.randint(30, 70)
# Update the y-coordinate for each moving point
if game_paused == False and lives != 0:
for point in points:
point['y'] -= point['speed']
if point['y'] < -13:
point['y'] = 105
point['x'] = random.uniform(23, 80)
#point_y = random.uniform(20, 80)
# Update the y-coordinate for the animation
if game_paused == False:
y_position -= speed
if y_position < -25:
y_position = 105
#OBSTACLES animation
obstacles.y -= speed
obstacles2.y -= speed
obstacles3.y -= speed
if obstacles.y < 0 or dis == True:
# once_neg = True
# felled = True
# obstacles_speed += .01
obstacles.x = random.randint(20, 70)
obstacles.y = 105
dis = False
if obstacles2.y < 0 or dis3 == True:
# once_neg = True
# felled = True
# obstacles_speed += .01
obstacles2.x = random.randint(20, 70)
obstacles2.y = 105
dis3 = False
if obstacles3.y < 0 or dis4 == True:
# once_neg = True
# felled = True
# obstacles_speed += .01
obstacles3.x = random.randint(20, 70)
obstacles3.y = 105
dis4 = False
#diamond_animation
diamond.y-= diamond_speed
diamon_dis = -2000
if diamond.y < diamon_dis or dis2 == True:
# once_neg = True
# felled = True
# obstacles_speed += .01
diamond.x = random.randint(20, 70)
diamond.y = 1000
dis2 = False
get = collides_with_sq_obstacle(car_position, 4, 8, diamond)
if get:
lives +=1
diamon_dis = diamon_dis*2
print("gotchaaaa!")
get = False
dis2 = True
collision = collides_with_sq_obstacle(car_position, 4, 8, obstacles)
collision2 = collides_with_sq_obstacle(car_position, 4, 8, obstacles2)
collision3 = collides_with_sq_obstacle(car_position, 4, 8, obstacles3)
if collision == True:
#print(var)
#var +=1
collision = False
dis = True
lives-=1
print("Ughhh!!")
if lives == 0:
print("Game Over")
winsound.PlaySound(None, 0)
# speed = 0
# circle_speed = 0
# point_speed = 0
# diamond_speed = 0
stop = False
else:
print(f"Lives remaining {lives}")
if collision2 == True:
#render_text(50, 50, f"It's your lucky day mate ('-')")
collision2 = False
dis3 = True
print("It's your lucky day mate ('-')")
if collision3 == True:
#render_text(50, 50, f"It's your lucky day mate ('-')")
collision3 = False
dis4 = True
print("It's your lucky day mate ('-')")
glutPostRedisplay()
def key_action(key, x, y):
global car_position, game_paused, lives,stop
# Adjust the car position based on the arrow keys
if game_paused == False and lives!=0:
if key == GLUT_KEY_LEFT and car_position > 24:
car_position -= 1.5
#print('l', car_position)
elif key == GLUT_KEY_RIGHT and car_position < 78:
car_position += 1.5
#print('r', car_position)
glutPostRedisplay()
def mouse_action(button, state, x, y):
global game_paused, speed, circle_speed, point_speed, p_speed, p_cir_speed, p_pts_speed, game_reset, score, diamond_speed
global point_x, point_y, y_position, circle1_y, circle2_y, circle_x, trunk, lives, obstacles, obstacles2, obstacles3, stop
if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN and lives!=0: #pause the game
game_paused = not game_paused
if game_paused == True:
# if stop == True:
# p_speed = speed
# p_cir_speed = circle_speed
# p_pts_speed = point_speed
# speed = 0
# circle_speed = 0
# point_speed = 0
# diamond_speed = 0
stop = False
else:
stop = True
# speed = p_speed
# circle_speed = p_cir_speed
# point_speed = p_pts_speed
# p_speed = 0
# p_cir_speed = 0
# p_pts_speed = 0
# diamond_speed = 0.45
if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN: #restart the game
winsound.PlaySound("Running in the 90's.wav",
winsound.SND_ASYNC + winsound.SND_LOOP)
stop = True
speed = 0.4
circle_speed = 0.5
point_speed = 0.5
point_x = random.randint(10, 80)
point_y = 105
y_position = 105
circle1_y = 105
circle2_y = 105
circle_x = random.randint(10, 80)
diamond.y = 1000
lives = 3
trunk = 96
score = 0
obstacles = AABB(random.randint(20, 70), 100-10, 10, 10)
obstacles2 = AABB(random.randint(20, 70), 100-10, 10, 10)
obstacles3 = AABB(random.randint(20, 70), 100-10, 10, 10)
obstacles = AABB(random.randint(20, 70), 100-10, 10, 10)
obstacles2 = AABB(random.randint(20, 70), 100-10, 10, 10)
obstacles3 = AABB(random.randint(20, 70), 100-10, 10, 10)
#obstacles_speed = 1
diamond = AABB(x=40, y=1000, w=8, h=8)
def init():
glClearColor(1, 0.76, 0.5, 1.75)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, 100, 0, 100) # Adjust the coordinates as needed
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT)
glutCreateWindow(b"DRIVE UNTIL DEATH")
init()
glutDisplayFunc(display)
glutSpecialFunc(key_action)
glutMouseFunc(mouse_action)
glutIdleFunc(animation)
glutMainLoop()
if __name__ == "__main__":
main()