squirrel-battle/dungeonbattle/game.py

63 lines
1.6 KiB
Python
Raw Normal View History

2020-11-06 14:33:26 +00:00
from typing import Any
2020-10-23 12:53:08 +00:00
from .interfaces import Map
2020-10-23 13:00:59 +00:00
from .mapdisplay import MapDisplay
from .settings import Settings
2020-10-23 12:53:08 +00:00
from .term_manager import TermManager
class Game:
INSTANCE = None
def init(self) -> None:
Game.INSTANCE = self
self.settings = Settings()
self.settings.load_settings()
self.settings.write_settings()
2020-10-23 12:53:08 +00:00
with TermManager() as term_manager:
self._start_game(term_manager.screen)
2020-11-06 14:33:26 +00:00
def _start_game(self, screen: Any) -> None:
2020-10-23 12:53:08 +00:00
# TODO Generate map, or make the possibility to load another one
m = Map.load("example_map.txt")
2020-10-23 13:14:19 +00:00
player = Player()
self.player = player
player.y = 1
player.x = 6
d = MapDisplay(m, player)
2020-10-23 12:53:08 +00:00
while True:
2020-10-23 13:40:32 +00:00
screen.clear()
2020-10-23 12:53:08 +00:00
screen.refresh()
2020-11-06 14:33:26 +00:00
d.display(player.y, player.y)
2020-10-23 12:53:08 +00:00
key = screen.getkey()
self.handle_key_pressed(key)
2020-11-06 14:33:26 +00:00
def handle_key_pressed(self, key: str) -> None:
2020-10-23 12:53:08 +00:00
# TODO load keys from settings
if key == 'z' or key == 'KEY_UP':
2020-10-23 13:14:19 +00:00
self.player.move_up()
2020-10-23 12:53:08 +00:00
if key == 's' or key == 'KEY_DOWN':
2020-10-23 13:14:19 +00:00
self.player.move_down()
2020-10-23 12:53:08 +00:00
if key == 'q' or key == 'KEY_LEFT':
2020-10-23 13:14:19 +00:00
self.player.move_left()
2020-10-23 12:53:08 +00:00
if key == 'd' or key == 'KEY_RIGHT':
2020-10-23 13:14:19 +00:00
self.player.move_right()
class Player:
# FIXME Should be elsewhere, only useful to don't break the previous code
y: int = 0
x: int = 0
2020-11-06 14:33:26 +00:00
def move_up(self) -> None:
2020-10-23 13:14:19 +00:00
self.y -= 1
2020-11-06 14:33:26 +00:00
def move_down(self) -> None:
2020-10-23 13:14:19 +00:00
self.y += 1
2020-11-06 14:33:26 +00:00
def move_left(self) -> None:
2020-10-23 13:14:19 +00:00
self.x -= 1
2020-11-06 14:33:26 +00:00
def move_right(self) -> None:
2020-10-23 13:14:19 +00:00
self.x += 1