-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriversEd.py
495 lines (465 loc) · 19.9 KB
/
DriversEd.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
import pygame
import math
# Initialize Pygame
pygame.init()
# Set the size of the window
screen = pygame.display.set_mode((950, 750))
#Game Over Messages
game_over_mess = ["Failure to Stop", "Failure to Follow Instructions", "Failure to Maintain Lane", "Failure to Use Turn Signal", "You Crashed"]
game_over_code = 0
# Set the title of the window
pygame.display.set_caption("Driver's Ed")
#game music
soundObj = pygame.mixer.Sound("song1.mp3")
soundObj.set_volume(0.3)
soundObj.play(-1)
# set font
FONT = pygame.font.SysFont("Arial", 20, bold=True)
TEXT_COLOR = (0, 0, 0)
start_time = pygame.time.get_ticks()
class game_start:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("title.png")
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class start_button:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("start_button.png")
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class Instruction:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("diagram.png")
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class Background:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("background.png")
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class Red_Zone:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("fail.png")
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class Stop_Zone:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("stop.png")
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class Car:
def __init__(self, x, y):
#initial speed, coords, image, friction, drag
self.x = x
self.y = y
self.speed = 0
self.angle = 90
self.friction = 0.05
self.original_image = pygame.image.load("car.png")
self.turn_left_image = pygame.image.load("car_light_left.png")
self.turn_right_image = pygame.image.load("car_light_right.png")
self.image = self.original_image.copy()
self.current_image = self.image
self.rect = self.image.get_rect()
self.turnSound = pygame.mixer.Sound("turnsignal.mp3")
self.turnSound.set_volume(1.2)
self.startSound = pygame.mixer.Sound("carstarting.mp3")
def update(self):
#friction
if self.speed > 0:
self.speed -= self.friction
elif self.speed < 0:
self.speed += self.friction
self.speed = max(min(self.speed,6),-6)
#rotation
self.y += math.sin(math.radians(self.angle)) * (self.speed)
self.x -= math.cos(math.radians(self.angle)) * (self.speed)
self.image = pygame.transform.rotate(self.current_image, self.angle)
self.rect = self.image.get_rect(center=(self.x, self.y))
def handle_keys(self):
#key presses and acceleration/rotation
keys = pygame.key.get_pressed()
if self.speed > 0.1 or self.speed < -0.1:
if keys[pygame.K_LEFT]:
self.angle += 2.5
if keys[pygame.K_RIGHT]:
self.angle -= 2.5
if keys[pygame.K_UP]:
self.speed -= 0.2
if keys[pygame.K_DOWN]:
self.speed += 0.2
if keys[pygame.K_w]:
self.current_image = self.original_image
self.image = pygame.transform.rotate(self.current_image, self.angle)
self.rect = self.image.get_rect(center=(self.x, self.y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_q:
self.current_image = self.turn_left_image
self.image = pygame.transform.rotate(self.current_image, self.angle)
self.rect = self.image.get_rect(center=(self.x, self.y))
self.turnSound.play()
if event.key == pygame.K_e:
self.current_image = self.turn_right_image
self.image = pygame.transform.rotate(self.turn_right_image, self.angle)
self.rect = self.image.get_rect(center=(self.x, self.y))
self.turnSound.play()
if event.key == pygame.K_UP:
self.startSound.set_volume(0.1)
self.startSound.play()
class AIcar:
def __init__(self, speed, waypoints, imgAngle, car, loop = False):
if car == "truck":
self.original_image = pygame.image.load("car_2.png")
self.turn_left_image = pygame.image.load("car_2_light_left.png")
self.turn_right_image = pygame.image.load("car_2_light_right.png")
else:
self.original_image = pygame.image.load("car_3.png")
self.turn_left_image = pygame.image.load("car_3_light_left.png")
self.turn_right_image = pygame.image.load("car_3_light_right.png")
self.image = self.original_image.copy()
self.current_image = self.image
self.rect = self.image.get_rect()
self.image = pygame.transform.rotate(self.current_image, imgAngle)
self.loop = loop
self.counter = 0
self.speed = speed
self.waypoints = waypoints
self.next_point = 0
self.current = pygame.math.Vector2(self.waypoints[0])
self.rect.center = self.current
#Sets end point if exists on list
self.tindex = 1
if self.tindex < len(self.waypoints) - 1:
self.target = pygame.math.Vector2(self.waypoints[self.tindex])
self.moving = True
else:
self.target = self.current
self.moving = False
def move(self):
if self.moving:
distance = self.current.distance_to(self.target)
if distance > self.speed:
self.current = self.current+(self.target-self.current).normalize()*self.speed
self.rect.center = self.current
else:
#Moves car to target and get new target from waypoints
self.current = self.target
self.rect.center = self.current
#Set next end point if exists on list
self.tindex += 1
if self.tindex < len(self.waypoints):
self.target = pygame.math.Vector2(self.waypoints[self.tindex])
else:
if self.loop:
self.tindex = 0
else:
self.moving = False
def instructions(num):
message = ["Stop, Turn-Signal, and Make a Left Turn", "Stop, Turn-Signal, and Make a Right Turn", "Stop and Go Straight"]
screen.blit(FONT.render(message[num], True, "black"), (20, 50))
diagram = Instruction(120, 650)
screen.blit(diagram.image, diagram.rect)
def start_screen():
waypoints = [(-100, 575), (450, 575), (1100, 575)]
aiCar = AIcar(4, waypoints, 270, "truck", False)
while True:
screen.blit(start.image, start.rect)
screen.blit(startButton.image, startButton.rect)
screen.blit(aiCar.image, aiCar.rect)
aiCar.move()
pygame.display.flip()
clock.tick(50)
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if startButton.rect.collidepoint(pygame.mouse.get_pos()):
startButton.image = pygame.image.load("start_button2.png")
else:
startButton.image = pygame.image.load("start_button.png")
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if startButton.rect.collidepoint(mouse_pos):
first_level()
def game_over(code, level):
level_fail_song = pygame.mixer.Sound("level_complete.mp3")
level_fail_song.set_volume(1)
level_fail_song.play()
grey_list = ['grey1', 'grey2', 'grey3', 'grey4', 'grey5', 'grey6', 'grey7', 'grey8', 'grey9', 'grey10',
'grey11', 'grey12', 'grey13', 'grey14', 'grey15', 'grey16', 'grey17', 'grey18', 'grey19',
'grey20', 'grey21', 'grey22', 'grey23', 'grey24', 'grey25', 'grey26', 'grey27', 'grey28',
'grey29', 'grey30', 'grey31', 'grey32', 'grey33', 'grey34', 'grey35', 'grey36', 'grey37',
'grey38', 'grey39', 'grey40', 'grey42', 'grey43', 'grey44', 'grey45', 'grey46', 'grey47',
'grey48', 'grey49', 'grey50', 'grey51', 'grey52', 'grey53', 'grey54', 'grey55', 'grey56',
'grey57', 'grey58', 'grey59', 'grey60', 'grey61', 'grey62', 'grey63', 'grey64', 'grey65',
'grey66', 'grey67', 'grey68', 'grey69', 'grey70', 'grey71', 'grey72', 'grey73', 'grey74',
'grey75', 'grey76', 'grey77', 'grey78', 'grey79', 'grey80', 'grey81', 'grey82', 'grey83',
'grey84', 'grey85', 'grey86', 'grey87', 'grey88', 'grey89', 'grey90', 'grey91', 'grey92',
'grey93', 'grey94', 'grey95', 'grey97', 'grey98', 'grey99']
for i in range(95, -1, -1):
pygame.draw.rect(screen, grey_list[i], pygame.Rect(0,0, screen.get_size()[0], screen.get_size()[1]))
pygame.display.flip()
pygame.time.delay(20)
message = ('GAME OVER: Level {} Failed'.format(level))
screen.blit(FONT.render(message, True, "white"), (375, 375))
reason = game_over_mess[code]
screen.blit(FONT.render(reason, True, "white"), (375, 400))
pygame.display.flip()
pygame.time.delay(3000)
first_level()
def win(time):
green_list = ['green4', 'green3', 'green2', 'green1']
for i in range(4):
pygame.draw.rect(screen, green_list[i], pygame.Rect(0,0, screen.get_size()[0], screen.get_size()[1]))
pygame.display.flip()
pygame.time.delay(10)
message = ('PASSED: In {} Seconds'.format(time))
screen.blit(FONT.render(message, True, "white"), (375, 375))
pygame.display.flip()
pygame.time.delay(3000)
pygame.quit()
def first_level():
#stop/zone flag
stopped = False
in_zone = False
player_car = Car(503, 710)
while True:
#frame rate is 50
clock.tick(50)
pygame.event.pump()
#blit fail zones
screen.blit(stop.image, stop.rect)
screen.blit(fail1.image, fail1.rect)
screen.blit(fail2.image, fail2.rect)
screen.blit(fail3.image, fail3.rect)
screen.blit(fail4.image, fail4.rect)
#blit background
screen.blit(background.image, background.rect)
#go straight instruction
instructions(2)
#blit car and update
player_car.handle_keys()
player_car.update()
screen.blit(player_car.image, player_car.rect)
keys = pygame.key.get_pressed()
if player_car.speed > 0.1 or player_car.speed < -0.1:
if keys[pygame.K_LEFT]:
if player_car.current_image != player_car.turn_left_image:
game_over(3,1)
if keys[pygame.K_RIGHT]:
if player_car.current_image != player_car.turn_right_image:
game_over(3,1)
#road direction win/lose
if player_car.rect.top < 0:
level_complete_song = pygame.mixer.Sound("level_complete.mp3")
level_complete_song.set_volume(1)
level_complete_song.play()
second_level()
elif player_car.rect.left > 850:
game_over(1, 1)
elif player_car.rect.left < 50:
game_over(1, 1)
elif player_car.rect.top > 735:
player_car.speed -= 0.2
#collisions
if player_car.rect.colliderect(fail1.rect) or player_car.rect.colliderect(fail2.rect) or player_car.rect.colliderect(fail3.rect) or player_car.rect.colliderect(fail4.rect):
game_over(2, 1)
# check if car is in zone
if player_car.rect.colliderect(stop.rect) and not in_zone:
in_zone = True
# check if car has left zone without stopping
elif not player_car.rect.colliderect(stop.rect) and in_zone and not stopped:
# car has left zone without stopping, game over
game_over(0, 1)
if not stopped:
if player_car.rect.colliderect(stop.rect):
if player_car.speed < 0.1 and player_car.speed > -0.1:
stopped = True
if start_time:
time_since_enter = (pygame.time.get_ticks() - start_time) / 1000
formatted_time = "{:.1f}".format(time_since_enter)
message = 'Timer: ' + str(formatted_time) + ' seconds'
screen.blit(FONT.render(message, True, "black"), (20, 20))
pygame.display.flip()
def second_level():
#stop/zone flag
stopped = False
in_zone = False
player_car = Car(503, 710)
waypoints = [(450, -100), (450, 400), (450, 1100)]
aiCar = AIcar(2, waypoints, 180, "truck", False)
while True:
clock.tick(50)
pygame.event.pump()
#blit fail zones
screen.blit(stop.image, stop.rect)
screen.blit(fail1.image, fail1.rect)
screen.blit(fail2.image, fail2.rect)
screen.blit(fail3.image, fail3.rect)
screen.blit(fail4.image, fail4.rect)
#blit background
screen.blit(background.image, background.rect)
instructions(0)
pauseTime = 20
minStop = 270
maxStop = 300
if((minStop > aiCar.current[1] or aiCar.current[1] > maxStop) or aiCar.counter >= pauseTime):
aiCar.move()
screen.blit(aiCar.image, aiCar.rect)
if(minStop <= aiCar.current[1] and aiCar.current[1] <= maxStop and aiCar.counter < pauseTime):
aiCar.counter += 1
aiCar.waypoints.append(((maxStop - 5),457))
screen.blit(aiCar.image, aiCar.rect)
if(aiCar.counter == pauseTime - 1):
aiCar.waypoints = [(457,400), (457, 1100), (457, 1200)]
screen.blit(aiCar.image, aiCar.rect)
if((1050 < aiCar.current[1] and aiCar.current[1] < 1100)):
aiCar = AIcar(2, waypoints, 270, False)
player_car.handle_keys()
player_car.update()
screen.blit(player_car.image, player_car.rect)
# checks if player crosses a certain point on map, can be used to translate to level two
keys = pygame.key.get_pressed()
if player_car.speed > 0.1 or player_car.speed < -0.1:
if keys[pygame.K_LEFT]:
if player_car.current_image != player_car.turn_left_image:
game_over(3,2)
if keys[pygame.K_RIGHT]:
if player_car.current_image != player_car.turn_right_image:
game_over(3,2)
if player_car.rect.left < 50:
level_complete_song = pygame.mixer.Sound("level_complete.mp3")
level_complete_song.set_volume(1)
level_complete_song.play()
third_level()
elif player_car.rect.left > 850:
game_over(1, 2)
elif player_car.rect.top < 0:
game_over(1, 2)
if player_car.rect.colliderect(fail1.rect) or player_car.rect.colliderect(fail2.rect) or player_car.rect.colliderect(fail3.rect) or player_car.rect.colliderect(fail4.rect):
game_over(2, 2)
if player_car.rect.colliderect(aiCar.rect):
car_crash_song = pygame.mixer.Sound("car_crash.mp3")
car_crash_song.set_volume(1)
car_crash_song.play()
game_over(4, 2)
# check if car is in zone
if player_car.rect.colliderect(stop.rect) and not in_zone:
in_zone = True
# check if car has left zone without stopping
elif not player_car.rect.colliderect(stop.rect) and in_zone and not stopped:
# car has left zone without stopping, game over
game_over(0, 2)
if not stopped:
if player_car.rect.colliderect(stop.rect):
if player_car.speed < 0.1 and player_car.speed > -0.1:
stopped = True
if start_time:
time_since_enter = (pygame.time.get_ticks() - start_time) / 1000
formatted_time = "{:.1f}".format(time_since_enter)
message = 'Timer: ' + str(formatted_time) + ' seconds'
screen.blit(FONT.render(message, True, TEXT_COLOR), (20, 20))
pygame.display.flip()
def third_level():
#stop/zone flag
stopped = False
in_zone = False
player_car = Car(503, 710)
time = 0
waypoints = [(-100, 422), (400, 422), (1100, 422)]
aiCar = AIcar(4, waypoints, 270, "truck", False)
waypoints2 = [(-500, 415), (400, 415), (1100, 415)]
aiCar2 = AIcar(4, waypoints2, 270, "Modern", False)
while True:
clock.tick(50)
pygame.event.pump()
#blit fail zones
screen.blit(stop.image, stop.rect)
screen.blit(fail1.image, fail1.rect)
screen.blit(fail2.image, fail2.rect)
screen.blit(fail3.image, fail3.rect)
screen.blit(fail4.image, fail4.rect)
screen.blit(background.image, background.rect)
screen.blit(aiCar.image, aiCar.rect)
aiCar.move()
aiCar2.move()
screen.blit(aiCar2.image, aiCar2.rect)
instructions(1)
if((1050 < aiCar.current[0] and aiCar.current[0] < 1100)):
aiCar = AIcar(4, waypoints, 270, "truck", False)
if((1050 < aiCar2.current[0] and aiCar2.current[0] < 1100)):
aiCar2 = AIcar(4, waypoints2, 270, "Modern", False)
player_car.handle_keys()
player_car.update()
screen.blit(player_car.image, player_car.rect)
# checks if player crosses a certain point on map, can be used to translate to level two
keys = pygame.key.get_pressed()
if player_car.speed > 0.1 or player_car.speed < -0.1:
if keys[pygame.K_LEFT]:
if player_car.current_image != player_car.turn_left_image:
game_over(3,1)
if keys[pygame.K_RIGHT]:
if player_car.current_image != player_car.turn_right_image:
game_over(3,1)
if player_car.rect.left > 850:
win_song = pygame.mixer.Sound("game_win.mp3")
win_song.set_volume(1)
win_song.play()
win(formatted_time)
elif player_car.rect.left < 50:
game_over(1, 3)
elif player_car.rect.top < 0:
game_over(1, 3)
if player_car.rect.colliderect(fail1.rect) or player_car.rect.colliderect(fail2.rect) or player_car.rect.colliderect(fail3.rect) or player_car.rect.colliderect(fail4.rect):
game_over(2, 3)
# check if car is in zone
if player_car.rect.colliderect(stop.rect) and not in_zone:
in_zone = True
# check if car has left zone without stopping
elif not player_car.rect.colliderect(stop.rect) and in_zone and not stopped:
# car has left zone without stopping, game over
game_over(0, 3)
if not stopped:
if player_car.rect.colliderect(stop.rect):
if player_car.speed < 0.1 and player_car.speed > -0.1:
stopped = True
if start_time:
time_since_enter = (pygame.time.get_ticks() - start_time) / 1000
formatted_time = "{:.1f}".format(time_since_enter)
message = 'Timer: ' + str(formatted_time) + ' seconds'
screen.blit(FONT.render(message, True, TEXT_COLOR), (20, 20))
pygame.display.flip()
#start screen
start = game_start(475,375)
#start button
startButton = start_button(475,450)
#background
background = Background(475,375)
#fail zones
fail1 = Red_Zone(190,110)
fail2 = Red_Zone(800,160)
fail3 = Red_Zone(160,580)
fail4 = Red_Zone(820,620)
#stop zone
stop = Stop_Zone(500,500)
#clock
clock = pygame.time.Clock()
#start game screen
start_screen()
#quit sequence
pygame.quit()