squirrel-battle/dungeonbattle/game.py

72 lines
1.7 KiB
Python
Raw Normal View History

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-11-06 13:23:12 +00:00
self.key_handler = self.player_move
2020-10-23 12:53:08 +00:00
2020-10-23 16:01:39 +00:00
def new_game(self):
# TODO generate a new map procedurally
2020-11-06 13:23:12 +00:00
self.m = Map.load("example_map.txt")
self.d = MapDisplay(self.m)
2020-10-23 16:01:39 +00:00
self.player = Player()
self.player.y = 1
self.player.x = 6
@classmethod
def load_game(filename):
# TODO loading map from a file
raise NotImplementedError()
def run(self, screen):
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 13:23:12 +00:00
self.d.display(self.player.getPosY(), self.player.getPosX())
2020-10-23 12:53:08 +00:00
key = screen.getkey()
2020-10-23 16:01:39 +00:00
self.key_handler(key)
2020-10-23 12:53:08 +00:00
2020-10-23 16:01:39 +00:00
def player_move(self, key):
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
def move_up(self):
self.y -= 1
def move_down(self):
self.y += 1
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def getPosX(self):
return self.x
def getPosY(self):
return self.y