-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarGame.py
258 lines (185 loc) · 6.73 KB
/
CarGame.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
import pygame
import random
pygame.init()
FPS = 60
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
red = (150, 0, 0)
green = (0, 150, 0)
bred = (255, 0, 0)
bgreen = (0, 255, 0)
blue = (0, 0, 255)
biggening_factor_car = 6
carw = 12 * biggening_factor_car
carh = 16 * biggening_factor_car
turn_angle = 7
biggening_factor_back = 50
backImg = pygame.image.load('Background.png')
backImg = pygame.transform.scale(backImg, (display_width, display_height))
carImg = pygame.image.load('Car.png')
carImg = pygame.transform.scale(carImg, (carw, carh))
leftCarImg = pygame.transform.rotate(carImg, turn_angle)
rightCarImg = pygame.transform.rotate(carImg, -turn_angle)
expImg = pygame.image.load('Explosion.png')
expImg = pygame.transform.scale(expImg, (carw * 2, carh * 2))
enemyImg = pygame.image.load('Enemy.png')
enemyImg = pygame.transform.scale(enemyImg, (carw, carh))
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Car Game')
clock = pygame.time.Clock()
pause = False
def score_disp(score):
font = pygame.font.SysFont(None, 100)
text = font.render(str(score), True, black)
gameDisplay.blit(text, (display_width / 2 - 20, 0))
def things(thingx, thingy):
gameDisplay.blit(enemyImg, [thingx, thingy])
def back(backx, backy):
gameDisplay.blit(backImg, (backx, backy))
gameDisplay.blit(backImg, (backx, backy - display_height))
def car(x, y, crashed):
if crashed:
gameDisplay.blit(expImg, (x - carw / 2, y - carh / 2))
else:
gameDisplay.blit(carImg, (x, y))
def text_objects(text, font):
text_surface = font.render(text, True, black)
return text_surface, text_surface.get_rect()
def message_display(text, x, y):
font = pygame.font.SysFont(None, 115)
text_surf, text_rect = text_objects(text, font)
text_rect.center = (x, y)
gameDisplay.blit(text_surf, text_rect)
pygame.display.update()
def crash():
crashed = True
while crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit_game()
if event.key == pygame.K_RETURN:
game_loop()
font = pygame.font.SysFont(None, 115)
text_surf, text_rect = text_objects("You Crashed!", font)
text_rect.center = (display_width / 2, display_height / 2)
gameDisplay.blit(text_surf, text_rect)
button("Restart", 150, 450, 100, 50, green, bgreen, game_loop)
button("Exit", 550, 450, 100, 50, red, bred, quit_game)
pygame.display.update()
clock.tick(FPS)
def button(msg, x, y, w, h, i_color, a_color, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, a_color, (x, y, w, h))
if click[0] == 1 and action is not None:
action()
else:
pygame.draw.rect(gameDisplay, i_color, (x, y, w, h))
small_text = pygame.font.SysFont(None, 20)
text_surf, text_rect = text_objects(msg, small_text)
text_rect.center = (x + w / 2, y + h / 2)
gameDisplay.blit(text_surf, text_rect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit_game()
if event.key == pygame.K_RETURN:
game_loop()
gameDisplay.fill(white)
font = pygame.font.SysFont(None, 115)
text_surf, text_rect = text_objects("Race Game", font)
text_rect.center = (display_width / 2, display_height / 2)
gameDisplay.blit(text_surf, text_rect)
button("Start", 150, 450, 100, 50, green, bgreen, game_loop)
button("Exit", 550, 450, 100, 50, red, bred, quit_game)
pygame.display.update()
clock.tick(FPS)
def unpause():
global pause
pause = False
def pause_screen():
global pause
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
unpause()
if event.key == pygame.K_RETURN:
quit_game()
gameDisplay.fill(white)
font = pygame.font.SysFont(None, 115)
text_surf, text_rect = text_objects("Paused", font)
text_rect.center = (display_width / 2, display_height / 2)
gameDisplay.blit(text_surf, text_rect)
button("Resume", 150, 450, 100, 50, green, bgreen, unpause)
button("Exit", 550, 450, 100, 50, red, bred, quit_game)
pygame.display.update()
clock.tick(FPS)
def quit_game():
pygame.quit()
quit()
def game_loop():
global pause
score = 0
carx = (display_width / 2 - carw / 2)
cary = (display_height * 0.8)
deltacar = 9
back_starty = 0
thing_startx = random.randrange(50 + carw, display_width - (50 + carw))
thing_starty = -600
thing_speed = 7
game_exit = False
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
pause_screen()
# Movement controls
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_LEFT]:
carx -= deltacar
if keys_pressed[pygame.K_RIGHT]:
carx += deltacar
back(0, back_starty)
# things(thingx, thingy)
things(thing_startx, thing_starty)
thing_starty += thing_speed
back_starty += thing_speed * 2
car(carx, cary, False)
score_disp(score)
if carx > display_width - 50 - carw or carx < 50:
car(carx, cary, True)
crash()
if back_starty > display_height:
back_starty = 0
if thing_starty > display_height:
thing_starty = 0 - carh
thing_startx = random.randrange(50 + carw, display_width - (50 + carw))
score += 1
if thing_speed < 25:
thing_speed += 0.3
if thing_starty + carh > cary and thing_starty < cary + carh:
if thing_startx < carx + carw and thing_startx + carw > carx:
car(carx, cary, True)
crash()
pygame.display.update()
clock.tick(FPS)
game_intro()
game_loop()
quit_game()