From f4a064c1f36c7ab7993b30832dea386e4b79b49d Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Sat, 20 Apr 2024 22:09:23 +0300 Subject: sample loading and laser sound --- src/sliceitoff/sfx/sfx.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/sliceitoff/sfx') diff --git a/src/sliceitoff/sfx/sfx.py b/src/sliceitoff/sfx/sfx.py index f9347df..15dd1a6 100644 --- a/src/sliceitoff/sfx/sfx.py +++ b/src/sliceitoff/sfx/sfx.py @@ -1,10 +1,12 @@ """ sfx.sfx - pygame.mixer initialization and sound effects handling """ +from pathlib import Path import pygame class Sfx: """ Sound Effects and Music? """ def __init__(self): self.initialized = False + self.sound = {} try: pygame.mixer.pre_init(channels=2, buffer=512, frequency=48000) except pygame.error: @@ -16,9 +18,16 @@ class Sfx: try: pygame.mixer.init() self.initialized = True + for mp3_file in Path(base_path).glob('*.mp3'): + self.sound[str(mp3_file.stem)] = pygame.mixer.Sound(mp3_file) except pygame.error: pass + def play(self, sample): + """ Just plays named sample loaded from assets directory """ + if self.initialized: + self.sound[sample].play() + # Initialize only one time try: # pylint: disable = used-before-assignment -- cgit v1.2.3