-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsound_manager.py
53 lines (45 loc) · 1.37 KB
/
sound_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pygame
class SoundManager:
"""
Static class to handle loading of pygame surfaces to improve performance
"""
initialized = False
sounds = None
@staticmethod
def init():
SoundManager.initialized = True
SoundManager.sounds = {}
@staticmethod
def check_initialized():
if not SoundManager.initialized:
raise Exception("Must call ImageHandler.init() before any other methods.")
@staticmethod
def clear(path):
"""
Forgets one thing.
:param path: The path of the file to remove from memory
:return:
"""
SoundManager.check_initialized()
if path in SoundManager.sounds:
del SoundManager.sounds[path]
@staticmethod
def clear_all():
"""
Forgets everything
"""
SoundManager.check_initialized()
SoundManager.sounds = {}
@staticmethod
def load(path):
"""
Loads a surface from file or from cache
:param path: The path of the image
:return: The surface. This is likely the same reference others are using, so don't be destructive.
"""
SoundManager.check_initialized()
if path in SoundManager.sounds:
return SoundManager.sounds[path]
sound = pygame.mixer.Sound(path)
SoundManager.sounds[path] = sound
return sound