-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
178 lines (144 loc) · 5.68 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
177
178
import os
import pygame
from const import *
from utils import *
from sprites import *
from pygame.locals import *
from level import Level
def main():
if any(folder not in os.listdir() for folder in ('images', 'map', 'sounds')):
try:
os.chdir('game_project')
except:
print('The main folder/file name needs to be called "game_project" for the game to run.')
return
pygame.mixer.pre_init(frequency=22050, size=-16, channels=2, buffer=512)
pygame.init()
pygame.display.set_caption('MegaMan: Pump Man Stage')
pygame.mixer.music.load('sounds/level.mp3')
pygame.mixer.music.play(-1)
buster_sound = pygame.mixer.Sound('sounds/buster.wav')
bg = pygame.Surface(SCREEN_SIZE)
screen = pygame.display.set_mode(SCREEN_SIZE)
fps = pygame.time.Clock()
bg.fill((0,0,0))
shot_img = pygame.image.load('images/shot.png').convert_alpha()
death_img = pygame.image.load('images/death.png').convert_alpha()
win_img = pygame.image.load('images/win.png').convert_alpha()
splash_img = pygame.image.load('images/splash.png').convert_alpha()
instructions_img = pygame.image.load('images/instructions.png').convert_alpha()
enemy_img = {
d: [pygame.image.load(f'images/enemy/{d}/{n + 1}.png').convert_alpha()
for n in range(4)]
for d in (FWD, BACK)
}
player = Player(screen=screen)
all_shots = pygame.sprite.Group()
enemies = pygame.sprite.Group()
with open('map/walls.txt', 'r') as f:
walls = [eval(f'({r})') for r in f.read().split('\n')[1:] if r != '']
with open('map/traps.txt', 'r') as f:
traps = [eval(f'({r})') for r in f.read().split('\n') if r != '']
with open('map/ladders.txt', 'r') as f:
ladders = [eval(f'({r})') for r in f.read().split('\n') if r != '']
with open('map/grid.txt', 'r') as f:
grid = List([List(eval(f'[{l}]')) for l in f.read().split('\n') if l != ''])
with open('map/spawn_zones.txt', 'r') as f:
spawn_zones = [eval(f'({r})') for r in f.read().split('\n') if r != '']
level = Level(
image = 'map/image.png',
player = player,
display = screen,
walls = walls,
ladders = ladders,
traps = traps,
spawn_zones = spawn_zones,
grid = grid,
shots = all_shots,
enemies = enemies,
enemy_img = enemy_img
)
pygame.time.set_timer(ANIMATE, 120)
pygame.time.set_timer(SPAWN, 3000)
intro = {
'splash': {'run': True, 'image': splash_img},
'instructions': {'run': True, 'image': instructions_img}
}
for thing in intro:
imgs = [(level.image, (0,0)), (intro[thing]['image'], (0,0))]
while intro[thing]['run']:
fps.tick(30)
for event in pygame.event.get():
if event.type == KEYDOWN:
intro[thing]['run'] = False
screen.blits(imgs)
pygame.display.flip()
# game loop
game_running = True
while game_running:
while not level.if_dead and not level.if_win:
fps.tick(60)
level.move(pygame.key.get_pressed())
all_shots.update()
enemies.update()
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
game_running = False
return level.debug_info
elif event.type == QUIT:
game_running = False
return level.debug_info
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
all_shots.add(Shot(shot_img, player, SCREEN_SIZE[0]))
player.buster = BUSTER
buster_sound.play()
elif event.type == ANIMATE:
player.update()
enemies.update(True)
elif event.type == SPAWN:
level.spawn()
screen.blit(bg, (0,0))
screen.blit(level.image, level.rect)
if DEBUG:
screen.blits(
sum([rect_to_surface(r, c)
for r, c in ((level.spawn_zones, (255,255,255)),
(level.traps, (255, 0, 0)),
(level.walls, (0,0,255)),
(level.ladders, (0,255,0)),
(level.ladder_tops, (255,0,255)))],
[])
)
screen.blit(player.image, player.img_point)
enemies.draw(screen)
all_shots.draw(screen)
if DEBUG:
pygame.display.set_caption(f'{int(fps.get_fps())} FPS; {level.x}, {level.y}')
else:
pygame.display.set_caption(f'{int(fps.get_fps())} FPS')
pygame.display.flip()
imgs = [(bg, (0,0)), (level.image, level.rect), (death_img, (0,0))]
while level.if_dead:
fps.tick(30)
screen.blits(imgs)
pygame.display.flip()
for event in pygame.event.get():
if event.type == KEYDOWN:
level.reset()
player.reset()
level.if_dead = False
imgs[2] = (player.image, player.img_point)
imgs.append((win_img, (0,0)))
while level.if_win:
fps.tick(30)
screen.blits(imgs)
pygame.display.flip()
for event in pygame.event.get():
if event.type == KEYDOWN:
level.reset()
player.reset()
level.if_win = False
if DEBUG:
print(main())
else:
main()