Skip to content

Instantly share code, notes, and snippets.

@pydsigner
Last active June 7, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pydsigner/231c0812f9f91050dd83c744d6d5dc4b to your computer and use it in GitHub Desktop.
Save pydsigner/231c0812f9f91050dd83c744d6d5dc4b to your computer and use it in GitHub Desktop.
Example For Loading Pygame Sounds in Background
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