Handle player position in game display
This commit is contained in:
parent
eea9b45f6c
commit
d264bb45cf
|
@ -14,10 +14,14 @@ class Game:
|
|||
def _start_game(self, screen):
|
||||
# TODO Generate map, or make the possibility to load another one
|
||||
m = Map.load("example_map.txt")
|
||||
d = MapDisplay(m)
|
||||
player = Player()
|
||||
self.player = player
|
||||
player.y = 1
|
||||
player.x = 6
|
||||
d = MapDisplay(m, player)
|
||||
screen.refresh()
|
||||
while True:
|
||||
d.display(1, 6)
|
||||
d.display(player.getPosY(), player.getPosX())
|
||||
screen.refresh()
|
||||
key = screen.getkey()
|
||||
self.handle_key_pressed(key)
|
||||
|
@ -25,10 +29,34 @@ class Game:
|
|||
def handle_key_pressed(self, key):
|
||||
# TODO load keys from settings
|
||||
if key == 'z' or key == 'KEY_UP':
|
||||
pass # TODO Go up
|
||||
self.player.move_up()
|
||||
if key == 's' or key == 'KEY_DOWN':
|
||||
pass # TODO Go down
|
||||
self.player.move_down()
|
||||
if key == 'q' or key == 'KEY_LEFT':
|
||||
pass # TODO Go left
|
||||
self.player.move_left()
|
||||
if key == 'd' or key == 'KEY_RIGHT':
|
||||
pass # TODO Go right
|
||||
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
|
||||
|
|
|
@ -4,14 +4,16 @@ from dungeonbattle.interfaces import Map
|
|||
|
||||
class MapDisplay:
|
||||
|
||||
def __init__(self, m: Map):
|
||||
def __init__(self, m: Map, player):
|
||||
self.map = m
|
||||
self.pad = curses.newpad(m.height, m.width+1)
|
||||
self.player = player
|
||||
|
||||
def update_pad(self):
|
||||
self.pad.addstr(0, 0, self.map.draw_string())
|
||||
for e in self.map.entities:
|
||||
self.pad.addch(e.y, e.x, e.img)
|
||||
self.pad.addstr(self.player.getPosY(), self.player.getPosX(), '🐿')
|
||||
|
||||
def display(self, y, x):
|
||||
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1
|
||||
|
|
Loading…
Reference in New Issue