blob: d6032c915055d8bd2df909a008ae223ecd6714aa (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# -*- coding: utf-8 -*-
# Copyright (C) 2011 ceibalJAM! - ceibaljam.org
# This file is part of Saludame.
#
# Saludame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Saludame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Saludame. If not, see <http://www.gnu.org/licenses/>.
import pygame
music = {
"happy": "assets/music/happy.ogg",
"normal": "assets/music/normal.ogg",
"sad": "assets/music/sad.ogg",
"angry": "assets/music/angry.ogg",
"sick": "assets/music/sick.ogg",
}
instance = None
class SoundManager:
IDLE = 0
PLAYING = 1
FADE_OUT = 2
FADE_IN = 3
def __init__(self):
global instance
instance = self
self.current_music_name = ""
self.next_music_name = ""
self.state = SoundManager.IDLE
self.volume = 0.3
def set_volume(self, value):
self.volume = value
pygame.mixer.music.set_volume(self.volume)
def set_music(self, name):
if self.state == SoundManager.IDLE:
self.current_music_name = name
self.state = SoundManager.PLAYING
self.start_playing()
if self.state == SoundManager.PLAYING:
if name <> self.current_music_name:
self.state = SoundManager.FADE_OUT
self.next_music_name = name
pygame.mixer.music.fadeout(1000)
elif self.state == SoundManager.FADE_OUT:
if name == self.current_music_name:
self.state = SoundManager.FADE_IN
self.next_music_name = ""
else:
self.next_music_name = name
elif self.state == SoundManager.FADE_IN:
if name <> self.current_music_name:
self.state = SoundManager.FADE_OUT
self.next_music_name = name
def start_playing(self):
global music
if self.next_music_name:
self.current_music_name = self.next_music_name
self.next_music_name = None
path = music[self.current_music_name]
pygame.mixer.music.load(path)
pygame.mixer.music.set_volume(self.volume)
pygame.mixer.music.play()
# Sounds
def play_time_change(self):
pygame.mixer.Sound("assets/sound/time_change.ogg").play()
def play_event_solved(self):
pygame.mixer.Sound("assets/sound/challenge_win.ogg").play()
def play_forbidden_action(self):
pygame.mixer.Sound("assets/sound/alert.ogg").play(2)
|