diff options
author | Viljami Ilola <+@hix.fi> | 2024-04-27 16:03:08 +0300 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-04-27 16:03:08 +0300 |
commit | d64c6b122eacd7b33bbda3a62093b492c786b1f9 (patch) | |
tree | 8145f0baae4fa822281ea8f8c05381abc747fee8 | |
parent | d73fe4bb20e544443476ebb4c9fc593fa0a8ad4a (diff) |
refactor menus
-rw-r--r-- | src/sliceitoff/game/game.py | 14 | ||||
-rw-r--r-- | src/sliceitoff/game/mainmenu.py | 68 | ||||
-rw-r--r-- | src/sliceitoff/game/menu.py | 70 | ||||
-rw-r--r-- | src/sliceitoff/game/settings.py | 78 | ||||
-rw-r--r-- | src/sliceitoff/screens/settings.py | 9 | ||||
-rw-r--r-- | src/sliceitoff/settings/settings.py | 2 | ||||
-rw-r--r-- | tests/test_screens.py | 7 |
7 files changed, 105 insertions, 143 deletions
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index 57f2c56..e25a6a6 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -17,7 +17,7 @@ from sliceitoff.hiscores import hi_scores from sliceitoff.text import fonts from sliceitoff.sfx import sfx -from .mainmenu import Mainmenu, MenuItems +from .mainmenu import MainMenu, MainMenuItems from .level import Level from .show import Show from .initials import Initials @@ -76,7 +76,7 @@ class Game: def mainmenu(self): """ menu where one select what to do """ sfx.music("pimpelipompeli") - menu = Mainmenu() + menu = MainMenu() while menu.active: menu.update(dt = self.clock.tick()) self.display.update([menu]) @@ -93,17 +93,17 @@ class Game: """ This is the main loop of the game """ while True: match self.mainmenu(): - case MenuItems.QUIT: + case MainMenuItems.QUIT: hi_scores.save() settings.save() break - case MenuItems.HISCORES: + case MainMenuItems.HISCORES: self.show_highscores() - case MenuItems.INSTRUCT: + case MainMenuItems.INSTRUCT: self.instructions() - case MenuItems.SETTINGS: + case MainMenuItems.SETTINGS: self.settings() - case MenuItems.NEWGAME: + case MainMenuItems.NEWGAME: self.newgame() if hi_scores.high_enough(self.stats.score): hi_scores.add( self.stats.score, self.initials()) diff --git a/src/sliceitoff/game/mainmenu.py b/src/sliceitoff/game/mainmenu.py index 8db0536..fbd91c9 100644 --- a/src/sliceitoff/game/mainmenu.py +++ b/src/sliceitoff/game/mainmenu.py @@ -1,16 +1,10 @@ """ game.mainmenu - Let's user choose """ from enum import IntEnum -import pygame from sliceitoff.screens import mainmenu_screen -from sliceitoff.display import Scaling -from sliceitoff.sfx import sfx +from .menu import Menu -from .explodeout import ExplodeOutGroup - -MOUSE_TRESHOLD = 100 - -class MenuItems(IntEnum): +class MainMenuItems(IntEnum): """ Items in the menu. Should match mainmenu_screen """ NEWGAME = 0 HISCORES = 1 @@ -18,59 +12,7 @@ class MenuItems(IntEnum): SETTINGS = 3 QUIT = 4 -class Mainmenu(ExplodeOutGroup): - """ sprite group with imputs to make selection """ +class MainMenu(Menu): + """ Main menu """ def __init__(self): - super().__init__() - self.add(mainmenu_screen(0)) - self.selection = 0 - self.mousey = 0 - - def update(self, dt = 0, **kwargs): - """ Does it all. Reads keyboard and updates screen """ - if not super().update(dt = dt, **kwargs) or self.explode: - return - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - self.selection = MenuItems.QUIT - self.do_fadeout() - break - if event.type == pygame.MOUSEBUTTONDOWN and event.button <= 3: - self.do_fadeout() - break - if event.type == pygame.KEYDOWN: - if self.process_key(event.key): - break - elif event.type == pygame.MOUSEMOTION: - self.process_mouse_motion() - self.empty() - self.add(mainmenu_screen(self.selection)) - - def process_mouse_motion(self): - """ Mouse movement up or down moves menu selection """ - self.mousey += pygame.mouse.get_rel()[1] - pygame.mouse.set_pos(Scaling.center) - if abs(self.mousey) > MOUSE_TRESHOLD: - self.selection += 1 if self.mousey > 0 else -1 - self.selection %= len(MenuItems) - self.mousey = 0 - - def process_key(self, key): - """ Processes known key presses """ - match key: - case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: - self.do_fadeout() - return True - case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: - self.selection = MenuItems.QUIT - self.do_fadeout() - sfx.music(None) - return True - case pygame.K_UP: - self.selection -= 1 - self.selection %= len(MenuItems) - case pygame.K_DOWN: - self.selection += 1 - self.selection %= len(MenuItems) - return False + super().__init__(mainmenu_screen, len(MainMenuItems)) diff --git a/src/sliceitoff/game/menu.py b/src/sliceitoff/game/menu.py new file mode 100644 index 0000000..446e37f --- /dev/null +++ b/src/sliceitoff/game/menu.py @@ -0,0 +1,70 @@ +""" game.menu - Skeleton for menus """ +import pygame + +from sliceitoff.display import Scaling + +from .explodeout import ExplodeOutGroup + +MOUSE_TRESHOLD = 100 + +class Menu(ExplodeOutGroup): + """ sprite group with imputs to make selection """ + def __init__(self, screen, items): + super().__init__() + self.items = items + self.selection = 0 + self.mousey = 0 + self.screen = screen + self.add(self.screen(self.selection)) + + def do_selection(self): + """ Default selection handler. Every action just ends menu. """ + self.do_fadeout() + + def update(self, dt = 0, **kwargs): + """ Does it all. Reads keyboard and updates screen """ + if not super().update(dt = dt, **kwargs) or self.explode: + return + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.selection = self.items - 1 + self.do_selection() + break + if event.type == pygame.MOUSEBUTTONDOWN and event.button <= 3: + self.do_selection() + break + if event.type == pygame.KEYDOWN: + if self.process_key(event.key): + break + elif event.type == pygame.MOUSEMOTION: + self.process_mouse_motion() + self.empty() + self.add(self.screen(self.selection)) + + def process_mouse_motion(self): + """ Mouse movement up or down moves menu selection """ + self.mousey += pygame.mouse.get_rel()[1] + pygame.mouse.set_pos(Scaling.center) + if abs(self.mousey) > MOUSE_TRESHOLD: + self.selection += 1 if self.mousey > 0 else -1 + self.selection %= self.items + self.mousey = 0 + + def process_key(self, key): + """ Processes known key presses """ + match key: + case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: + self.do_selection() + return True + case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: + self.selection = self.items - 1 + self.do_selection() + return True + case pygame.K_UP: + self.selection -= 1 + self.selection %= self.items + case pygame.K_DOWN: + self.selection += 1 + self.selection %= self.items + return False diff --git a/src/sliceitoff/game/settings.py b/src/sliceitoff/game/settings.py index 53d0362..0b3bed8 100644 --- a/src/sliceitoff/game/settings.py +++ b/src/sliceitoff/game/settings.py @@ -1,86 +1,28 @@ """ game.settings - Settings dialog """ from enum import IntEnum -import pygame -from sliceitoff.screens import settings_screen -from sliceitoff.display import Scaling from sliceitoff.sfx import sfx +from sliceitoff.screens import settings_screen +from .menu import Menu -from .explodeout import ExplodeOutGroup - -MOUSE_TRESHOLD = 100 -class MenuItems(IntEnum): +class SettingsItems(IntEnum): """ Items in the menu. Should match settings_screen """ SFX = 0 MUSIC = 1 BACK = 2 -class SettingsMenu(ExplodeOutGroup): - """ sprite group with imputs to make selection """ +class SettingsMenu(Menu): + """ Settings menu """ def __init__(self): - super().__init__() - self.add(settings_screen(0,0,0)) - self.selection = 0 - self.mousey = 0 + super().__init__(settings_screen, len(SettingsItems)) def do_selection(self): - """ Actions for manu entries """ + """ Custom actions for menu entries """ match self.selection: - case MenuItems.BACK: + case SettingsItems.BACK: self.do_fadeout() - case MenuItems.SFX: + case SettingsItems.SFX: sfx.sfx_up() - case MenuItems.MUSIC: + case SettingsItems.MUSIC: sfx.music_up() - - def update(self, dt = 0, **kwargs): - """ Does it all. Reads keyboard and updates screen """ - if not super().update(dt = dt, **kwargs) or self.explode: - return - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - self.selection = MenuItems.BACK - self.do_fadeout() - break - if event.type == pygame.MOUSEBUTTONDOWN and event.button <= 3: - self.do_selection() - break - if event.type == pygame.KEYDOWN: - if self.process_key(event.key): - break - elif event.type == pygame.MOUSEMOTION: - self.process_mouse_motion() - self.empty() - self.add(settings_screen( - self.selection, - sfx.sfx_volume, - sfx.music_volume)) - - def process_mouse_motion(self): - """ Mouse movement up or down moves menu selection """ - self.mousey += pygame.mouse.get_rel()[1] - pygame.mouse.set_pos(Scaling.center) - if abs(self.mousey) > MOUSE_TRESHOLD: - self.selection += 1 if self.mousey > 0 else -1 - self.selection %= len(MenuItems) - self.mousey = 0 - - def process_key(self, key): - """ Processes known key presses """ - match key: - case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: - self.do_selection() - return True - case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: - self.selection = MenuItems.BACK - self.do_fadeout() - return True - case pygame.K_UP: - self.selection -= 1 - self.selection %= len(MenuItems) - case pygame.K_DOWN: - self.selection += 1 - self.selection %= len(MenuItems) - return False diff --git a/src/sliceitoff/screens/settings.py b/src/sliceitoff/screens/settings.py index c987126..4be56a8 100644 --- a/src/sliceitoff/screens/settings.py +++ b/src/sliceitoff/screens/settings.py @@ -1,9 +1,12 @@ """ screens.mainmenu - Screen for mainmenu""" from random import randrange from sliceitoff.text import TextPage +from sliceitoff.sfx import sfx -def settings_screen(selection, sfx_vol, music_vol): +def settings_screen(selection): """ Screen where current selection is flashing """ + sfx_slider = '#' * sfx.sfx_volume + ' ' * (10 - sfx.sfx_volume) + bgm_slider = '#' * sfx.music_volume + ' ' * (10 - sfx.music_volume) active = randrange(0xe9,0xf0) inactive = 0xe7 text = ( @@ -11,10 +14,10 @@ def settings_screen(selection, sfx_vol, music_vol): f"\n\n" f"\xe7SFX: " f"{chr(active if selection == 0 else inactive)}" - f"[{'#' * sfx_vol}{' ' * (10 - sfx_vol)}]\n\n" + f"[{sfx_slider}]\n\n" f"\xe7BGM: " f"{chr(active if selection == 1 else inactive)}" - f"[{'#' * music_vol}{' ' * (10 - music_vol)}]\n\n" + f"[{bgm_slider}]\n\n" f"{chr(active if selection == 2 else inactive)}" f"Back.") return TextPage( diff --git a/src/sliceitoff/settings/settings.py b/src/sliceitoff/settings/settings.py index 2d4b28f..48b754c 100644 --- a/src/sliceitoff/settings/settings.py +++ b/src/sliceitoff/settings/settings.py @@ -7,7 +7,7 @@ TEST_CONFIG_FILE = os.getenv("TEST_CONFIG_FILE") class Settings: """ Handles loading and saving settings from config file""" - def __init__(self, filename = None): + def __init__(self): self.settings=[] if TEST_CONFIG_FILE: self.config_filename = Path(TEST_CONFIG_FILE) diff --git a/tests/test_screens.py b/tests/test_screens.py index 00cf694..800dcd5 100644 --- a/tests/test_screens.py +++ b/tests/test_screens.py @@ -13,7 +13,8 @@ from sliceitoff.screens import ( instructions2_screen, level_screen, levelup_screen, - mainmenu_screen) + mainmenu_screen, + settings_screen) class TestScreens(unittest.TestCase): def setUp(self): @@ -61,3 +62,7 @@ class TestScreens(unittest.TestCase): def test_can_run_mainmenu(self): page = mainmenu_screen(2) self.assertEqual(TextPage, type(page)) + + def test_can_run_mainmenu(self): + page = settings_screen(2) + self.assertEqual(TextPage, type(page)) |