summaryrefslogtreecommitdiff
path: root/src/sliceitoff/sfx/sfx.py
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-04-26 12:08:06 +0300
committerViljami Ilola <+@hix.fi>2024-04-26 12:08:06 +0300
commit54a17d969c85e150486e0960e90ab166e945c359 (patch)
treef76e57dddb2ad92fc043c48ea1bb118207b06c73 /src/sliceitoff/sfx/sfx.py
parent2d94bb60ff2fea09097d0e33dfd8b92aeef55531 (diff)
initial music
Diffstat (limited to 'src/sliceitoff/sfx/sfx.py')
-rw-r--r--src/sliceitoff/sfx/sfx.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/sliceitoff/sfx/sfx.py b/src/sliceitoff/sfx/sfx.py
index 15dd1a6..fd494b8 100644
--- a/src/sliceitoff/sfx/sfx.py
+++ b/src/sliceitoff/sfx/sfx.py
@@ -1,12 +1,16 @@
""" sfx.sfx - pygame.mixer initialization and sound effects handling """
+import os
from pathlib import Path
import pygame
+DEBUG = os.getenv("DEBUG")
+
class Sfx:
""" Sound Effects and Music? """
def __init__(self):
self.initialized = False
self.sound = {}
+ self.bgm = None
try:
pygame.mixer.pre_init(channels=2, buffer=512, frequency=48000)
except pygame.error:
@@ -20,6 +24,8 @@ class Sfx:
self.initialized = True
for mp3_file in Path(base_path).glob('*.mp3'):
self.sound[str(mp3_file.stem)] = pygame.mixer.Sound(mp3_file)
+ if DEBUG:
+ print("Loading sound:", mp3_file, str(mp3_file.stem))
except pygame.error:
pass
@@ -28,6 +34,18 @@ class Sfx:
if self.initialized:
self.sound[sample].play()
+ def music(self, music):
+ """ Plays sample as music. There is only one music at the time """
+ if not self.initialized:
+ return
+ if self.bgm == music:
+ return
+ if self.bgm:
+ self.sound[self.bgm].fadeout(500)
+ self.bgm = music
+ if self.bgm:
+ self.sound[self.bgm].play()
+
# Initialize only one time
try:
# pylint: disable = used-before-assignment