-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_fx.py
173 lines (153 loc) · 5.03 KB
/
sound_fx.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pygame
from pygame import mixer
from pathlib import Path
class SoundFx:
"""
Constructor/instance included so that a tracker can be used for sound options,
to turn off and on the sound & music.
"""
def __init__(self):
"""
is_running keeps track of whether music & sound is on to
allow sound option to turn off or on.
Set initial background music and sounds to medium level.
"""
self.__is_running = True
pygame.init()
mixer.music.set_volume(0.4)
mixer.Channel(0).set_volume(0.75)
@property
def is_running(self):
"""
Getter for is_running status of all sounds
:return: status of whether sounds are on
:rtype: bool
"""
return self.__is_running
@is_running.setter
def is_running(self, change):
"""
Setter for is_running status of all sounds
:param change: switching off or on of all sounds
:type change: bool
:raises: if param is not a boolean
"""
if isinstance(change, bool):
self.__is_running = change
else:
raise TypeError("Only boolean param accepted")
def turn_off(self):
"""
Turn off all sounds
"""
self.__is_running = False
mixer.quit()
def turn_on(self, in_game=False):
"""
Turn on all sounds, play intro music or in-game music depending
on when this is called
:param in_game: whether this is accessed at intro or in-game
:type in_game: bool
:raises: if param is not a boolean type
"""
if isinstance(in_game, bool):
self.__is_running = True
mixer.init()
mixer.music.set_volume(0.4)
if in_game:
self.in_game()
else:
self.intro()
else:
raise TypeError("Only boolean param accepted")
def low_volume(self):
"""
Set to low volume
"""
if self.__is_running:
mixer.music.set_volume(0.1)
mixer.Channel(0).set_volume(0.25)
def normal_volume(self):
"""
Set to normal/default volume
"""
if self.__is_running:
mixer.music.set_volume(0.4)
mixer.Channel(0).set_volume(0.75)
def high_volume(self):
"""
Set to high volume
"""
if self.__is_running:
mixer.music.set_volume(0.7)
mixer.Channel(0).set_volume(1)
def intro(self):
"""
Play intro music. Song obtained by Pranav, shorted by Darance
"""
if self.__is_running:
mixer.music.load(Path('sound/Mysterious Strange Things - Yung Logos_short.mp3'))
mixer.music.play(-1)
def in_game(self):
"""
Play in-game music. Song obtained by Pranav, shorted by Darance
"""
if self.__is_running:
mixer.music.load(Path('sound/Subterranean Howl - ELPHNT_short.mp3'))
mixer.music.play(-1)
def pause_menu(self, resume=False):
"""
Pause music while in pause menu
:param resume: allow resuming game music
:type resume: bool
"""
if resume:
mixer.music.unpause()
else:
mixer.music.pause()
def lose(self):
"""
Music with player loses, obtained by Manny
"""
if self.__is_running:
mixer.music.load(Path('sound/gameover.mp3'))
mixer.music.play(-1)
def win(self):
"""
Music with player wins, obtained by Manny
"""
if self.__is_running:
mixer.music.load(Path('sound/win.mp3'))
mixer.music.play(-1)
def enter_room(self):
"""
Sound for entering each room
Credit https://stackoverflow.com/questions/53617967/play-music-and-sound-effects-on-top-of-each-other-pygame
"""
if self.__is_running:
# play a sound on channel 0 with a max time of 600 milliseconds
mixer.Channel(0).play(pygame.mixer.Sound(Path('sound', 'enter_room.mp3')), maxtime=600)
def pillar(self):
"""
Sound for collecting pillar key, obtained by Manny
"""
if self.__is_running:
mixer.Channel(1).play(pygame.mixer.Sound(Path('sound', 'pillar.mp3')))
def pit(self):
"""
Sound for falling into pit, obtained by Manny, edited by Darence
"""
if self.__is_running:
mixer.Channel(1).play(pygame.mixer.Sound(Path('sound', 'pit.mp3')))
def health_potion(self):
"""
Sound for consuming health potion, obtained by Pranav
"""
if self.__is_running:
mixer.Channel(1).play(pygame.mixer.Sound(Path('sound', 'health_potion.mp3')))
def vision_potion(self):
"""
Sound for effects of vision potion, obtained by Pranav
"""
if self.__is_running:
mixer.Channel(1).play(pygame.mixer.Sound(Path('sound', 'vision_potion.mp3')))