-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsoundmanager.py
58 lines (53 loc) · 2.04 KB
/
soundmanager.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 pygame
class SoundManager:
def __init__(self):
pygame.mixer.init()
self.soundVolume = 0.4
self.musicVolume = 0 #0.2
self.targetMusicVolume = 0 #0.2
self.nextMusic = None
self.currentMusic = None
self.sounds = {
'jump' : pygame.mixer.Sound('sounds/03_Jump_v2.ogg'),
'coin' : pygame.mixer.Sound('sounds/01_Coin Pickup_v2.ogg')
}
self.music = {
'solace' : 'music/23 Solace.ogg',
'dawn' : 'music/03 Before the Dawn.ogg'
}
def playSound(self, soundName):
self.sounds[soundName].set_volume(self.soundVolume)
self.sounds[soundName].play()
def playMusic(self, musicName):
# don't play the music if already playing
if musicName is self.currentMusic:
return
pygame.mixer.music.load(self.music[musicName])
pygame.mixer.music.set_volume(self.musicVolume)
self.currentMusic = musicName
pygame.mixer.music.play(-1)
def playMusicFade(self, musicName):
# don't play the music if already playing
if musicName is self.currentMusic:
return
# add music to queue
self.nextMusic = musicName
# fade out current music
self.fadeOut()
def fadeOut(self):
pygame.mixer.music.fadeout(500)
def update(self):
# raise volume if lower than target
if self.musicVolume < self.targetMusicVolume:
self.musicVolume = min(self.musicVolume + 0.005, self.targetMusicVolume)
pygame.mixer.music.set_volume(self.musicVolume)
# play next music if appropriate
if self.nextMusic is not None:
# if 'old' music has finished fading out
if not pygame.mixer.music.get_busy():
self.currentMusic = None
self.musicVolume = 0
pygame.mixer.music.set_volume(self.musicVolume)
self.playMusic(self.nextMusic)
# remove from music queue
self.nextMusic = None