From e3a1bf96c2dc23b10bc5d3fa5e4735e082926f8f Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 17:24:20 +0100 Subject: [PATCH] Used settings for keys --- dungeonbattle/game.py | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 77daf35..c22e0b1 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -4,6 +4,22 @@ from .interfaces import Map from .mapdisplay import MapDisplay from .settings import Settings from .term_manager import TermManager +from enum import Enum, auto + + +class GameMode(Enum): + MAINMENU = auto() + PLAY = auto() + SETTINGS = auto() + INVENTORY = auto() + + +class KeyValues(Enum): + UP = auto() + DOWN = auto() + LEFT = auto() + RIGHT = auto() + ENTER = auto() class Game: @@ -11,6 +27,7 @@ class Game: def init(self) -> None: Game.INSTANCE = self + self.state = GameMode.MAINMENU self.settings = Settings() self.settings.load_settings() self.settings.write_settings() @@ -34,17 +51,27 @@ class Game: screen.refresh() self.d.display(self.player.y, self.player.x) key = screen.getkey() - self.handle_key_pressed(key) + self.handle_key_pressed(self.translate_key(key)) - def handle_key_pressed(self, key: str) -> None: - # TODO load keys from settings - if key == 'z' or key == 'KEY_UP': + def translate_key(self, key:str) -> KeyValues: + if key in (self.settings.KEY_DOWN_SECONDARY, self.settings.KEY_DOWN_PRIMARY): + return KeyValues.DOWN + elif key in (self.settings.KEY_LEFT_PRIMARY, self.settings.KEY_LEFT_SECONDARY): + return KeyValues.LEFT + elif key in (self.settings.KEY_RIGHT_PRIMARY, self.settings.KEY_RIGHT_SECONDARY): + return KeyValues.RIGHT + elif key in (self.settings.KEY_UP_PRIMARY, self.settings.KEY_UP_SECONDARY): + return KeyValues.UP + + def handle_key_pressed(self, key: KeyValues) -> None: + + if key == KeyValues.UP: self.player.move_up() - if key == 's' or key == 'KEY_DOWN': + if key == KeyValues.DOWN: self.player.move_down() - if key == 'q' or key == 'KEY_LEFT': + if key == KeyValues.LEFT: self.player.move_left() - if key == 'd' or key == 'KEY_RIGHT': + if key == KeyValues.RIGHT: self.player.move_right()