diff options
author | Viljami Ilola <+@hix.fi> | 2024-04-27 01:29:45 +0300 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-04-27 01:29:45 +0300 |
commit | 7a33e5020f9b1e7cf89bc532e2ddcf681f5bcf9c (patch) | |
tree | 544e868288f05213452dec9b7b789bd1eb208191 /src/sliceitoff/sfx/sfx.py | |
parent | a4794437d561a01c8bea12d59502e4cc72acfbea (diff) |
volume control
Diffstat (limited to 'src/sliceitoff/sfx/sfx.py')
-rw-r--r-- | src/sliceitoff/sfx/sfx.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/sliceitoff/sfx/sfx.py b/src/sliceitoff/sfx/sfx.py index fd494b8..8f3371e 100644 --- a/src/sliceitoff/sfx/sfx.py +++ b/src/sliceitoff/sfx/sfx.py @@ -1,8 +1,11 @@ """ sfx.sfx - pygame.mixer initialization and sound effects handling """ import os +from random import choice from pathlib import Path import pygame +from sliceitoff.settings import settings + DEBUG = os.getenv("DEBUG") class Sfx: @@ -12,6 +15,14 @@ class Sfx: self.sound = {} self.bgm = None try: + self.sfx_volume = int( + settings.get_value_or_default("sfx_volume")) + self.music_volume = int( + settings.get_value_or_default("music_volume")) + except ValueError: + self.sfx_volume = 10 + self.music_volume = 10 + try: pygame.mixer.pre_init(channels=2, buffer=512, frequency=48000) except pygame.error: pass @@ -29,9 +40,24 @@ class Sfx: except pygame.error: pass + def sfx_up(self): + """ Turn volume up. If its turned to 11 it resets back to 0. """ + self.sfx_volume += 1 + self.sfx_volume %= 11 + settings.replace_value("sfx_volume", self.sfx_volume) + self.play(choice(("baby", "laser", "glass"))) + + def music_up(self): + """ Turn volume up. If its turned to 11 it resets back to 0. """ + self.music_volume += 1 + self.music_volume %= 11 + settings.replace_value("music_volume", self.music_volume) + self.sound[self.bgm].set_volume(self.music_volume / 10) + def play(self, sample): """ Just plays named sample loaded from assets directory """ if self.initialized: + self.sound[sample].set_volume(self.sfx_volume / 10) self.sound[sample].play() def music(self, music): @@ -44,6 +70,7 @@ class Sfx: self.sound[self.bgm].fadeout(500) self.bgm = music if self.bgm: + self.sound[self.bgm].set_volume(self.music_volume / 10) self.sound[self.bgm].play() # Initialize only one time |