-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_manager.py
More file actions
327 lines (274 loc) · 11.7 KB
/
game_manager.py
File metadata and controls
327 lines (274 loc) · 11.7 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
import pygame
from pytmx import load_pygame
from map.map_manager import MapManager
from objects.box import Box
from objects.breakable_block import BreakableBlock
from player.player import Player
from enemies.enemy import Enemy
from enemies.boss import Boss
from objects.coin import Coin
from ui.hud import HUD
from core.setting import *
from core.background import BackgroundLayer
class GameManager:
STATE_PLAYING = "PLAYING"
STATE_GAME_OVER = "GAME_OVER"
def __init__(self, screen, mute):
self.screen = screen
self.clock = pygame.time.Clock()
self.state = GameManager.STATE_PLAYING
self.score = 0
self.coin_count = 0
self.background = BackgroundLayer(SCREEN_WIDTH,SCREEN_HEIGHT)
self.map_manager = MapManager("assets/maps/pirate.tmx",screen)
self.player = Player(6*32, 15*32)
self.enemies = pygame.sprite.Group(
Enemy(54*32, 13*32), Enemy(77*32, 5*32), Enemy(142*32, 12*32),
Enemy(145*32, 12*32), Enemy(32*32, 13*32), Enemy(49*32, 19*32),
Enemy(95*32, 17*32) ,Enemy(112*32, 17*32), Enemy(119*32, 17*32),
Enemy(117*32, 8*32)
)
self.boss = pygame.sprite.Group(
Boss(151*32, 17*32)
)
self.boxes = pygame.sprite.Group(
Box(15*32, 9*32), Box(25*32, 11*32, Box.ITEM_STAR),Box(50*32, 6*32), Box(106*32, 11*32, Box.ITEM_STAR)
)
self.blocks = pygame.sprite.Group(
#BreakableBlock(300, 400), BreakableBlock(350, 400)
)
self.coins = pygame.sprite.Group(
Coin(76*32, 3*32), Coin(77*32, 3*32), Coin(78*32, 3*32),Coin(102*32, 16*32),
Coin(103*32, 16*32), Coin(121*32, 18*32),Coin(105*32, 18*32),
Coin(140*32, 16*32), Coin(142*32, 18*32),Coin(144*32, 18*32),
)
self.stars = pygame.sprite.Group()
self.projectiles = pygame.sprite.Group()
self.obstacles = list(self.map_manager.tiles) + [box.rect for box in self.boxes]
self.hud = HUD(self.player, game_manager=self)
# === AUDIO ===
pygame.mixer.init()
# Nhạc nền
pygame.mixer.music.load("assets/audio/background.mp3")
pygame.mixer.music.set_volume(MUSIC_VOLUME)
if AUDIO_ENABLED:
pygame.mixer.music.play(-1)
self.is_muted = mute
self.hud.is_muted = mute
if self.is_muted:
pygame.mixer.music.pause()
pygame.mixer.pause()
else:
pygame.mixer.music.unpause()
pygame.mixer.unpause()
# SFX
self.sfx_explosion = pygame.mixer.Sound("assets/audio/explosion.mp3")
self.sfx_explosion.set_volume(SFX_VOLUME)
self.sfx_die = pygame.mixer.Sound("assets/audio/8-bit-die.mp3")
self.sfx_die.set_volume(SFX_VOLUME)
self.sfx_game_over = pygame.mixer.Sound("assets/audio/8-bit-game-over.mp3")
self.sfx_game_over.set_volume(0.8)
self.sfx_jump = pygame.mixer.Sound("assets/audio/pixel-jump.mp3")
self.sfx_jump.set_volume(SFX_VOLUME)
self.sfx_powerup = pygame.mixer.Sound("assets/audio/powerup.mp3")
self.sfx_powerup.set_volume(SFX_VOLUME)
self.sfx_walk = pygame.mixer.Sound("assets/audio/8-bit-walk.mp3")
self.sfx_walk.set_volume(WALK_VOLUME)
# looping walk
self.walk_channel = pygame.mixer.Channel(5)
self.game_over_played = False
self.music_paused_for_powerup = False
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
result = self.hud.handle_event(event)
if result == "home":
return "home"
if self.state == GameManager.STATE_GAME_OVER:
if event.type == pygame.KEYDOWN and event.key in (
pygame.K_RETURN,
pygame.K_SPACE,
):
self.reset_level()
return True
def update(self, dt):
if self.hud.is_paused:
return
if self.state == GameManager.STATE_PLAYING:
keys = pygame.key.get_pressed()
self.obstacles = list(self.map_manager.tiles) + [box.rect for box in self.boxes] + [block.rect for block in self.blocks]
self.player.update(keys, dt, self.obstacles)
self.background.update(dt)
self.map_manager.update(dt)
self.enemies.update(keys, dt, self.obstacles, self.player)
self.boss.update(keys, dt, self.obstacles, self.player, self)
self.boxes.update(dt, self.map_manager.tiles,self.player, self)
self.coins.update(dt, self.map_manager.tiles, self.player, self)
self.stars.update(dt, self.map_manager.tiles, self.player, self)
self.blocks.update(self.player, self)
self.projectiles.update(dt, self.obstacles, self.player)
if self.hud.is_muted or not AUDIO_ENABLED:
self.is_muted = True
else:
self.is_muted = False
# Pause background music during power-up
if self.player.star_powered and not self.music_paused_for_powerup:
try:
pygame.mixer.music.pause()
except Exception:
pass
self.music_paused_for_powerup = True
elif (not self.player.star_powered) and self.music_paused_for_powerup and self.state == GameManager.STATE_PLAYING:
if not self.is_muted:
try:
pygame.mixer.music.unpause()
except Exception:
pass
self.music_paused_for_powerup = False
# Jump SFX (rising edge)
if getattr(self.player, 'just_jumped', False):
self.play_sfx("jump")
# Walk loop control
moving_horizontally = abs(self.player.vel.x) > 1 and self.player.on_ground
if moving_horizontally and not self.walk_channel.get_busy() and not self.is_muted:
try:
self.walk_channel.play(self.sfx_walk, loops=-1)
except Exception:
pass
if (not moving_horizontally or self.is_muted) and self.walk_channel.get_busy():
self.walk_channel.stop()
for enemy in list(self.enemies): # copy since enemy may kill() itself
died, score_value = enemy.handle_player_collision(self.player)
if died:
self.score += score_value
self.play_sfx("die")
for boss in list(self.boss):
died, score_value = boss.handle_player_collision(self. player)
if died:
self.score += score_value
if self.player.health <= 0:
if not self.game_over_played:
self.play_sfx("game_over")
try:
pygame.mixer.music.fadeout(500)
except Exception:
pass
self.game_over_played = True
self.state = GameManager.STATE_GAME_OVER
self.map_manager.update_camera(self.player.rect)
def draw(self):
self.screen.fill((92, 148, 252))
self.background.draw(self.screen)
self.map_manager.draw(self.screen)
self.player.draw(self.screen, self.map_manager.camera_offset)
for enemy in self.enemies:
enemy.draw(self.screen, self.map_manager.camera_offset)
for boss in self.boss:
boss.draw(self.screen, self.map_manager.camera_offset)
for box in self.boxes:
box.draw(self.screen, self.map_manager.camera_offset)
for coin in self.coins:
self.screen.blit(coin.image, coin.rect.move(-self.map_manager.camera_offset.x, 0))
for star in self.stars:
self.screen.blit(star.image, star.rect.move(-self.map_manager.camera_offset.x, 0))
for block in self.blocks:
self.screen.blit(block.image, block.rect.move(-self.map_manager.camera_offset.x, 0))
for projectile in self.projectiles:
projectile.draw(self.screen, self.map_manager.camera_offset)
self.hud.draw(self.screen)
if self.state == GameManager.STATE_GAME_OVER:
self._draw_game_over()
pygame.display.flip()
def _draw_game_over(self):
self.hud.draw_game_over_panel(self.screen)
def reset_level(self):
"""Reset the entire level (score to 0, recreate world)."""
self.score = 0
self.coin_count = 0
self.state = GameManager.STATE_PLAYING
self.background = BackgroundLayer(SCREEN_WIDTH, SCREEN_HEIGHT)
self.map_manager = MapManager("assets/maps/pirate.tmx", self.screen)
self.player = Player(6 * 32, 15 * 32)
self.enemies = pygame.sprite.Group(
Enemy(54 * 32, 13 * 32),
Enemy(77 * 32, 5 * 32),
Enemy(142 * 32, 12 * 32),
Enemy(145 * 32, 12 * 32),
Enemy(32 * 32, 13 * 32),
Enemy(49 * 32, 19 * 32),
Enemy(95 * 32, 17 * 32),
Enemy(112 * 32, 17 * 32),
Enemy(119 * 32, 17 * 32),
Enemy(117 * 32, 8 * 32),
)
self.boss = pygame.sprite.Group(Boss(151 * 32, 17 * 32))
self.boxes = pygame.sprite.Group(
Box(15 * 32, 9 * 32),
Box(25 * 32, 11 * 32, Box.ITEM_STAR),
Box(50 * 32, 6 * 32),
Box(106 * 32, 11 * 32, Box.ITEM_STAR),
)
self.blocks = pygame.sprite.Group()
self.coins = pygame.sprite.Group(
Coin(76 * 32, 3 * 32),
Coin(77 * 32, 3 * 32),
Coin(78 * 32, 3 * 32),
Coin(102 * 32, 16 * 32),
Coin(103 * 32, 16 * 32),
Coin(121 * 32, 18 * 32),
Coin(105 * 32, 18 * 32),
Coin(140 * 32, 16 * 32),
Coin(142 * 32, 18 * 32),
Coin(144 * 32, 18 * 32),
)
self.stars = pygame.sprite.Group()
self.projectiles = pygame.sprite.Group()
self.obstacles = list(self.map_manager.tiles) + [box.rect for box in self.boxes]
self.hud = HUD(self.player, game_manager=self)
self.game_over_played = False
self.music_paused_for_powerup = False
# Sync mute state to settings/HUD
self.is_muted = self.hud.is_muted or (not AUDIO_ENABLED)
# Ensure background music restarts cleanly
try:
pygame.mixer.music.stop()
pygame.mixer.music.load("assets/audio/background.mp3")
pygame.mixer.music.set_volume(MUSIC_VOLUME)
if not self.is_muted:
pygame.mixer.music.play(-1)
except Exception:
pass
# Stop walking loop
try:
if self.walk_channel.get_busy():
self.walk_channel.stop()
except Exception:
pass
def play_sfx(self, key):
if self.is_muted:
return
mapping = {
"explosion": self.sfx_explosion,
"die": self.sfx_die,
"game_over": self.sfx_game_over,
"jump": self.sfx_jump,
"powerup": self.sfx_powerup,
}
sound = mapping.get(key)
if sound is not None:
try:
sound.play()
except Exception:
pass
def run(self):
running = True
while running:
dt = self.clock.tick(FPS) / 1000.0
running = self.handle_events()
if running == "home":
return "home" # báo về main rằng cần quay lại start
if not running:
break
self.update(dt)
self.draw()