feat: sound
- add SoundManager class to handle bgm and sfx - add script to generate wav sound assets - integrate sfx for shop purchases and player transform - add dynamic bgm switching based on player state - add sound file paths and volume configurations to settings
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import pygame
|
||||
from src.settings import (
|
||||
SFX_TOWER_SHOOT, SFX_MONSTER_HIT, SFX_BUY_SUCCESS,
|
||||
SFX_BUY_FAIL, SFX_MONSTER_HEALED, SFX_MONSTER_TRANSFORM,
|
||||
BGM_NORMAL, BGM_CREEPY, VOLUME_SFX, VOLUME_BGM
|
||||
)
|
||||
|
||||
class SoundManager:
|
||||
def __init__(self):
|
||||
pygame.mixer.init()
|
||||
self.effects = {}
|
||||
self.current_bgm = None
|
||||
|
||||
self._load_sfx("tower_shoot", SFX_TOWER_SHOOT)
|
||||
self._load_sfx("monster_hit", SFX_MONSTER_HIT)
|
||||
self._load_sfx("buy_success", SFX_BUY_SUCCESS)
|
||||
self._load_sfx("buy_fail", SFX_BUY_FAIL)
|
||||
self._load_sfx("monster_healed", SFX_MONSTER_HEALED)
|
||||
self._load_sfx("monster_transform", SFX_MONSTER_TRANSFORM)
|
||||
|
||||
# set volume via settings param
|
||||
for sound in self.effects.values():
|
||||
if sound:
|
||||
sound.set_volume(VOLUME_SFX)
|
||||
|
||||
def _load_sfx(self, name, path):
|
||||
try:
|
||||
self.effects[name] = pygame.mixer.Sound(path)
|
||||
except pygame.error:
|
||||
print(f"Could not find sound: {path}")
|
||||
self.effects[name] = None
|
||||
|
||||
def play_sfx(self, name):
|
||||
sound = self.effects.get(name)
|
||||
if isinstance(sound, pygame.mixer.Sound):
|
||||
sound.play()
|
||||
|
||||
# set BackGroundMusic dependent if monster is active or tutor is active
|
||||
def play_bgm(self, mode):
|
||||
if self.current_bgm == mode:
|
||||
return
|
||||
|
||||
path = BGM_NORMAL if mode == "normal" else BGM_CREEPY
|
||||
|
||||
try:
|
||||
pygame.mixer.music.load(path)
|
||||
pygame.mixer.music.set_volume(VOLUME_BGM)
|
||||
pygame.mixer.music.play(-1)
|
||||
self.current_bgm = mode
|
||||
except pygame.error:
|
||||
print(f"Could not find sound: {path}")
|
||||
Reference in New Issue
Block a user