-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaming1.py
More file actions
311 lines (210 loc) · 8.15 KB
/
gaming1.py
File metadata and controls
311 lines (210 loc) · 8.15 KB
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
import pygame
import time
import random
pygame.init()
crash_sound = pygame.mixer.Sound("crash.wav")
pygame.mixer.music.load("car_sound.wav")
dispaly_width = 800
dispaly_hight = 700
initial_pos = [230, 430]
road_startx = 205
road_finishx = 605
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
light_red = (150, 0, 0)
light_green = (0, 150, 0)
car_width = 100
car_height = 168
pause = False
crashed = True
intro = True
game_introduction1 = "This is a car race.And you have to dodge each car coming towars you."
game_introduction2 = "Also you have to keep your car within the road."
game_introduction3 = "Othewise you will crash."
gameDisplay = pygame.display.set_mode((dispaly_width, dispaly_hight))
pygame.display.set_caption('A Car Race')
clock = pygame.time.Clock()
carImg = pygame.image.load('car1.png')
car2Img = pygame.image.load('car3.png')
roadImg = pygame.image.load('road2.png')
pygame.display.set_icon(carImg)
def thing_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged : "+str(count), True, black)
gameDisplay.blit(text, (0, 0))
def things(thingx, thingy):
gameDisplay.blit(car2Img, (thingx, thingy))
def road(roadx, roady):
gameDisplay.blit(roadImg, (roadx, roady))
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ic, (x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
smallText = pygame.font.Font('freesansbold.ttf', 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
gameDisplay.blit(textSurf, textRect)
def game_intro_2():
global intro, game_introduction1, game_introduction2, game_introduction3
intro = False
intro2 = True
while intro2:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
large_Text1 = pygame.font.Font('freesansbold.ttf', 20)
Text_Surface1, Text_Rectangle1 = text_objects(game_introduction1, large_Text1)
Text_Rectangle1.center = (400, 100)
gameDisplay.blit(Text_Surface1, Text_Rectangle1)
large_Text2 = pygame.font.Font('freesansbold.ttf', 20)
Text_Surface2, Text_Rectangle2 = text_objects(game_introduction2, large_Text2)
Text_Rectangle2.center = (400, 150)
gameDisplay.blit(Text_Surface2, Text_Rectangle2)
large_Text3 = pygame.font.Font('freesansbold.ttf', 20)
Text_Surface3, Text_Rectangle3 = text_objects(game_introduction3, large_Text3)
Text_Rectangle3.center = (400, 200)
gameDisplay.blit(Text_Surface3, Text_Rectangle3)
button("GO!", 150, 550, 100, 50, light_green, green, game_loop)
button("Back", 450, 550, 100, 50, light_red, red, game_intro)
pygame.display.update()
clock.tick(60)
def quiteGame():
pygame.quit()
quit()
def game_intro():
global intro
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
largeText = pygame.font.Font('freesansbold.ttf', 100)
TextSurface, TextRectangle = text_objects("A Bit Racy", largeText)
TextRectangle.center = ((dispaly_width / 2), (dispaly_hight / 2))
gameDisplay.blit(TextSurface, TextRectangle)
button("Play", 150, 450, 100, 50, light_green, green, game_intro_2)
button("Quit", 450, 450, 100, 50, light_red, red, quiteGame)
pygame.display.update()
clock.tick(20)
def unpause():
global pause
pygame.mixer.music.unpause()
pause = False
def Pause():
pygame.mixer.music.pause()
largeText = pygame.font.Font('freesansbold.ttf', 100)
TextSurface, TextRectangle = text_objects("pause", largeText)
TextRectangle.center = ((dispaly_width / 2), (dispaly_hight / 2))
gameDisplay.blit(TextSurface, TextRectangle)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#gameDisplay.fill(white)
button("Continue", 150, 450, 100, 50, light_green, green, unpause)
button("Quit", 450, 450, 100, 50, light_red, red, quiteGame)
pygame.display.update()
clock.tick(20)
def car(x, y):
gameDisplay.blit(carImg, (x, y))
def text_objects(text, font):
textSurface = font.render(text, True, blue)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 100)
TextSurface, TextRectangle = text_objects(text, largeText)
TextRectangle.center = ((dispaly_width / 2), (dispaly_hight / 2))
gameDisplay.blit(TextSurface, TextRectangle)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(crash_sound)
global crashed
largeText = pygame.font.Font('freesansbold.ttf', 100)
TextSurface, TextRectangle = text_objects("You Crashed", largeText)
TextRectangle.center = ((dispaly_width / 2), (dispaly_hight / 2))
gameDisplay.blit(TextSurface, TextRectangle)
while crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#gameDisplay.fill(white)
button("Play Again", 150, 450, 110, 50, light_green, green, game_loop)
button("Quit", 450, 450, 100, 50, light_red, red, quiteGame)
pygame.display.update()
clock.tick(20)
def game_loop():
pygame.mixer.music.play(-1)
global pause
x = (dispaly_width * 0.35)
y = (dispaly_hight * 0.65)
x_change = 0
thing_startx = random.choice(initial_pos)
thing_starty = -100
thing_speed = 6
thing_speed_change = 1
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key == pygame.K_RIGHT:
x_change = 10
if event.key == pygame.K_p:
pause = True
Pause()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event == pygame.key.get_pressed():
#keystate = pygame.key.get_pressed()
if event[pygame.K_UP]:
thing_speed += thing_speed_change
if event[pygame.K_DOWN]:
thing_speed -= thing_speed_change
#keystate = pygame.key.get_pressed()
#print(event)
x += x_change
gameDisplay.fill(white)
road(-100, 0)
things(thing_startx, thing_starty)
thing_starty += thing_speed
car(x, y)
thing_dodged(dodged)
if x > road_finishx - car_width or x < road_startx:
crash()
if thing_starty > dispaly_hight:
thing_starty = -100
thing_startx = random.choice(initial_pos)
dodged += 1
thing_speed += 1
if y < thing_starty + car_height:
if x > thing_startx and x < thing_startx + car_width or x + car_width > thing_startx and x + car_width < thing_startx + car_width:
crash()
pygame.display.update()
clock.tick(120)
game_intro()
#game_loop()
pygame.quit()
quit()