Last active
June 7, 2018 05:55
-
-
Save pydsigner/231c0812f9f91050dd83c744d6d5dc4b to your computer and use it in GitHub Desktop.
Example For Loading Pygame Sounds in Background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import thread # _thread in Python 3 | |
class FakeSound: | |
def play(self, *args, **kw): | |
pass | |
class SoundStore: | |
def __init__(self, sound_mapping): | |
self.sound_mapping = sound_mapping | |
self._sounds = {name: FakeSound() for name in sound_mapping} | |
self.loading_complete = False | |
def play_sound(self, name, *args, **kw): | |
self._sounds[name].play(*args, **kw) | |
def load_sounds(self): | |
for name, path in self.sound_mapping: | |
self._sounds[name] = pygame.mixer.Sound(path) | |
self.loading_complete = True | |
def load_sounds_background(self): | |
thread.start_new_thread(self.load_sounds, ()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment