feat(game): volume from file

This commit is contained in:
Kristofers Solo 2024-01-07 16:52:58 +02:00
parent 8e3ed493e0
commit 37132f54e0
4 changed files with 21 additions and 9 deletions

View File

@ -15,6 +15,10 @@ ccw = ["ctrl", "z", "kp3", "kp7"] # counter-clockwise
hold = ["shift", "c", "kp0"]
drop = ["space", "kp5"]
[Sound]
music = true
volume = 0.5
[Volume.Music]
enabled = true
level = 0.01
[Volume.SFX]
enabled = true
level = 0.02

View File

@ -100,7 +100,10 @@ class Game(BaseScreen):
def _start_background_music(self) -> None:
"""Start playing background music."""
if self.game_mode is GameMode.PLAYER:
if (
self.game_mode is GameMode.PLAYER
and self.settings["Volume"]["Music"]["enabled"]
):
self.music = pygame.mixer.Sound(CONFIG.music.background)
self.music.set_volume(CONFIG.music.volume)
self.music.set_volume(self.settings["Volume"]["Music"]["level"])
self.music.play(-1)

View File

@ -337,13 +337,19 @@ class Tetris(BaseScreen):
def _initialize_sound(self) -> None:
"""Initialize game sounds."""
if self.game_mode is GameMode.PLAYER:
if (
self.game_mode is GameMode.PLAYER
and self.settings["Volume"]["SFX"]["enabled"]
):
self.landing_sound = pygame.mixer.Sound(CONFIG.music.landing)
self.landing_sound.set_volume(CONFIG.music.volume * 2)
self.landing_sound.set_volume(self.settings["Volume"]["SFX"]["level"])
def _play_landing_sound(self) -> None:
"""Play the landing sound effect."""
if self.game_mode is GameMode.PLAYER:
if (
self.game_mode is GameMode.PLAYER
and self.settings["Volume"]["SFX"]["enabled"]
):
self.landing_sound.play()
def _update_display_surface(self) -> None:

View File

@ -63,7 +63,6 @@ class Window:
class Music:
background: Path = BASE_PATH / "assets" / "music" / "background.wav"
landing: Path = BASE_PATH / "assets" / "music" / "landing.wav"
volume: float = 0.01
@define