squirrel-battle/dungeonbattle/game.py

92 lines
3.0 KiB
Python
Raw Normal View History

from random import randint
2020-11-11 21:45:15 +00:00
from typing import Any, Optional
2020-11-06 14:33:26 +00:00
from .entities.player import Player
from .enums import GameMode, KeyValues, DisplayActions
2020-10-23 12:53:08 +00:00
from .interfaces import Map
from .settings import Settings
2020-11-06 17:06:28 +00:00
from . import menus
from .mapgeneration import randomwalk
from typing import Callable
2020-11-06 16:24:20 +00:00
2020-11-06 17:11:59 +00:00
2020-10-23 12:53:08 +00:00
class Game:
2020-11-08 22:26:54 +00:00
map: Map
player: Player
# display_actions is a display interface set by the bootstrapper
display_actions: Callable[[DisplayActions], None]
2020-11-08 22:26:54 +00:00
2020-11-06 17:39:55 +00:00
def __init__(self) -> None:
2020-11-08 22:31:17 +00:00
"""
Init the game.
"""
self.state = GameMode.MAINMENU
2020-11-06 17:06:28 +00:00
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)
2020-10-23 12:53:08 +00:00
def new_game(self) -> None:
2020-11-08 22:31:17 +00:00
"""
Create a new game on the screen.
"""
self.map = randomwalk.Generator().run()
2020-10-23 16:01:39 +00:00
self.player = Player()
2020-11-08 22:26:54 +00:00
self.map.add_entity(self.player)
2020-11-11 15:58:20 +00:00
self.player.move(self.map.start_y, self.map.start_x)
2020-11-11 15:23:27 +00:00
self.map.spawn_random_entities(randint(3, 10))
2020-10-23 16:01:39 +00:00
2020-11-06 17:06:28 +00:00
@staticmethod
2020-11-06 17:11:59 +00:00
def load_game(filename: str) -> None:
2020-10-23 16:01:39 +00:00
# TODO loading map from a file
raise NotImplementedError()
2020-11-06 17:11:59 +00:00
def run(self, screen: Any) -> None:
2020-11-08 22:31:17 +00:00
"""
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: # pragma no cover
2020-10-23 13:40:32 +00:00
screen.clear()
2020-10-23 12:53:08 +00:00
screen.refresh()
self.display_actions(DisplayActions.REFRESH)
2020-10-23 12:53:08 +00:00
key = screen.getkey()
self.handle_key_pressed(
KeyValues.translate_key(key, self.settings), key)
2020-11-06 16:24:20 +00:00
2020-11-11 21:45:15 +00:00
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\
-> None:
2020-11-08 22:31:17 +00:00
"""
Indicates what should be done when the given key is pressed,
according to the current game state.
"""
2020-11-06 17:06:28 +00:00
if self.state == GameMode.PLAY:
2020-11-08 22:26:54 +00:00
self.handle_key_pressed_play(key)
elif self.state == GameMode.MAINMENU:
2020-11-11 21:22:33 +00:00
self.main_menu.handle_key_pressed(key, self)
2020-11-08 22:26:54 +00:00
elif self.state == GameMode.SETTINGS:
self.settings_menu.handle_key_pressed(key, raw_key, self)
self.display_actions(DisplayActions.REFRESH)
2020-11-08 22:26:54 +00:00
def handle_key_pressed_play(self, key: KeyValues) -> None:
2020-11-08 22:31:17 +00:00
"""
In play mode, arrows or zqsd should move the main character.
"""
2020-11-08 22:26:54 +00:00
if key == KeyValues.UP:
if self.player.move_up():
self.map.tick()
2020-11-08 22:26:54 +00:00
elif key == KeyValues.DOWN:
if self.player.move_down():
self.map.tick()
2020-11-08 22:26:54 +00:00
elif key == KeyValues.LEFT:
if self.player.move_left():
self.map.tick()
2020-11-08 22:26:54 +00:00
elif key == KeyValues.RIGHT:
if self.player.move_right():
self.map.tick()
2020-11-08 22:26:54 +00:00
elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU