Skip to content

Commit

Permalink
Changed mute selector to 'sound' toggle switch
Browse files Browse the repository at this point in the history
Made the sound system a bit easier to understand by replacing
the .muted variable with .enabled
  • Loading branch information
halfburnttoast committed Sep 14, 2022
1 parent 3f610ae commit 9aea40b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
55 changes: 27 additions & 28 deletions dungeon_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from save_game import SaveFile
from debug_timer import debug_timer

VERSION = "v0.22.0-OPT_TEST_1"
VERSION = "v0.22.0-OPT_TEST_2"
G_LOG_LEVEL = logging.DEBUG
TILE_SIZE = 90
G_RESOLUTION = (TILE_SIZE * 9, TILE_SIZE * 9)
Expand Down Expand Up @@ -282,11 +282,11 @@ def load_save(self):
if data["VERSION"] == VERSION:

# sound settings
self._sound.muted = data["MUTE"]
if self._sound.muted:
self._sound.enabled = data["SOUND"]
if not self._sound.enabled:
self._sound.stop_music()
self._sound.muted = True
self._menu.get_widget("SOUND").set_value("Off")
self._sound.enabled = False
self._menu.get_widget("SOUND").set_value(False)

# colorblind mode settings
self._cb_mode = data["CB_MODE"]
Expand Down Expand Up @@ -321,7 +321,7 @@ def save_game(self):
save_data = {}
save_data['VERSION'] = VERSION
save_data['WINS'] = self._player_wins
save_data['MUTE'] = self._sound.muted
save_data['SOUND'] = self._sound.enabled
save_data['CB_MODE'] = self._cb_mode
save_data['PW_SAVE'] = self._power_save
save_data["LEVEL"] = self.get_puzzle_id()
Expand Down Expand Up @@ -351,9 +351,9 @@ def _menu_close(self):
self._menu_is_open = False
self._menu.disable()
self.needs_display_update = True
def _menu_set_mute(self, selection: tuple, val: bool) -> None:
self._sound.muted = val
if self._sound.muted:
def _menu_set_mute(self, val: bool) -> None:
self._sound.enabled = val
if not self._sound.enabled:
self._sound.stop_music()
else:
self._sound.play_next_background_song()
Expand Down Expand Up @@ -642,14 +642,25 @@ def _menu_build_main(self) -> pygame_menu.Menu:
menu.add.button('Resume', action=self._menu_close)
menu.add.button('Reset', action=self._menu_reset)
menu.add.button("Random Puzzle", action=self._menu_random_map)

# True and False specify the sound system variable _sound.muted, which is why they're backwards
self._menu_sound_selector = menu.add.selector(
"Sound: ",
[("Off", True), ["On", False]],
menu.add.vertical_fill(2)
menu.add.text_input(
'Puzzle ID: ',
default='00000',
maxchar=5,
valid_chars=[*'0123456789'],
onchange=self._menu_update_pid,
onreturn=self._menu_open_map,
background_color = (70, 50, 0),
textinput_id="PUZZLE_ID"
)
menu.add.button("Load Puzzle", action=self._menu_open_map)
menu.add.vertical_fill(2)
self._menu_sound_selector = menu.add.toggle_switch(
"Sound: ",
onchange=self._menu_set_mute,
default=1,
selector_id="SOUND"
default=True,
width=90,
toggleswitch_id="SOUND"
)
self._menu_colorblind_selector = menu.add.toggle_switch(
"Colorblind Mode: ",
Expand All @@ -666,18 +677,6 @@ def _menu_build_main(self) -> pygame_menu.Menu:
toggleswitch_id="PW_SAVE"
)
menu.add.vertical_fill(2)
menu.add.text_input(
'Puzzle ID: ',
default='00000',
maxchar=5,
valid_chars=[*'0123456789'],
onchange=self._menu_update_pid,
onreturn=self._menu_open_map,
background_color = (70, 50, 0),
textinput_id="PUZZLE_ID"
)
menu.add.button("Load Puzzle", action=self._menu_open_map)
menu.add.vertical_fill(2)
menu.add.button("How to Play", action=self._menu_tutorial)
menu.add.button("About", self._menu_about)
menu.add.button('Quit', self._menu_quit)
Expand Down
8 changes: 4 additions & 4 deletions sound_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, music_path: str):
self._playlist: list = []
self._volume: float = 0.5
self._music_path = music_path
self.muted = False
self.enabled = True
self.song_idx = 0

def load_music_all(self):
Expand All @@ -56,7 +56,7 @@ def play_next_background_song(self):
"""

if self._mixer_running and not mixer.get_init() is None:
if not self.muted:
if self.enabled:
song = self._playlist[self.song_idx]
logging.debug(f"Playing background song: {song}")
mixer.music.unload()
Expand All @@ -75,7 +75,7 @@ def stop_music(self):
mixer.music.stop()

def load_sfx(self, sound_effect_path: str, volume: float = 0.6) -> mixer.Sound:
if not self.muted and self._mixer_running:
if self.enabled and self._mixer_running:
try:
snd = mixer.Sound(resource_path(sound_effect_path))
snd.set_volume(0.6)
Expand All @@ -86,7 +86,7 @@ def load_sfx(self, sound_effect_path: str, volume: float = 0.6) -> mixer.Sound:

def play_sfx(self, sound_effect: mixer.Sound) -> None:
"""Wrap sfx calls in a try/except block."""
if not self.muted and self._mixer_running:
if self.enabled and self._mixer_running:
try:
sound_effect.play()
except pygame_error as e:
Expand Down

0 comments on commit 9aea40b

Please sign in to comment.