Optional descriptions for sounds in learn sounds menu.
This commit is contained in:
1 parent
9eabc4b934
commit
fe409db50f
3 files changed
+111
-5
No files matched your search
@@ -392,6 +392,20 @@ elif choice == "options":
|
||||
# Handle options screen
|
||||
show_options()
|
||||
# etc...
|
||||
|
||||
# Learn sounds with a description of what each sound means. Keys are a sound
|
||||
# key ("enemies/goblin"), a filename ("goblin"), or either without trailing
|
||||
# digits, so "falling_skull" covers falling_skull1 and falling_skull2.
|
||||
# The menu then says "goblin: A goblin is near, 1 of 5" instead of "goblin, 1 of 5".
|
||||
# Sounds without a description are announced as before.
|
||||
soundDescriptions = {
|
||||
"goblin": "A goblin is near",
|
||||
"falling_skull": "A skull falling towards you. Move away from it",
|
||||
}
|
||||
choice = sg.game_menu(sounds, None, "practice_mode", soundDescriptions=soundDescriptions)
|
||||
|
||||
# learn_sounds can also be called directly
|
||||
sg.learn_sounds(sounds, soundDescriptions)
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ def instruction_menu(sounds, instruction_text, *options):
|
||||
pygame.event.clear()
|
||||
time.sleep(0.001)
|
||||
|
||||
def game_menu(sounds, playCallback=None, *customOptions):
|
||||
def game_menu(sounds, playCallback=None, *customOptions, soundDescriptions=None):
|
||||
"""Display and handle the main game menu with standard and custom options.
|
||||
|
||||
Standard menu structure:
|
||||
@@ -140,6 +140,8 @@ def game_menu(sounds, playCallback=None, *customOptions):
|
||||
*customOptions: Additional action identifiers to include after play but before
|
||||
standard options. Underscores are spoken as spaces; the original
|
||||
identifiers are used for actions and returned unchanged.
|
||||
soundDescriptions (dict, optional): What each sound means, passed on to
|
||||
learn_sounds. See learn_sounds for the format.
|
||||
|
||||
Returns:
|
||||
str: Selected menu option or "exit" if user pressed escape
|
||||
@@ -300,7 +302,7 @@ def game_menu(sounds, playCallback=None, *customOptions):
|
||||
elif selectedOption == "credits":
|
||||
credits()
|
||||
elif selectedOption == "learn_sounds":
|
||||
learn_sounds(sounds)
|
||||
learn_sounds(sounds, descriptions=soundDescriptions)
|
||||
elif selectedOption == "high_scores":
|
||||
Scoreboard.display_high_scores()
|
||||
elif selectedOption == "donate":
|
||||
@@ -344,7 +346,24 @@ def game_menu(sounds, playCallback=None, *customOptions):
|
||||
pygame.event.clear()
|
||||
time.sleep(0.001)
|
||||
|
||||
def learn_sounds(sounds):
|
||||
def find_sound_description(soundKey, descriptions):
|
||||
"""Find what a sound means, for the learn sounds menu.
|
||||
|
||||
Looks for the whole sound key first, then the filename alone, then each
|
||||
of those without trailing digits, so one entry for "falling_skull" covers
|
||||
"falling_skull1" and "falling_skull2". Returns None if nothing matches.
|
||||
"""
|
||||
if not descriptions:
|
||||
return None
|
||||
filename = soundKey.split('/')[-1]
|
||||
for candidate in (soundKey, filename, soundKey.rstrip('0123456789'), filename.rstrip('0123456789')):
|
||||
description = descriptions.get(candidate)
|
||||
if description:
|
||||
return description
|
||||
return None
|
||||
|
||||
|
||||
def learn_sounds(sounds, descriptions=None):
|
||||
"""Interactive menu for learning game sounds.
|
||||
|
||||
Allows users to:
|
||||
@@ -360,6 +379,11 @@ def learn_sounds(sounds):
|
||||
|
||||
Args:
|
||||
sounds (dict): Dictionary of available sound objects
|
||||
descriptions (dict, optional): What each sound means, keyed by sound
|
||||
key ("enemies/goblin"), filename ("goblin") or either without
|
||||
trailing digits ("falling_skull" for "falling_skull1"). A sound
|
||||
with a description is announced as "name: description, 1 of 5";
|
||||
one without is announced as before, "name, 1 of 5".
|
||||
|
||||
Returns:
|
||||
str: "menu" if user exits with escape
|
||||
@@ -443,6 +467,11 @@ def learn_sounds(sounds):
|
||||
else:
|
||||
displayName = soundName
|
||||
|
||||
description = find_sound_description(soundName, descriptions)
|
||||
if description:
|
||||
# A final full stop would run into the comma before the count.
|
||||
displayName = f"{displayName}: {description.strip().rstrip('.')}"
|
||||
|
||||
# If directory just changed, include directory name in announcement
|
||||
if directoryChanged:
|
||||
dirDescription = "Root directory" if currentDir == 'root' else currentDir
|
||||
|
||||
+65
-2
@@ -38,7 +38,9 @@ def load_menu():
|
||||
return menu
|
||||
|
||||
|
||||
class MenuTests(unittest.TestCase):
|
||||
class MenuTestCase(unittest.TestCase):
|
||||
"""Helpers for running the real menu code against scripted key presses."""
|
||||
|
||||
def make_menu(self, keyCodes, highScores=True, files=True):
|
||||
menu = load_menu()
|
||||
menu.Scoreboard.has_high_scores.return_value = highScores
|
||||
@@ -60,6 +62,8 @@ class MenuTests(unittest.TestCase):
|
||||
def spoken_labels(self, menu):
|
||||
return [call.args[0] for call in menu.Speech.get_instance.return_value.speak.call_args_list]
|
||||
|
||||
|
||||
class MenuTests(MenuTestCase):
|
||||
def test_custom_action_is_spoken_readably_and_returned_unchanged(self):
|
||||
menu = self.make_menu([pygame.K_DOWN, pygame.K_DOWN, pygame.K_RETURN])
|
||||
result = menu.game_menu({}, None, "load_game")
|
||||
@@ -80,7 +84,7 @@ class MenuTests(unittest.TestCase):
|
||||
if actionName == "high_scores":
|
||||
menu.Scoreboard.display_high_scores.assert_called_once_with()
|
||||
elif actionName == "learn_sounds":
|
||||
menu.learn_sounds.assert_called_once_with(sounds)
|
||||
menu.learn_sounds.assert_called_once_with(sounds, descriptions=None)
|
||||
else:
|
||||
getattr(menu, actionName).assert_called_once_with()
|
||||
|
||||
@@ -118,6 +122,65 @@ class MenuTests(unittest.TestCase):
|
||||
self.assertEqual(menu.instruction_menu({}, "Select a file:", "example_file.json"), "example_file.json")
|
||||
self.assertEqual(self.spoken_labels(menu), ["Select a file:", "example_file.json"])
|
||||
|
||||
def test_game_menu_passes_sound_descriptions_to_learn_sounds(self):
|
||||
options = ["play", "high_scores", "load_game", "learn_sounds"]
|
||||
keyCodes = [pygame.K_DOWN] * options.index("learn_sounds") + [pygame.K_RETURN, pygame.K_HOME, pygame.K_RETURN]
|
||||
menu = self.make_menu(keyCodes)
|
||||
menu.learn_sounds = Mock()
|
||||
sounds = {}
|
||||
descriptions = {"candy": "Small, temporary speed boost."}
|
||||
self.assertEqual(menu.game_menu(sounds, None, "load_game", soundDescriptions=descriptions), "play")
|
||||
menu.learn_sounds.assert_called_once_with(sounds, descriptions=descriptions)
|
||||
|
||||
|
||||
class LearnSoundsTests(MenuTestCase):
|
||||
def run_learn_sounds(self, soundKeys, keyCodes, descriptions=None):
|
||||
"""Run learn_sounds with these keys pressed, then Escape. Returns the menu module and the sounds."""
|
||||
menu = self.make_menu(list(keyCodes) + [pygame.K_ESCAPE])
|
||||
sounds = {soundKey: Mock() for soundKey in soundKeys}
|
||||
if descriptions is None:
|
||||
self.assertEqual(menu.learn_sounds(sounds), "menu")
|
||||
else:
|
||||
self.assertEqual(menu.learn_sounds(sounds, descriptions), "menu")
|
||||
return menu, sounds
|
||||
|
||||
def test_without_descriptions_announcements_are_unchanged(self):
|
||||
menu, _sounds = self.run_learn_sounds(["candy", "guts"], [pygame.K_DOWN])
|
||||
self.assertEqual(self.spoken_labels(menu), ["Root directory: candy, 1 of 2", "guts, 2 of 2"])
|
||||
|
||||
def test_descriptions_are_spoken_after_the_name(self):
|
||||
descriptions = {"candy": "Small, temporary speed boost.", "guts": "Sends your vehicle out of control"}
|
||||
menu, _sounds = self.run_learn_sounds(["candy", "guts"], [pygame.K_DOWN], descriptions)
|
||||
self.assertEqual(self.spoken_labels(menu), [
|
||||
"Root directory: candy: Small, temporary speed boost, 1 of 2",
|
||||
"guts: Sends your vehicle out of control, 2 of 2",
|
||||
])
|
||||
|
||||
def test_a_sound_without_a_description_keeps_the_plain_announcement(self):
|
||||
menu, _sounds = self.run_learn_sounds(["candy", "guts"], [pygame.K_DOWN], {"candy": "Speed boost."})
|
||||
self.assertEqual(self.spoken_labels(menu), ["Root directory: candy: Speed boost, 1 of 2", "guts, 2 of 2"])
|
||||
|
||||
def test_numbered_variants_share_one_description(self):
|
||||
descriptions = {"falling_skull": "A skull falling towards you."}
|
||||
menu, _sounds = self.run_learn_sounds(["falling_skull1", "falling_skull2"], [pygame.K_DOWN], descriptions)
|
||||
self.assertEqual(self.spoken_labels(menu), [
|
||||
"Root directory: falling_skull1: A skull falling towards you, 1 of 2",
|
||||
"falling_skull2: A skull falling towards you, 2 of 2",
|
||||
])
|
||||
|
||||
def test_descriptions_match_full_key_or_filename_in_folders(self):
|
||||
descriptions = {"enemies/goblin": "A goblin, from the full key.", "witch": "A witch, from the filename."}
|
||||
menu, _sounds = self.run_learn_sounds(["enemies/goblin", "enemies/witch"], [pygame.K_DOWN], descriptions)
|
||||
self.assertEqual(self.spoken_labels(menu), [
|
||||
"enemies: goblin: A goblin, from the full key, 1 of 2",
|
||||
"witch: A witch, from the filename, 2 of 2",
|
||||
])
|
||||
|
||||
def test_enter_still_plays_the_selected_sound(self):
|
||||
menu, sounds = self.run_learn_sounds(["candy", "guts"], [pygame.K_DOWN, pygame.K_RETURN], {"guts": "Out of control."})
|
||||
sounds["guts"].play.assert_called_once_with()
|
||||
sounds["candy"].play.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user