-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (127 loc) · 5.87 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
import pygame
from random import choice
pygame.init()
screen = pygame.display.set_mode((700, 730))
pygame.display.set_caption("2D car racer")
clock = pygame.time.Clock()
pygame.mixer.music.load("music/main_menu.mp3")
pygame.mixer.music.play()
font = pygame.font.Font("Assets/mfont.otf", 35)
home = pygame.image.load("Assets/splashscreen.png")
road = pygame.image.load("Assets/road.png")
grass = pygame.image.load("Assets/grass.jpg")
car = pygame.image.load("Assets/car.png")
dead = pygame.image.load("Assets/gameover.png")
o1 = pygame.image.load("Assets/o1.png").convert_alpha()
o2 = pygame.image.load("Assets/o2.png").convert_alpha()
o3 = pygame.image.load("Assets/o3.png").convert_alpha()
o4 = pygame.image.load("Assets/o4.png").convert_alpha()
o5 = pygame.image.load("Assets/o5.png").convert_alpha()
car = pygame.transform.scale(car, (57.5, 128.25)).convert_alpha()
grass = pygame.transform.scale(grass, (700, 735)).convert()
road = pygame.transform.scale(road, (477, 500)).convert()
dead = pygame.transform.scale(dead, (700, 730)).convert()
home = pygame.transform.scale(home, (700, 730)).convert()
screen.blit(home, (0,0))
scroll = 0
lane_x = (375, 475, 275, 175)
left = False
right = False
score = 0
car_r = car.get_rect(topleft = (375, 400))
obj_list = []
spawn_obj_event = pygame.USEREVENT #timer for spawning obstacles
pygame.time.set_timer(spawn_obj_event, 500)
score_event = pygame.USEREVENT + 1 #timer for score
pygame.time.set_timer(score_event, 2000)
running = True
maingame = False
gameend = False
def obj_movement(obj): #spawns, moves and removes obstacles
if obj:
for i in obj:
i[1].y += 8
obj = [o for o in obj if o[1].y < 700]
screen.blit(i[0] ,i[1])
return obj
else:
return []
def collisions(car, obj): #checks for collision between car and the obstacles
if maingame:
for o in obj:
if car.colliderect(o):
return True
return False
def display_score(x, y, color): #displays score
score_text = font.render(f"SCORE: {score}", True, color)
screen.blit(score_text, (x, y))
def spawn_obj(): #chooses obstacles to spawn
ox = choice(lane_x)
o1_r = o1.get_rect(topleft = (ox, -200))
o2_r = o2.get_rect(topleft = (ox, -200))
o3_r = o3.get_rect(topleft = (ox, -200))
o4_r = o4.get_rect(topleft = (ox, -200))
o5_r = o5.get_rect(topleft = (ox, -200))
ob_choice = choice([(o1, o1_r), (o2, o2_r), (o3, o3_r), (o4, o4_r), (o5, o5_r)])
obj_list.append(ob_choice)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == spawn_obj_event and maingame: #calls a function to spawn obstacles when there is a spawn_obj_event
spawn_obj()
if event.type == score_event and maingame: #adds 1 to score when there is a score_event
score += 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: #exits the game when esc is pressed
running = False
if event.key == pygame.K_RETURN:
if not maingame and not gameend: #starts the music and main game from the mainmenu
pygame.mixer.music.fadeout(200)
pygame.mixer.music.load("music/game.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(.6)
maingame = True
if gameend: #relaunches the game after it ends and resets the score
gameend = False
maingame = True
score = 0
pygame.mixer.music.play()
if event.key == pygame.K_LEFT:
left = True
if event.key == pygame.K_RIGHT:
right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left = False
if event.key == pygame.K_RIGHT:
right = False
if maingame:
screen.blit(grass, (0, 0))
for i in range(-1, 2): #scrolls the road
screen.blit(road, (111.5, 500*i + scroll))
scroll += 7
if scroll > 500: #checks if the road is out of screen and deletes the road out of the screen
scroll = 0
screen.blit(car, car_r)
if left: #moves the car left and right when it detects that the left/right variable is True
car_r.x -= 4
if right:
car_r.x +=4
if collisions(car_r, [o[1] for o in obj_list]): #checks if the car collides with a obstacle and ends the game
maingame = False
gameend = True
if car_r.collidepoint(577, 450) or car_r.collidepoint(116, 450): #checks if car goes out of the road and ends the game
gameend = True
maingame = False
obj_list = obj_movement(obj_list) #updates the list of obstacles
display_score(5, 10, "darkgrey") #displays and updates the score in the top-left corner of the screen
if gameend: #shows the game over screen, resets cars and obstacles and displays score
screen.blit(dead, (0, 0))
car_r.x = 375
obj_list = []
display_score(260, 280, "purple")
pygame.mixer.music.stop()
pygame.display.update()
clock.tick(60)
pygame.quit()