-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.py
58 lines (47 loc) · 1.47 KB
/
audio.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
55
56
57
58
import os
import pygame
from utils import Settings
from random import shuffle
SONG_END = pygame.USEREVENT + 1
class Sound:
sounds = {}
music = []
# music = {}
@staticmethod
def init():
pygame.mixer.init()
for s in os.listdir('snd'):
Sound.sounds[s[:s.rindex('.')]] = pygame.mixer.Sound('snd/' + s)
@staticmethod
def reloadMusic():
Sound.music = ['music/' + s for s in os.listdir('music')]
shuffle(Sound.music)
@staticmethod
def play(name):
if name in Sound.sounds:
if Settings.AUDIO == 1:
Sound.sounds[name].play()
else:
print('ERROR: Sound "{}" not found'.format(name))
@staticmethod
def stop(name, fade):
if name in Sound.sounds:
if fade and Settings.AUDIO == 1:
Sound.sounds[name].fadeout()
else:
Sound.sounds[name].stop()
else:
print('ERROR: Sound "{}" not found'.format(name))
@staticmethod
def stop_all(fade):
map(lambda name: Sound.stop(name, fade), Sound.sounds.keys())
pygame.mixer.music.stop()
@staticmethod
def cycleMusic():
if Settings.AUDIO == 1:
if len(Sound.music) == 0:
Sound.reloadMusic()
if len(Sound.music) > 0:
pygame.mixer.music.load(Sound.music.pop(0))
pygame.mixer.music.set_endevent(SONG_END)
pygame.mixer.music.play()