-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.py
executable file
·54 lines (43 loc) · 1.24 KB
/
sound.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
54
import pygame, os, constants
class SoundManager:
def __init__(self, settings):
self.settings = settings
def trigger_music(self, name, volume=1.0):
if self.settings['music']:
play_music(name, volume)
def end_music(self):
stop_music()
def trigger_sound(self, name, volume=1.0):
if self.settings['sound']:
play_sound(name, volume)
sounds = {}
def play_music(name, volume=1.0):
fullname = os.path.join(constants.DATA_DIR, name)
pygame.mixer.music.load(fullname)
pygame.mixer.music.set_volume(volume)
pygame.mixer.music.play(-1)
def pause_music():
if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()
def unpause_music():
pygame.mixer.music.unpause()
def stop_music():
pygame.mixer.music.stop()
def play_sound(name, volume=1.0):
global sounds
if not name in sounds:
sounds[name] = load_sound(name)
sounds[name].set_volume(volume)
sounds[name].play()
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer:
return NoneSound()
fullname = os.path.join(constants.DATA_DIR, name + ".wav")
try:
sound = pygame.mixer.Sound(fullname)
except (pygame.error, message):
print("cannot load sound:", fullname)
raise SystemExit(message)
return sound