diff --git a/dungeonbattle/display/menudisplay.py b/dungeonbattle/display/menudisplay.py index 9dd416d..d25fe61 100644 --- a/dungeonbattle/display/menudisplay.py +++ b/dungeonbattle/display/menudisplay.py @@ -11,7 +11,6 @@ class MenuDisplay(Display): def update_menu(self, menu: Menu) -> None: self.menu = menu - self.values = [str(a) for a in menu.values] self.trueheight = len(self.values) self.truewidth = max([len(a) for a in self.values]) @@ -54,6 +53,10 @@ class MenuDisplay(Display): def preferred_height(self) -> int: return self.trueheight + 2 + @property + def values(self): + return [str(a) for a in self.menu.values] + class MainMenuDisplay(Display): def __init__(self, menu: MainMenu, *args): diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 8be446d..98e56c2 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -37,9 +37,11 @@ class Game: """ self.state = GameMode.MAINMENU self.main_menu = menus.MainMenu() + self.settings_menu = menus.SettingsMenu() self.settings = Settings() self.settings.load_settings() self.settings.write_settings() + self.settings_menu.update_values(self.settings) def new_game(self) -> None: """ @@ -70,7 +72,7 @@ class Game: screen.refresh() self.display_refresh() key = screen.getkey() - self.handle_key_pressed(self.translate_key(key)) + self.handle_key_pressed(self.translate_key(key), screen) def translate_key(self, key: str) -> KeyValues: """ @@ -93,7 +95,7 @@ class Game: elif key == ' ': return KeyValues.SPACE - def handle_key_pressed(self, key: KeyValues) -> None: + def handle_key_pressed(self, key: KeyValues, screen) -> None: """ Indicates what should be done when the given key is pressed, according to the current game state. @@ -103,7 +105,7 @@ class Game: elif self.state == GameMode.MAINMENU: self.handle_key_pressed_main_menu(key) elif self.state == GameMode.SETTINGS: - self.handle_key_pressed_settings(key) + self.handle_key_pressed_settings(key,screen) self.display_refresh() def handle_key_pressed_play(self, key: KeyValues) -> None: @@ -142,9 +144,20 @@ class Game: elif option == menus.MainMenuValues.EXIT: sys.exit(0) - def handle_key_pressed_settings(self, key: KeyValues) -> None: + def handle_key_pressed_settings(self, key: KeyValues, screen : Any) -> None: """ For now, in the settings mode, we can only go backwards. """ if key == KeyValues.SPACE: self.state = GameMode.MAINMENU + if key == KeyValues.DOWN: + self.settings_menu.go_down() + if key == KeyValues.UP: + self.settings_menu.go_up() + if key == KeyValues.ENTER: + option = self.settings_menu.validate().split(": ")[0] + if option != "TEXTURE_PACK": + newkey = screen.getkey() + self.settings.__setattr__(option, newkey) + self.settings.write_settings() + self.settings_menu.update_values(self.settings) diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index 68eeab7..06ef556 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -1,5 +1,6 @@ from enum import Enum from typing import Any +from .settings import Settings class Menu: @@ -30,6 +31,12 @@ class MainMenuValues(Enum): class MainMenu(Menu): values = [e for e in MainMenuValues] +class SettingsMenu(Menu) : + def __init__(self): + super().__init__() + def update_values(self, settings : Settings): + s = settings.dumps_to_string() + self.values = s[6:-2].replace("\"","").split(",\n ") class ArbitraryMenu(Menu): def __init__(self, values: list):