From 43001f6ede2c86f7b5e38aac1fe0a11bcebe815e Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 18:06:28 +0100 Subject: [PATCH] Basic menu handling --- dungeonbattle/game.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index c22e0b1..1541898 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -1,11 +1,12 @@ from typing import Any +import sys from .interfaces import Map from .mapdisplay import MapDisplay from .settings import Settings from .term_manager import TermManager from enum import Enum, auto - +from . import menus class GameMode(Enum): MAINMENU = auto() @@ -28,6 +29,7 @@ class Game: def init(self) -> None: Game.INSTANCE = self self.state = GameMode.MAINMENU + self.main_menu = menus.MainMenu() self.settings = Settings() self.settings.load_settings() self.settings.write_settings() @@ -40,7 +42,7 @@ class Game: self.player.x = 6 self.d = MapDisplay(self.m, self.player) - @classmethod + @staticmethod def load_game(filename): # TODO loading map from a file raise NotImplementedError() @@ -64,15 +66,28 @@ class Game: return KeyValues.UP def handle_key_pressed(self, key: KeyValues) -> None: - - if key == KeyValues.UP: - self.player.move_up() - if key == KeyValues.DOWN: - self.player.move_down() - if key == KeyValues.LEFT: - self.player.move_left() - if key == KeyValues.RIGHT: - self.player.move_right() + if self.state == GameMode.PLAY: + if key == KeyValues.UP: + self.player.move_up() + if key == KeyValues.DOWN: + self.player.move_down() + if key == KeyValues.LEFT: + self.player.move_left() + if key == KeyValues.RIGHT: + self.player.move_right() + if self.state == GameMode.MAINMENU: + if key == KeyValues.DOWN: + self.main_menu.go_down() + if key == KeyValues.UP: + self.main_menu.go_up() + if key == KeyValues.ENTER: + option = self.main_menu.validate() + if option == menus.MainMenuValues.START: + self.state = GameMode.PLAY + elif option == menus.MainMenuValues.SETTINGS: + self.state = GameMode.SETTINGS + elif option == menus.MainMenuValues.EXIT: + sys.exit(0) class Player: