-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leveon.py
426 lines (397 loc) · 15.9 KB
/
Leveon.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
"""
Created as a GIF meme mocking NBC's Frogger depiction of Le'Veon Bell
Game was originally Chiefs versus Steelers on Jan 15th, 2017
"""
import os
import sys
import pygame
from genmenu import *
(SCREEN_WIDTH, SCREEN_HEIGHT) = 728, 900
IMAGES = {}
SOUNDS = {}
SPAWN_POSITION = (SCREEN_WIDTH/2-30, SCREEN_HEIGHT-145)
PLAY_SCREEN, START_SCREEN, GAMEOVER_SCREEN, WINNER_SCREEN = 0, 1, 2, 3
TIME_LEFT = 31
def adjust_to_correct_appdir():
try:
appdir = sys.argv[0] #feel free to use __file__
if not appdir:
raise ValueError
appdir = os.path.abspath(os.path.dirname(sys.argv[0]))
os.chdir(appdir)
if not appdir in sys.path:
sys.path.insert(0, appdir)
except:
#placeholder for feedback, adjust to your app.
#remember to use only python and python standard libraries
#not any resource or module into the appdir
#a window in Tkinter can be adequate for apps without console
#a simple print with a timeout can be enough for console apps
print('Please run from an OS console.')
import time
time.sleep(10)
sys.exit(1)
adjust_to_correct_appdir()
def load_sound(file, name):
sound = pygame.mixer.Sound(file)
SOUNDS[name] = sound
def load_image(file, name, transparent, alpha):
new_image = pygame.image.load(file)
if alpha:
new_image = new_image.convert_alpha()
else:
new_image = new_image.convert()
if transparent:
colorkey = new_image.get_at((0, 0))
new_image.set_colorkey(colorkey, RLEACCEL)
IMAGES[name] = new_image
def display_caption():
pygame.display.set_caption("Le'Veon Style")
def startplaceholder(screen):
global MENU_SELECTION
MENU_SELECTION = PLAY_SCREEN
class Menu():
def __init__(self, screen, start_menu):
self.screen = screen
self.title = start_menu
self.menu = genmenu(['START', lambda: startplaceholder(screen)])
self.menu.position(900, 190)
self.menu.defaultColor((0, 0, 0))
self.menu.choiceColor((0, 0, 0))
self.clock = pygame.time.Clock()
event = pygame.event.get()
self.menu.create(self.screen)
self.menu.choose(event)
self.main_loop()
def main_loop(self):
global MENU_SELECTION
while MENU_SELECTION == START_SCREEN:
self.clock.tick(60)
events = pygame.event.get()
self.menu.choose(events)
self.screen.blit(self.title, (0, 0))
self.menu.create(self.screen)
pygame.display.flip()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
class GameOver():
def __init__(self, screen, gameover, arial_font, score):
self.screen = screen
self.title = gameover
self.clock = pygame.time.Clock()
self.main_loop(arial_font, score)
def main_loop(self, arial_font, score):
global MENU_SELECTION
while MENU_SELECTION == GAMEOVER_SCREEN:
self.clock.tick(60)
events = pygame.event.get()
self.screen.blit(self.title, (0, 0))
score_gameover_text = arial_font.render("Score: " + str(score), 1, (0, 0, 0))
self.screen.blit(score_gameover_text, (50, 175))
pygame.display.flip()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
MENU_SELECTION = START_SCREEN
class Winner():
def __init__(self, screen, winner):
self.screen = screen
self.title = winner
self.clock = pygame.time.Clock()
self.main_loop()
def main_loop(self):
global MENU_SELECTION
while MENU_SELECTION == WINNER_SCREEN:
self.clock.tick(60)
events = pygame.event.get()
self.screen.blit(self.title, (0, 0))
pygame.display.flip()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
MENU_SELECTION = START_SCREEN
def mainmenuplaceholder(screen):
global MENU_SELECTION
MENU_SELECTION = START_SCREEN
class Enemy(pygame.sprite.Sprite):
enemies = []
def __init__(self, allsprites, pos, tier):
pygame.sprite.Sprite.__init__(self)
self.image = IMAGES["spr_enemy"]
self.rect = self.image.get_rect()
self.rect.topleft = pos
allsprites.add(self)
self.tier = tier
Enemy.enemies.append(self)
def update(self):
self.difficulty_movement()
newpos = (self.rect.topleft[0] + self.direction[0], self.rect.topleft[1])
self.rect.topleft = newpos
if(self.rect.topleft[0] < -100 and self.tier == 3):
self.rect.topleft = (SCREEN_WIDTH, self.rect.topleft[1])
if(self.rect.topleft[0] > SCREEN_WIDTH and (self.tier == 1 or self.tier == 2)):
self.rect.topleft = (-68, self.rect.topleft[1])
def difficulty_movement(self):
# Tier 1 moves right, Tier 2 moves right, Tier 3 (bottom) moves left
if self.tier == 1:
self.direction = (2, 0)
elif self.tier == 2:
self.direction = (1, 0)
elif self.tier == 3:
self.direction = (-1, 0)
def destroy(self):
Enemy.enemies.remove(self)
self.kill()
class Steelers(pygame.sprite.Sprite):
steelers_list = []
def __init__(self, allsprites, pos, tier):
pygame.sprite.Sprite.__init__(self)
self.image = IMAGES["spr_steelers"]
self.rect = self.image.get_rect()
allsprites.add(self)
self.pos = pos
self.rect.topleft = pos
self.tier = tier
Steelers.steelers_list.append(self)
def update(self):
self.difficulty_movement()
newpos = (self.rect.topleft[0]+self.direction[0], self.rect.topleft[1])
self.rect.topleft = newpos
if(self.rect.topright[0] < 0 and self.tier == 2):
self.rect.topleft = (SCREEN_WIDTH, self.rect.topleft[1])
if(self.rect.topleft[0] > SCREEN_WIDTH and self.tier == 1):
self.rect.topleft = (-100, self.rect.topleft[1])
def difficulty_movement(self):
if self.tier == 1:
self.direction = (1, 0)
elif self.tier == 2:
self.direction = (-1, 0)
def destroy(self):
Steelers.steelers_list.remove(self)
self.kill()
class Goal(pygame.sprite.Sprite):
goals = []
def __init__(self, allsprites, pos):
pygame.sprite.Sprite.__init__(self)
self.image = IMAGES["spr_goal"]
self.rect = self.image.get_rect()
allsprites.add(self)
self.rect.topleft = pos
Goal.goals.append(self)
self.filled = 0
def update(self):
pass
def destroy(self):
Goal.goals.remove(self)
self.kill()
def collision_with_player(self):
self.image = IMAGES["spr_touchdown"]
self.filled = 1
class Player(pygame.sprite.Sprite):
def __init__(self, allsprites, pos, time_left):
pygame.sprite.Sprite.__init__(self)
self.image = IMAGES["spr_player"]
self.rect = self.image.get_rect()
allsprites.add(self)
self.player_width, self.player_height = (65, 80)
self.speed_x, self.speed_y = 50, 40
self.rect.topleft = (pos[0], pos[1])
self.pos = [pos[0], pos[1]]
self.time_left = time_left
self.score = 0
def update(self):
self.image = pygame.transform.smoothscale(self.image, (self.player_width, self.player_height))
self.rect.topleft = (self.pos[0], self.pos[1])
self.speed_x, self.speed_y = 40, 50
self.time_left -= .028
def collision_with_goal(self):
self.pos[0], self.pos[1] = SPAWN_POSITION
self.rect.topleft = self.pos
def destroy(self):
self.kill()
def main():
global MENU_SELECTION
MENU_SELECTION = START_SCREEN
RUNNING, RESTART, DEBUG = 0, 1, 2
state = RESTART
debug_message = 0
allsprites = pygame.sprite.Group()
clock = pygame.time.Clock()
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Le'Veon Style")
load_image("sprites/goal.png", "spr_goal", True, True)
load_image("sprites/touchdown.png", "spr_touchdown", True, True)
load_image("sprites/patriots.png", "spr_enemy", False, True)
load_image("sprites/steelers.png", "spr_steelers", True, True)
load_image("sprites/leveonbell.png", "spr_player", True, True)
# Font and texts
arcade_font_main = pygame.font.Font("fonts/ARCADE.ttf", 48)
arial_font = pygame.font.SysFont('Arial', 32)
# Backgrounds
start_menu = pygame.image.load("sprites/startmenu.png").convert()
start_menu = pygame.transform.scale(start_menu, (SCREEN_WIDTH, SCREEN_HEIGHT))
game_over = pygame.image.load("sprites/gameover.png").convert()
game_over = pygame.transform.scale(game_over, (SCREEN_WIDTH, SCREEN_HEIGHT))
winner = pygame.image.load("sprites/winner.png").convert()
winner = pygame.transform.scale(winner, (SCREEN_WIDTH, SCREEN_HEIGHT))
football_field = pygame.image.load("sprites/footballfield.jpg").convert()
football_field = pygame.transform.scale(football_field, (SCREEN_WIDTH, SCREEN_HEIGHT))
# Window
gameicon = pygame.image.load("sprites/bellico.png")
pygame.display.set_icon(gameicon)
pygame.display.set_caption('Le\'Veon Style')
pygame.mouse.set_visible(0)
# Sounds
load_sound("SOUNDS/snd_leap.wav", "snd_leap")
SOUNDS["snd_leap"].set_volume(.01)
load_sound("SOUNDS/playerdie.wav", "snd_playerdie")
SOUNDS["snd_playerdie"].set_volume(.01)
# Music loop
pygame.mixer.music.load("SOUNDS/froggertheme.mp3")
pygame.mixer.music.set_volume(.04)
pygame.mixer.music.play(-1)
# Main
while True:
clock.tick(35)
display_caption()
if MENU_SELECTION == START_SCREEN: # Menu Screen
keys = [False, False, False, False]
Menu(screen, start_menu)
elif MENU_SELECTION == PLAY_SCREEN and state == RESTART:
Enemy(allsprites, (0, 120), 1)
Enemy(allsprites, (300, 120), 1)
Enemy(allsprites, (600, 120), 1)
Enemy(allsprites, (0, 300), 2)
Enemy(allsprites, (200, 300), 2)
Enemy(allsprites, (400, 300), 2)
Enemy(allsprites, (600, 300), 2)
Enemy(allsprites, (0, 520), 3)
Enemy(allsprites, (200, 520), 3)
Enemy(allsprites, (400, 520), 3)
Enemy(allsprites, (600, 520), 3)
Steelers(allsprites, (350, 675), 2) # Quarterback
Steelers(allsprites, (50, 600), 1)
Steelers(allsprites, (200, 600), 1)
Steelers(allsprites, (350, 600), 1)
Steelers(allsprites, (500, 600), 1)
Steelers(allsprites, (650, 600), 1)
player = Player(allsprites, SPAWN_POSITION, TIME_LEFT)
Goal(allsprites, (25, 10))
Goal(allsprites, (175, 10))
Goal(allsprites, (325, 10))
Goal(allsprites, (475, 10))
Goal(allsprites, (625, 10))
score = 0
state = RUNNING
elif MENU_SELECTION == PLAY_SCREEN and state == RUNNING:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
for i in range(0, len(SOUNDS)):
sounds_list = list(SOUNDS.keys()) # Returns list of keys in SOUNDS
SOUNDS[sounds_list[i]].stop() # Stops all SOUNDS when go to menu
MENU_SELECTION = START_SCREEN
elif event.key == K_UP:
keys[0] = True
elif event.key == K_LEFT:
keys[1] = True
elif event.key == K_DOWN:
keys[2] = True
elif event.key == K_RIGHT:
keys[3] = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
keys[0] = False
elif event.key == pygame.K_LEFT:
keys[1] = False
elif event.key == pygame.K_DOWN:
keys[2] = False
elif event.key == pygame.K_RIGHT:
keys[3] = False
elif event.key == pygame.K_SPACE:
debug_message = 1
state = DEBUG
if keys[0]: # Up
SOUNDS["snd_leap"].play()
# Boundary, 32 is block, added a few extra pixels to make it look nicer
if player.pos[1] > 50:
player.pos[1] -= player.speed_y
keys[0] = False
if keys[2]: # Down
SOUNDS["snd_leap"].play()
if player.pos[1] < SCREEN_HEIGHT-95:
player.pos[1] += player.speed_y
keys[2] = False
if keys[1]: # Left
SOUNDS["snd_leap"].play()
if player.pos[0] > 32:
player.pos[0] -= player.speed_x
keys[1] = False
if keys[3]: # Right
SOUNDS["snd_leap"].play()
if player.pos[0] < SCREEN_WIDTH-75:
player.pos[0] += player.speed_x
keys[3] = False
# Draw and Update sprites on screen
allsprites.update()
screen.blit(football_field, (0, 0))
allsprites.draw(screen)
timer_text = arcade_font_main.render("TIME:", 1, (0, 0, 0))
screen.blit(timer_text, (SCREEN_WIDTH/2+170, SCREEN_HEIGHT-45))
timer_countdown_text = arcade_font_main.render(str(int(player.time_left)), 1, (0, 0, 0))
screen.blit(timer_countdown_text, (SCREEN_WIDTH-70, SCREEN_HEIGHT-45))
# Collisions
for enemy in Enemy.enemies:
if pygame.sprite.collide_mask(enemy, player):
for spr in allsprites:
spr.destroy()
state = RESTART
MENU_SELECTION = GAMEOVER_SCREEN
for goal in Goal.goals:
if pygame.sprite.collide_mask(goal, player) and goal.filled == 0:
goal.collision_with_player()
player.collision_with_goal()
score += 1
if player.time_left < 1:
MENU_SELECTION = GAMEOVER_SCREEN
for spr in allsprites:
spr.destroy()
state = RESTART
if score == 5:
MENU_SELECTION = WINNER_SCREEN
for spr in allsprites:
spr.destroy()
state = RESTART
pygame.display.flip()
elif MENU_SELECTION == GAMEOVER_SCREEN: # Gameover Screen
keys = [False, False, False, False]
SOUNDS["snd_playerdie"].play()
GameOver(screen, game_over, arial_font, score)
elif MENU_SELECTION == WINNER_SCREEN:
keys = [False, False, False, False]
Winner(screen, winner)
elif state == DEBUG and MENU_SELECTION == PLAY_SCREEN:
if debug_message == 1:
debug_message = 0
# USE BREAKPOINT HERE
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
state = RUNNING
if __name__ == '__main__':
main()