-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrogger.py
More file actions
378 lines (298 loc) · 13.6 KB
/
Copy pathFrogger.py
File metadata and controls
378 lines (298 loc) · 13.6 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
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
'''Author: Thomas Cheng
Date: Jan 28, 2018
Description: This is the the Frogger game. Below are the different additions made
to pyFrogger.
v.1 - Added basic sprite objects to the game, and movement to the log and car
sprite objects
v.2 - Added collision detection so for the vehicles as well as log
v.3 - Added a timer at the bottom of the screen
v.4 - Added a life system that displays the player's lives as circles
v.5 - Added functional score to the game
v.6 - Added highscore to the game
v.7 - Added a start screen to the game
v.8 - Added frog, log, life, and vehicle sprites to the game
v.9 - Added music and sound effects to the game
v.10 - Added a snake obstacle the player must dodge
v.11 - Added varying log speeds each time they move off screen
'''
# Import and Initialize
import pygame, pySprites, random
pygame.init()
pygame.mixer.init()
def start_screen(highscore):
''''''
# Dispay
screen = pygame.display.set_mode((660, 624))
pygame.display.set_caption('pyFrogger!')
# Entities
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
# BACKGROUNG SPRITES
# create the river
river = pySprites.River((screen.get_width(), 216), 65, 0)
# create the grass zones
bottom_position = [589, 309, 64]
grass = []
for iteration in xrange (3):
grass.append(pySprites.Grass(screen, (0, 100, 0), bottom_position[iteration]))
highScore = pySprites.HighScore(highscore)
# creating the frog sprite
frog = pySprites.Frog(screen, 589)
allSprites = pygame.sprite.OrderedUpdates(river, grass, frog, highScore)
# title font and label
titleFont = pygame.font.Font("./myTexts/PixelFlag.ttf", 90)
titleText = titleFont.render('{FROGGER}', 1, (255, 255, 255))
# flashing "press space to continue" label
messageFont = pygame.font.Font('./myTexts/PixelFlag.ttf', 35)
# load background music
pygame.mixer.music.load('./mySounds/Frogger-Menu-Music.mp3')
pygame.mixer.music.set_volume(0.35)
pygame.mixer.music.play(-1)
# Action via ALTER
# Assign
clock = pygame.time.Clock()
keepGoing = True
colour = 255
factor = 3
pygame.mouse.set_visible(False)
# Loop
while keepGoing:
# Time
clock.tick(30)
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
pygame.mixer.music.fadeout(1000)
return False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
frog.hop_sound()
frog.move_right()
elif event.key == pygame.K_LEFT:
frog.hop_sound()
frog.move_left()
elif event.key == pygame.K_ESCAPE:
keepGoing = False
pygame.mixer.music.fadeout(1000)
return False
elif event.key == pygame.K_SPACE:
keepGoing = False
pygame.mixer.music.fadeout(1000)
return True
# make the continue message blink
messageText = messageFont.render('(PRESS SPACEBAR TO CONTINUE)', 1, (colour, colour, colour))
colour -= factor
if colour <= 0 or colour >= 255:
factor = -factor
# Refresh
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
screen.blit(titleText, (135, 126))
screen.blit(messageText, (141, 465))
pygame.display.flip()
pygame.time.delay(1500)
def main_gameloop(highscore):
'''This is the main function for the Frogger game. This function accepts one
parameter. "highscore": a whole number used to display the player's current
high score. This function returns one value. A whole number stored in
the scorekeeper class.'''
# Dispay
screen = pygame.display.set_mode((660, 624))
pygame.display.set_caption('pyFrogger!')
# Entities
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
# BACKGROUNG SPRITES
# create the river
river = pySprites.River((screen.get_width(), 213), 65, 0)
# create the grass zones
bottom_position = [589, 309, 64]
grass = []
for iteration in xrange (3):
grass.append(pySprites.Grass(screen, (0, 100, 0), bottom_position[iteration]))
botSprites = pygame.sprite.OrderedUpdates(river, grass)
# MOVING / MIDDLE SPRITES
# creating the frog sprite
frog = pySprites.Frog(screen, 589)
velocities = [-2, 3, -4, 2, -5, -3, 4]
# create the logs
log_centers = [[(119, 259), (450, 259)], [(461, 224), (91, 224)], [(550, 189)],\
[(50, 154), (407, 154)], [(105, 119), (525, 119)], [(48, 84), (425, 84)]]
logs = []
for log_row in xrange (len(log_centers)):
log_speed = velocities[random.randrange(0, len(velocities))]
for log in xrange (len(log_centers[log_row])):
logs.append(pySprites.Logs(screen, log_centers[log_row][log], \
log_speed, random.randint(1, 3)))
logGroup = pygame.sprite.OrderedUpdates(logs)
# creates the vehicles
vehicle_centers = [[(450, 539)], [(100, 504), (500, 504)], \
[(300, 469)], [(50, 434), (450, 434)], \
[(79, 399), (450, 399)], [(300, 364)], [(485, 329), (150, 329)]]
vehicles = []
for vehicle_row in xrange (len(vehicle_centers)):
car_speed = velocities[random.randrange(0, len(velocities))]
for car in xrange (len(vehicle_centers[vehicle_row])):
vehicles.append(pySprites.Vehicle(screen, \
vehicle_centers[vehicle_row][car], car_speed))
vehicleGroup = pygame.sprite.OrderedUpdates(vehicles)
snake = pySprites.Snake(screen, 294)
# creating the midSprites group that will hold all the middle sprites
midSprites = pygame.sprite.OrderedUpdates(logGroup, vehicleGroup, snake, frog)
# HIGH SPRITES
# creating the timer
timer_bar = pySprites.Timer_Bar((450, 10), 600, 545, 1)
timer_text = pySprites.Timer_Text(555, screen.get_height())
# create the score keeper
scorekeeper = pySprites.ScoreKeeper(10)
# displays the players lives
life_right_pos = [646, 621, 595]
lives = []
for iteration in xrange (len(life_right_pos)):
lives.append(pySprites.LifeKeeper(7, life_right_pos[iteration]))
lifeGroup = pygame.sprite.Group(lives)
life_text = pySprites.Life_Text(0, 567)
highScore = pySprites.HighScore(highscore)
# creating the highSprites group that will hold all the middle sprites
highSprites = pygame.sprite.OrderedUpdates(timer_bar, timer_text, scorekeeper,\
lives, life_text, highScore)
# putting all the sprite groups into one allSprites group
allSprites = pygame.sprite.OrderedUpdates(botSprites, midSprites, highSprites)
# load background music
pygame.mixer.music.load('./mySounds/Frogger-Main-Theme.mp3')
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)
# Action via ALTER
# Assign
clock = pygame.time.Clock()
keepGoing = True
life_index = 0
lost_life = False
pygame.mouse.set_visible(False)
# Loop
while keepGoing:
# Time
clock.tick(30)
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
pygame.mixer.music.fadeout(1000)
# checks for if the player has pressed the directional keys
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
frog.hop_sound()
frog.move_up()
# checks to see if the frog has passed it's current max distance
if frog.distance_comparison(frog.rect.top):
scorekeeper.add_points()
elif event.key == pygame.K_DOWN:
frog.hop_sound()
frog.move_down()
elif event.key == pygame.K_RIGHT:
frog.hop_sound()
frog.move_right()
elif event.key == pygame.K_LEFT:
frog.hop_sound()
frog.move_left()
# checks to see if the snake is moving along the middle grass section
if not snake.is_moving():
if random.randrange(0, 100) == 19:
snake.start_moving()
# checks to see if the player has timed out
if timer_bar.has_reset():
frog.reset()
# checks to see if the frog has collided with a vehicle
if pygame.sprite.spritecollide(frog, vehicleGroup, False) and not lost_life:
frog.squished_sound()
frog.reset()
lost_life = True
timer_bar.reset()
# checks to see if the frog has collided with the snake
if snake.rect.colliderect(frog.rect):
lost_life = True
# checks to see if the frog has landed on a log
log_hitList = pygame.sprite.spritecollide(frog, logGroup, False)
for log in log_hitList:
if (frog.rect.centery == log.rect.centery):
if (frog.rect.left >= log.rect.left) and (frog.rect.right <= log.rect.right):
logSpeed = log.get_speed()
frog.log_movement(logSpeed)
# checks to see if the player missed the log
if not log_hitList and frog.rect.colliderect(river.rect):
frog.splash_sound()
frog.reset()
lost_life = True
timer_bar.reset()
# checks to see if the frog has reached the grass patch located at the
# top of the screen, and has landed on that patch
if frog.rect.centery == grass[-1].rect.centery:
if (frog.rect.left >= grass[-1].rect.left) and \
(frog.rect.right <= grass[-1].rect.right):
for log in xrange (len(logs)):
logs[log].speed_up(1)
for car in xrange (len(vehicles)):
vehicles[car].speed_up(2)
grass[-1].half_grass()
scorekeeper.crossing_complete()
else:
lost_life = True
frog.reset()
timer_bar.reset()
# checks to see if the player has lost a life
if lost_life:
lives[life_index].kill()
life_index += 1
lost_life = False
frog.reset_distance_travelled()
# Refresh
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
# FOLLOWING CODE IS NOT PART OF THE REFRESH SECTION
# It is there to avoid the image flickering once game ends
# checks to see if the player has lost all of their lives
if len(lifeGroup) == 0:
pygame.time.delay(750)
endgame_message(allSprites, screen, background, 'GAME OVER')
pygame.mixer.music.fadeout(1000)
keepGoing = False
# checks to see if the player has successfully made it to the oter side 3 times
if scorekeeper.beat_game():
pygame.time.delay(750)
endgame_message(allSprites, screen, background, 'WINNER!')
pygame.mixer.music.fadeout(1000)
keepGoing = False
pygame.mouse.set_visible(True)
pygame.time.delay(1500)
return scorekeeper.get_score()
def endgame_message(allSprites, screen, background, message):
'''This function is used to display the endgame message. This function returns
no values and accpets four parameters. "allSprites": a pygame.Group object.
"screen": a copy of the pygame.screen object. "background": a copy of the
pygame.Surface object. "message": a string value.'''
allSprites.clear(screen, background)
display_font = pygame.font.Font('./myTexts/Abstract.TTF', 25)
display_label = display_font.render(message, 1, (255, 255, 255))
screen.blit(display_label, (25, 224))
pygame.display.flip()
pygame.time.delay(2000)
def main():
'''This is the main function that will call the start screen function and
main_gameloop function.'''
current_highscore = 0
keepPlaying = True
while keepPlaying:
keepPlaying = start_screen(current_highscore)
if keepPlaying:
highscore = main_gameloop(current_highscore)
current_highscore = max(highscore, current_highscore)
pygame.quit()
main()