From 72ec02dbaef730da116e47c0f8e5182f5700f562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20N=C3=B6llemeyer?= Date: Sat, 17 Aug 2024 10:05:44 +0200 Subject: [PATCH] extracted states to own files --- game/{states.py => states/InGame.py} | 46 ++-------------------------- game/states/MainMenu.py | 45 +++++++++++++++++++++++++++ main.py | 4 ++- 3 files changed, 51 insertions(+), 44 deletions(-) rename game/{states.py => states/InGame.py} (76%) create mode 100644 game/states/MainMenu.py diff --git a/game/states.py b/game/states/InGame.py similarity index 76% rename from game/states.py rename to game/states/InGame.py index 181a13c..106f45c 100644 --- a/game/states.py +++ b/game/states/InGame.py @@ -1,7 +1,4 @@ -"""A collection of game states.""" - from __future__ import annotations -from functools import reduce import attrs import tcod.console @@ -10,13 +7,14 @@ import tcod.event from tcod.event import KeySym import g -import game.menus import game.world_tools from game.components import Gold, Graphic, Position from game.constants import DIRECTION_KEYS, ACTION_KEYS -from game.state import Push, Reset, State, StateResult +from game.state import Push, State, StateResult from game.tags import IsItem, IsPlayer, IsDoor, IsActor from game.constants import WALL_CHAR +from game.states import MainMenu + @attrs.define() class InGame(State): @@ -74,41 +72,3 @@ class InGame(State): draw(player.components[Position], player.components[Graphic]) if text := g.world[None].components.get(("Text", str)): console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0)) - - -class MainMenu(game.menus.ListMenu): - """Main/escape menu.""" - - __slots__ = () - - def __init__(self) -> None: - """Initialize the main menu.""" - items = [ - game.menus.SelectItem("New game", self.new_game), - game.menus.SelectItem("Quit", self.quit), - ] - if hasattr(g, "world"): - items.insert(0, game.menus.SelectItem("Continue", self.continue_)) - - super().__init__( - items=tuple(items), - selected=0, - x=5, - y=5, - ) - - @staticmethod - def continue_() -> StateResult: - """Return to the game.""" - return Reset(InGame()) - - @staticmethod - def new_game() -> StateResult: - """Begin a new game.""" - g.world = game.world_tools.new_world() - return Reset(InGame()) - - @staticmethod - def quit() -> StateResult: - """Close the program.""" - raise SystemExit() diff --git a/game/states/MainMenu.py b/game/states/MainMenu.py new file mode 100644 index 0000000..6854593 --- /dev/null +++ b/game/states/MainMenu.py @@ -0,0 +1,45 @@ +"""The main menu state""" + +from __future__ import annotations + +import g +import game.menus +import game.world_tools +from game.state import Reset, StateResult +from game.states.InGame import InGame + +class MainMenu(game.menus.ListMenu): + """Main/escape menu.""" + __slots__ = () + + def __init__(self) -> None: + """Initialize the main menu.""" + items = [ + game.menus.SelectItem("New game", self.new_game), + game.menus.SelectItem("Quit", self.quit), + ] + if hasattr(g, "world"): + items.insert(0, game.menus.SelectItem("Continue", self.continue_)) + + super().__init__( + items=tuple(items), + selected=0, + x=5, + y=5, + ) + + @staticmethod + def continue_() -> StateResult: + """Return to the game.""" + return Reset(InGame()) + + @staticmethod + def new_game() -> StateResult: + """Begin a new game.""" + g.world = game.world_tools.new_world() + return Reset(InGame()) + + @staticmethod + def quit() -> StateResult: + """Close the program.""" + raise SystemExit() diff --git a/main.py b/main.py index 176aefd..6703a24 100755 --- a/main.py +++ b/main.py @@ -11,6 +11,8 @@ import g import game.state_tools import game.states +from game.states.MainMenu import MainMenu + def main() -> None: """Entry point function.""" tileset = tcod.tileset.load_tilesheet( @@ -18,7 +20,7 @@ def main() -> None: ) tcod.tileset.procedural_block_elements(tileset=tileset) - g.states = [game.states.MainMenu()] + g.states = [MainMenu()] g.console = tcod.console.Console(80, 50) with tcod.context.new(console=g.console, tileset=tileset) as g.context: game.state_tools.main_loop()