Add some comments

This commit is contained in:
Yohann D'ANELLO 2020-11-08 23:31:17 +01:00
parent 259bde81a4
commit 65d89b7f9f
1 changed files with 27 additions and 1 deletions

View File

@ -36,6 +36,9 @@ class Game:
current_display: Display
def __init__(self) -> None:
"""
Init the game.
"""
self.state = GameMode.MAINMENU
self.main_menu = menus.MainMenu()
self.settings = Settings()
@ -43,6 +46,9 @@ class Game:
self.settings.write_settings()
def new_game(self, screen: Any) -> None:
"""
Create a new game on the screen.
"""
# TODO generate a new map procedurally
self.map = Map.load("example_map.txt")
self.player = Player()
@ -60,15 +66,22 @@ class Game:
raise NotImplementedError()
def run(self, screen: Any) -> None:
"""
Main infinite loop.
We wait for a player action, then we do what that should be done
when the given key got pressed.
"""
while True:
screen.clear()
screen.refresh()
# self.d.display(self.player.y, self.player.x)
self.current_display.refresh()
key = screen.getkey()
self.handle_key_pressed(self.translate_key(key))
def translate_key(self, key: str) -> KeyValues:
"""
Translate the raw string key into an enum value that we can use.
"""
if key in (self.settings.KEY_DOWN_SECONDARY,
self.settings.KEY_DOWN_PRIMARY):
return KeyValues.DOWN
@ -87,6 +100,10 @@ class Game:
return KeyValues.SPACE
def handle_key_pressed(self, key: KeyValues) -> None:
"""
Indicates what should be done when the given key is pressed,
according to the current game state.
"""
if self.state == GameMode.PLAY:
self.handle_key_pressed_play(key)
elif self.state == GameMode.MAINMENU:
@ -96,6 +113,9 @@ class Game:
self.current_display.refresh()
def handle_key_pressed_play(self, key: KeyValues) -> None:
"""
In play mode, arrows or zqsd should move the main character.
"""
if key == KeyValues.UP:
self.player.move_up()
elif key == KeyValues.DOWN:
@ -109,6 +129,9 @@ class Game:
self.current_display = self.menu_display
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
"""
In the main menu, we can navigate through options.
"""
if key == KeyValues.DOWN:
self.main_menu.go_down()
if key == KeyValues.UP:
@ -124,6 +147,9 @@ class Game:
sys.exit(0)
def handle_key_pressed_settings(self, key: KeyValues) -> None:
"""
For now, in the settings mode, we can only go backwards.
"""
if key == KeyValues.SPACE:
self.state = GameMode.MAINMENU
self.current_display = self.menu_display