Skip to content

Commit

Permalink
Mute is now togglable, setting stored in savefile
Browse files Browse the repository at this point in the history
Sound can now be toggled from the menu. The _sound.muted variable
is saved in the save file under dict key "MUTE"
  • Loading branch information
halfburnttoast committed Sep 12, 2022
1 parent cd8178d commit b9ffa20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 22 additions & 11 deletions dungeon_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from resource_path import resource_path
from save_game import SaveFile

VERSION = "v0.20.1"
VERSION = "v0.21.0"
G_LOG_LEVEL = logging.INFO
TILE_SIZE = 90
G_RESOLUTION = (TILE_SIZE * 9, TILE_SIZE * 9)
Expand All @@ -68,7 +68,6 @@ def __init__(self, screen, sound):
self._screen: pygame.Surface = screen
self._sound: sound_handler.SoundHandler = sound
self._save_file = SaveFile("dungeon_cross.sav")
self._mute_always = False

# Create Menus
self._menu_theme = self._menu_build_theme()
Expand Down Expand Up @@ -232,8 +231,8 @@ def load_save(self):
data = self._save_file.get_save_data()
self._player_wins = data["WINS"]
if data["VERSION"] == VERSION:
self._mute_always = data["MUTE_ALWAYS"]
if self._mute_always:
self._sound.muted = data["MUTE"]
if self._sound.muted:
self._sound.stop_music()
self._sound.muted = True
self.open_puzzle(data["LEVEL"])
Expand All @@ -254,7 +253,7 @@ def save_game(self):
save_data = {}
save_data['VERSION'] = VERSION
save_data['WINS'] = self._player_wins
save_data['MUTE_ALWAYS'] = self._mute_always
save_data['MUTE'] = self._sound.muted
save_data["LEVEL"] = self.get_puzzle_id()
save_data["PROGRESS"] = self.placed_walls
save_data["MAPHASH"] = self._map_hash
Expand All @@ -281,9 +280,15 @@ def _menu_update_pid(self, value):
def _menu_close(self):
self._menu_is_open = False
self._menu.disable()
def _menu_mute(self):
self._sound.stop_music()
self._sound.muted = True
# def _menu_mute(self):
# self._sound.stop_music()
# self._sound.muted = True
def _menu_set_mute(self, selection: tuple, val: bool) -> None:
self._sound.muted = val
if self._sound.muted:
self._sound.stop_music()
else:
self._sound.play_music_all()
def _menu_quit(self):
pygame.event.post(pygame.event.Event(pygame.QUIT))
self._menu_is_open = False
Expand Down Expand Up @@ -540,7 +545,6 @@ def _menu_build_theme(self) -> pygame_menu.Theme:
theme.background_color = THEME_COLOR
theme.widget_font_shadow = True
theme.widget_font_size = 20
#theme.widget_padding = 10
return theme

def _menu_build_main(self) -> pygame_menu.Menu:
Expand All @@ -554,7 +558,14 @@ 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)
menu.add.button("Mute", action=self._menu_mute)

# True and False specify the sound system variable _sound.muted, which is why they're backwards
menu.add.selector(
"Sound: ",
[("Off", True), ["On", False]],
onchange=self._menu_set_mute,
default=1
)
menu.add.vertical_fill(2)
menu.add.text_input(
'Puzzle ID: ',
Expand Down Expand Up @@ -682,7 +693,7 @@ def main():
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
print("GAME EXITING")
logging.info("GAME EXITING")
game.save_game()
game_run = False
else:
Expand Down
2 changes: 1 addition & 1 deletion sound_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def play_music_all(self):
except pygame_error as e:
logging.warn("Couldn't open audio output device.")
raise
mixer.music.play(fade_ms=2000)
mixer.music.play(fade_ms=5000)
def set_volume(self, volume: int = 100):
if self._mixer_running:
vol = min(100, max(0, volume))
Expand Down

0 comments on commit b9ffa20

Please sign in to comment.