-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
641 lines (538 loc) · 22.3 KB
/
main.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
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
import pygame
import random
from pygame import mixer
pygame.init()
# create a screen
screen = pygame.display.set_mode((600, 600))
# Title and Icon
pygame.display.set_caption("Cars!")
icon = pygame.image.load('Menu design/001-racing-car.png')
pygame.display.set_icon(icon)
# background
background = pygame.image.load('Menu design/background image.png')
# game map
game_map = pygame.image.load('Level maps/game map.png')
# background music
mixer.music.load('Sound/got.mp3')
mixer.music.play(-1)
# score variables
score = 0
# difficulty variables
basic = True
easy = False
medium = False
hard = False
extreme = False
# general variables
next_page_arrow = pygame.image.load('Menu design/next page arrow.png')
go_back_arrow = pygame.image.load('Menu design/go back arrow.png')
no_collide = True
game_start_screen = True
avatar_screen = False
avatar_screen_two = False
tutorial_screen = False
game_screen = False
# colour variables
purple = (221, 160, 221)
black = (0, 0, 0)
grey = (100, 100, 100)
red = (140, 0, 0)
blue = (16, 47, 87)
green = (28, 77, 28)
orange = (201, 59, 12)
grey_for_car = (87, 87, 87)
purple_for_car = (100, 13, 166)
colour_of_tank = (61, 122, 61)
current_colour = red
# Fonts and text
my_font = pygame.font.Font('Font/BACKTO1982.TTF', 32)
text_surface = my_font.render('Down Town Drift', False, (255, 255, 255))
my_font_start = pygame.font.Font('Font/BACKTO1982.TTF', 20)
small_font = pygame.font.Font('Font/BACKTO1982.TTF', 10)
text_avatar = my_font_start.render('Press A to view Avatar', False, (255, 255, 255))
text_tutorial = my_font_start.render('Press T to start Tutorial', False, (255, 255, 255))
text_start = my_font_start.render('Press SPACE to start Game', False, (255, 255, 255))
avatar_title = my_font.render('Avatar', False, (255, 255, 255))
start_esc = my_font_start.render('ESC', False, (255, 255, 255))
tutorial_title = my_font.render('Tutorial', False, (255, 255, 255))
game_title = my_font.render('Game', False, (255, 255, 255))
tutorial_text_1 = my_font_start.render('BAD - LOSE HP', False, (255, 255, 255))
tutorial_text_2 = my_font_start.render('GET COINS $$$', False, (255, 255, 255))
# loading bar variables
sprites = [pygame.image.load('Animation/Loading screen/pixil-frame-0.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-1.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-2.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-3.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-4.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-5.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-6.png'),
pygame.image.load('Animation/Loading screen/pixil-frame-7.png')]
counter = 0
money_adder_counter = 0
tank_sprites = [pygame.image.load('Animation/Tank/tank-frame-0.png'),
pygame.image.load('Animation/Tank/tank-frame-1.png')]
tank_counter = 0
# car
red_car = pygame.image.load('Cars/red car.png')
blue_car = pygame.image.load('Cars/blue car.png')
green_car = pygame.image.load('Cars/green car.png')
orange_car = pygame.image.load('Cars/orange car.png')
purple_car = pygame.image.load('Cars/purple car.png')
grey_car = pygame.image.load('Cars/grey car.png')
tank = pygame.image.load('Cars/2s3m.png')
current_car = red_car
car_x = 10
car_y = 245
HP = 350
car_collide = False
# shop variables:
total_money = 0
coins_gained = 0
small_coin_pic = pygame.image.load("Menu design/small_coin.png")
# obstacle variables
oil = pygame.image.load('Obstacles/oil leak.png')
cone = pygame.image.load('Obstacles/cone.png')
barrel = pygame.image.load('Obstacles/barrel.png')
coin_pic = pygame.image.load('Obstacles/coin.png')
obstacle_speed = 2.4
obstacle0_x = 500
obstacle1_x = 750
obstacle2_x = 1150
obstacle0_y = 125
obstacle1_y = 275
obstacle2_y = 425
coin_x = 3000
coin_y = 275
coin_hit = False
coin_on_map = False
class Button:
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False
def draw(self):
action = False
# finding mouse position
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
button_sound = mixer.Sound('Sound/button sound.wav')
button_sound.play()
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
screen.blit(self.image, (self.rect.x, self.rect.y))
return action
tank_button = Button(80, 260, tank)
red_car_button = Button(80, 260, red_car)
blue_car_button = Button(240, 260, blue_car)
green_car_button = Button(400, 260, green_car)
orange_car_button = Button(80, 400, orange_car)
purple_car_button = Button(240, 400, purple_car)
grey_car_button = Button(400, 400, grey_car)
next_page_button = Button(480, 9, next_page_arrow)
go_back_button = Button(15, 5, go_back_arrow)
def game_over():
with open('Data/highscore.txt', 'r+') as file:
try:
high_score = int(file.read())
except ValueError:
high_score = 0
death_message = my_font.render('...You Died...', False, (255, 255, 255))
if high_score < score:
high_score = score
with open('Data/highscore.txt', 'r+') as file:
file.write(str(high_score))
high_score_message = my_font.render(f'High score: {str(high_score)}', False, (255, 255, 255))
money_gained_message = my_font.render(f'Money gained: {coins_gained}', False, (255, 255, 255))
screen.blit(death_message, (50, 50))
screen.blit(high_score_message, (50, 150))
screen.blit(money_gained_message, (50, 250))
def loading():
global counter
loading_message = my_font.render('Loading', False, (255, 255, 255))
pygame.draw.rect(screen, grey, pygame.Rect(195, 108, 220, 60))
pygame.draw.rect(screen, purple, pygame.Rect(195, 108, 220, 60), 3)
screen.blit(loading_message, (208, 118))
current_sprite = sprites[int(counter)]
if counter < len(sprites):
screen.blit(current_sprite, (110, 200))
if counter + 1 > len(sprites):
counter = 0
counter += 0.1
def fade_screen_game_over(width, height):
fade_game_over = pygame.Surface((width, height))
fade_game_over.fill(black)
for alpha in range(0, 300):
fade_game_over.set_alpha(alpha)
screen.blit(fade_game_over, (0, 0))
game_over()
pygame.display.update()
pygame.time.delay(5)
def fade_screen_loading(width, height):
fade_loading = pygame.Surface((width, height))
fade_loading.fill(black)
for alpha in range(0, 300):
fade_loading.set_alpha(alpha)
screen.blit(fade_loading, (0, 0))
loading()
pygame.display.update()
def start_screen():
global money_adder_counter
money_adder_counter = 0
pygame.draw.rect(screen, black, pygame.Rect(90, 38, 423, 60))
pygame.draw.rect(screen, purple, pygame.Rect(90, 38, 423, 60), 3)
screen.blit(text_surface, (100, 50))
pygame.draw.rect(screen, black, pygame.Rect(90, 300, 435, 45))
pygame.draw.rect(screen, purple, pygame.Rect(90, 300, 435, 45), 3)
screen.blit(text_start, (100, 310))
pygame.draw.rect(screen, black, pygame.Rect(90, 500, 432, 45))
pygame.draw.rect(screen, purple, pygame.Rect(90, 500, 432, 45), 3)
screen.blit(text_tutorial, (100, 510))
pygame.draw.rect(screen, black, pygame.Rect(90, 400, 378, 45))
pygame.draw.rect(screen, purple, pygame.Rect(90, 400, 378, 45), 3)
screen.blit(text_avatar, (100, 410))
def game():
global total_money
global coin_x
global coin_y
global current_car
global car_collide
global HP
global game_start_screen
global game_screen
global no_collide
global obstacle0_x
global obstacle1_x
global obstacle2_x
global obstacle0_y
global obstacle1_y
global obstacle2_y
global obstacle_speed
global score
global coin_hit
global coin_on_map
global coins_gained
screen.blit(game_map, (0, 0))
pygame.draw.rect(screen, black, pygame.Rect(535, 535, 60, 60))
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(535, 535, 60, 60), 3)
screen.blit(small_coin_pic, (540, 540))
money_text = my_font_start.render(f'{coins_gained}', False, (255, 255, 255))
screen.blit(money_text, (548, 540))
# HEALTH BAR #
if HP == 350:
pygame.draw.rect(screen, current_colour, pygame.Rect(20, 540, HP, 45))
pygame.draw.rect(screen, black, pygame.Rect(20, 540, 350, 45), 3)
user = pygame.Rect(car_x, car_y, 110, 110)
coin = pygame.Rect(coin_x, coin_y, 50, 50)
obstacle0 = pygame.Rect(obstacle0_x, obstacle0_y, 50, 50)
obstacle1 = pygame.Rect(obstacle1_x, obstacle1_y, 50, 50)
obstacle2 = pygame.Rect(obstacle2_x, obstacle2_y, 50, 50)
screen.blit(oil, (obstacle0_x, obstacle0_y))
screen.blit(cone, (obstacle1_x, obstacle1_y))
screen.blit(barrel, (obstacle2_x, obstacle2_y))
if no_collide:
obstacle0_x -= obstacle_speed
obstacle1_x -= obstacle_speed
obstacle2_x -= obstacle_speed
if obstacle0_x < -70:
obstacle0_x = random.randrange(1200, 2000, 400)
obstacle0_y = random.randrange(125, 525, 150)
score += 1
if score >= 10:
obstacle_speed += 0.1
if obstacle1_x < -70:
obstacle1_x = obstacle0_x + 300
obstacle1_y = random.randrange(125, 525, 150)
score += 1
if score >= 10:
obstacle_speed += 0.1
if obstacle2_x < -70:
obstacle2_x = obstacle1_x + 600
obstacle2_y = random.randrange(125, 525, 150)
score += 1
if score >= 10:
obstacle_speed += 0.1
if score % 10 == 0 or coin_on_map:
if coin.colliderect(obstacle0) or coin.colliderect(obstacle1) or coin.colliderect(obstacle2):
coin_x = coin_x + 500
screen.blit(coin_pic, (coin_x, coin_y))
coin_x -= obstacle_speed
coin_on_map = True
if coin_x < -70:
coin_x = obstacle2_x + 900
coin_y = random.randrange(125, 525, 150)
coin_on_map = False
if user.colliderect(obstacle0) or user.colliderect(obstacle1) or user.colliderect(obstacle2):
pygame.draw.rect(screen, current_colour, pygame.Rect(20, 540, HP, 45))
pygame.draw.rect(screen, black, pygame.Rect(20, 540, 350, 45), 3)
if current_car == tank:
HP -= obstacle_speed / 6
else:
HP -= obstacle_speed / 0.6
if user.colliderect(coin) and not coin_hit:
total_money += 1
coins_gained += 1
coin_x = obstacle2_x + 900
coin_y = random.randrange(125, 525, 150)
coin_hit = True
coin_on_map = False
if not user.colliderect(coin):
coin_hit = False
if HP <= 0:
no_collide = False
game_screen = False
game_start_screen = True
fade_screen_game_over(600, 600)
pygame.draw.rect(screen, current_colour, pygame.Rect(20, 540, HP, 45))
pygame.draw.rect(screen, black, pygame.Rect(20, 540, 350, 45), 3)
# practice game with obstacles that can get hit 3 times before restart until the "basic level" in the real game
def tutorial():
pygame.draw.rect(screen, black, pygame.Rect(187, 27, 240, 55))
pygame.draw.rect(screen, purple, pygame.Rect(187, 27, 240, 55), 4)
screen.blit(tutorial_title, (195, 35))
pygame.draw.rect(screen, black, pygame.Rect(48, 30, 75, 40))
pygame.draw.rect(screen, purple, pygame.Rect(48, 30, 75, 40), 4)
screen.blit(start_esc, (58, 38))
pygame.draw.rect(screen, grey, pygame.Rect(30, 130, 540, 430))
pygame.draw.rect(screen, purple, pygame.Rect(30, 130, 540, 430), 5)
pygame.draw.line(screen, purple, (300, 130), (300, 559), 4)
pygame.draw.rect(screen, red, pygame.Rect(40, 140, 253, 200))
pygame.draw.rect(screen, black, pygame.Rect(40, 140, 253, 200), 4)
screen.blit(tutorial_text_1, (60, 170))
pygame.draw.rect(screen, green, pygame.Rect(40, 350, 253, 200))
pygame.draw.rect(screen, black, pygame.Rect(40, 350, 253, 200), 4)
screen.blit(tutorial_text_2, (60, 380))
screen.blit(oil, (50, 225))
screen.blit(cone, (130, 225))
screen.blit(barrel, (210, 225))
screen.blit(coin_pic, (130, 435))
def avatar():
global total_money
with open('Data/user_money.txt', 'r+') as file:
try:
final_money = int(file.read())
except ValueError:
final_money = 0
with open('Data/user_money.txt', 'r+') as file:
final_money += total_money
file.write(str(final_money))
total_money = 0
# title
pygame.draw.rect(screen, black, pygame.Rect(190, 27, 202, 57))
pygame.draw.rect(screen, purple, pygame.Rect(190, 27, 202, 57), 4)
screen.blit(avatar_title, (204, 37))
# esc
pygame.draw.rect(screen, black, pygame.Rect(48, 30, 75, 40))
pygame.draw.rect(screen, purple, pygame.Rect(48, 30, 75, 40), 4)
screen.blit(start_esc, (58, 38))
# customise box
pygame.draw.rect(screen, grey, pygame.Rect(50, 250, 500, 290))
pygame.draw.rect(screen, purple, pygame.Rect(50, 250, 500, 290), 5)
# current car
pygame.draw.rect(screen, grey, pygame.Rect(200, 110, 180, 105))
pygame.draw.rect(screen, purple, pygame.Rect(200, 110, 180, 105), 4)
screen.blit(current_car, (228, 108))
# next page arrow
pygame.draw.rect(screen, black, pygame.Rect(465, 18, 120, 83))
pygame.draw.rect(screen, purple, pygame.Rect(465, 18, 120, 83), 4)
# money
pygame.draw.rect(screen, black, pygame.Rect(50, 130, 106, 55))
pygame.draw.rect(screen, purple, pygame.Rect(50, 130, 106, 55), 4)
screen.blit(small_coin_pic, (40, 120))
total_money_message = my_font_start.render(f' - {final_money}', False, (255, 255, 255))
screen.blit(total_money_message, (90, 145))
def avatar_page_2():
global total_money
with open('Data/user_money.txt', 'r+') as file:
try:
final_money = int(file.read())
except ValueError:
final_money = 0
with open('Data/user_money.txt', 'r+') as file:
final_money += total_money
file.write(str(final_money))
total_money = 0
# title
pygame.draw.rect(screen, black, pygame.Rect(190, 27, 202, 57))
pygame.draw.rect(screen, purple, pygame.Rect(190, 27, 202, 57), 4)
screen.blit(avatar_title, (204, 37))
# go back arrow
pygame.draw.rect(screen, black, pygame.Rect(10, 18, 120, 83))
pygame.draw.rect(screen, purple, pygame.Rect(10, 18, 120, 83), 4)
# current car
pygame.draw.rect(screen, grey, pygame.Rect(200, 110, 180, 105))
pygame.draw.rect(screen, purple, pygame.Rect(200, 110, 180, 105), 4)
screen.blit(current_car, (228, 108))
# customise box
pygame.draw.rect(screen, grey, pygame.Rect(50, 250, 500, 290))
pygame.draw.rect(screen, purple, pygame.Rect(50, 250, 500, 290), 5)
# money
pygame.draw.rect(screen, black, pygame.Rect(50, 130, 106, 55))
pygame.draw.rect(screen, purple, pygame.Rect(50, 130, 106, 55), 4)
screen.blit(small_coin_pic, (40, 120))
total_money_message = my_font_start.render(f' - {final_money}', False, (255, 255, 255))
screen.blit(total_money_message, (90, 145))
running = True
while running:
screen.blit(background, (0, 0))
if game_start_screen:
start_screen()
if avatar_screen:
avatar()
if tutorial_screen:
tutorial()
if game_screen:
game()
score_display = my_font_start.render(f'Score: {score}', False, (255, 255, 255))
screen.blit(score_display, (450, 25))
difficulty_title = my_font_start.render('Difficulty: ', False, (255, 255, 255))
screen.blit(difficulty_title, (32, 25))
if basic or score == 0:
easy_title = my_font_start.render('Basic', False, (0, 0, 190))
screen.blit(easy_title, (215, 25))
if 15 <= score < 30:
basic = False
easy = True
if easy:
medium_title = my_font_start.render('Easy', False, (0, 255, 0))
screen.blit(medium_title, (215, 25))
if 30 <= score < 55:
easy = False
medium = True
if medium:
hard_title = my_font_start.render('Medium', False, (228, 155, 15))
screen.blit(hard_title, (215, 25))
if 55 <= score < 85:
medium = False
hard = True
if hard:
extreme_title = my_font_start.render('Hard', False, (255, 0, 0))
screen.blit(extreme_title, (215, 25))
if score > 85:
hard = False
extreme = True
if extreme:
extreme_title = my_font_start.render('Extreme', False, (180, 0, 0))
screen.blit(extreme_title, (215, 25))
# ~~~DRAWING PLAYER + ANIMATIONS FOR CARS~~~ #
if current_car == tank:
current_tank = tank_sprites[int(tank_counter)]
tank_counter += 0.07
if tank_counter >= len(tank_sprites):
tank_counter = 0
screen.blit(current_tank, (car_x, car_y))
else:
screen.blit(current_car, (car_x, car_y))
if avatar_screen:
if red_car_button.draw():
current_car = red_car
current_colour = red
if blue_car_button.draw():
current_car = blue_car
current_colour = blue
if green_car_button.draw():
current_car = green_car
current_colour = green
if orange_car_button.draw():
current_car = orange_car
current_colour = orange
if purple_car_button.draw():
current_car = purple_car
current_colour = purple_for_car
if grey_car_button.draw():
current_car = grey_car
current_colour = grey_for_car
if next_page_button.draw():
avatar_screen = False
avatar_screen_two = True
if avatar_screen_two:
avatar_page_2()
if tank_button.draw():
current_car = tank
current_colour = colour_of_tank
if go_back_button.draw():
avatar_screen_two = False
avatar_screen = True
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
# avatar screen
if game_start_screen:
if event.key == pygame.K_a:
button_sound = mixer.Sound('Sound/button sound.wav')
button_sound.play()
fade_screen_loading(600, 600)
game_start_screen = False
tutorial_screen = False
game_screen = False
avatar_screen = True
# tutorial screen
if game_start_screen:
if event.key == pygame.K_t:
button_sound = mixer.Sound('Sound/button sound.wav')
button_sound.play()
fade_screen_loading(600, 600)
game_start_screen = False
game_screen = False
avatar_screen = False
tutorial_screen = True
# game screen
if game_start_screen:
if event.key == pygame.K_SPACE:
button_sound = mixer.Sound('Sound/button sound.wav')
button_sound.play()
fade_screen_loading(600, 600)
car_x = 10
car_y = 245
coin_x = 3000
coin_y = 275
coins_gained = 0
coin_hit = False
coin_on_map = False
obstacle0_x = 500
obstacle1_x = 750
obstacle2_x = 1150
obstacle0_y = 125
obstacle1_y = 275
obstacle2_y = 425
obstacle_speed = 2.4
score = 0
HP = 350
no_collide = True
game_start_screen = False
tutorial_screen = False
avatar_screen = False
game_screen = True
basic = True
if game_screen:
if event.key == pygame.K_UP or event.key == pygame.K_w:
if car_y > 0:
car_y -= 150
if car_y - 50 < 0:
image_border_sound = mixer.Sound('Sound/image border sound.wav')
image_border_sound.play()
car_y += 150
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
if car_y < 600:
car_y += 150
if car_y + 50 > 500:
image_border_sound = mixer.Sound('Sound/image border sound.wav')
image_border_sound.play()
car_y -= 150
# go back to starting screen
if not game_start_screen:
if event.type == pygame.KEYDOWN:
if not avatar_screen_two:
if event.key == pygame.K_ESCAPE:
button_sound = mixer.Sound('Sound/button sound.wav')
button_sound.play()
tutorial_screen = False
avatar_screen = False
avatar_screen_two = False
game_screen = False
game_start_screen = True
if event.type == pygame.QUIT:
running = False
pygame.display.update()