Merge branch 'game' into 'master'

Game

See merge request ynerant/dungeon-battle!3
This commit is contained in:
ynerant 2020-10-23 15:44:22 +02:00
commit 292a63c16d
4 changed files with 68 additions and 50 deletions

62
dungeonbattle/game.py Normal file
View File

@ -0,0 +1,62 @@
from .interfaces import Map
from .mapdisplay import MapDisplay
from .term_manager import TermManager
class Game:
INSTANCE = None
def init(self) -> None:
Game.INSTANCE = self
with TermManager() as term_manager:
self._start_game(term_manager.screen)
def _start_game(self, screen):
# TODO Generate map, or make the possibility to load another one
m = Map.load("example_map.txt")
player = Player()
self.player = player
player.y = 1
player.x = 6
d = MapDisplay(m, player)
while True:
screen.clear()
screen.refresh()
d.display(player.getPosY(), player.getPosX())
key = screen.getkey()
self.handle_key_pressed(key)
def handle_key_pressed(self, key):
# TODO load keys from settings
if key == 'z' or key == 'KEY_UP':
self.player.move_up()
if key == 's' or key == 'KEY_DOWN':
self.player.move_down()
if key == 'q' or key == 'KEY_LEFT':
self.player.move_left()
if key == 'd' or key == 'KEY_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

View File

@ -4,15 +4,16 @@ from dungeonbattle.interfaces import Map
class MapDisplay: class MapDisplay:
def __init__(self, m: Map): def __init__(self, m: Map, player):
self.map = m self.map = m
self.pad = curses.newpad(m.height, m.width+1) self.pad = curses.newpad(m.height, m.width+1)
self.player = player
def update_pad(self): def update_pad(self):
for i in range(self.map.height): self.pad.addstr(0, 0, self.map.draw_string())
self.pad.addstr(i, 0, self.map.tiles[i])
for e in self.map.entities: for e in self.map.entities:
self.pad.addch(e.y, e.x, e.img) self.pad.addch(e.y, e.x, e.img)
self.pad.addstr(self.player.getPosY(), self.player.getPosX(), '🐿')
def display(self, y, x): def display(self, y, x):
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1 deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1

View File

@ -1,45 +0,0 @@
from .interfaces import Map, Tile
from .term_manager import TermManager
def proof_of_concept() -> None:
"""
Read an example map, parse it, then the (squirrel) player can move freely.
"""
# Load the example map
m = Map.load("example_map.txt")
# Create the context manager to manipulate the screen
with TermManager() as term_manager:
stdscr = term_manager.screen
# Draw the full map
stdscr.addstr(0, 0, m.draw_string())
stdscr.refresh()
cur = [1, 6] # (y,x)
# We are a squirrel
stdscr.addstr(1, 6, '🐿')
stdscr.refresh()
while True:
# Get movements of the user
key = stdscr.getkey()
stdscr.addstr(cur[0], cur[1], Tile.FLOOR.value)
next_pos = cur[:]
if key == 'z' or key == 'KEY_UP':
next_pos[0] = cur[0] - 1
if key == 's' or key == 'KEY_DOWN':
next_pos[0] = cur[0] + 1
if key == 'q' or key == 'KEY_LEFT':
next_pos[1] = cur[1] - 1
if key == 'd' or key == 'KEY_RIGHT':
next_pos[1] = cur[1] + 1
# Check if we stay in the bounds
if 0 <= next_pos[0] < m.height and 0 <= next_pos[0] < m.width:
next_tile = m.tiles[next_pos[0]][next_pos[1]]
# Check if the new position is valid
if next_tile.can_walk():
cur = next_pos
# Draw the squirrel
stdscr.addstr(cur[0], cur[1], '🐿')

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from dungeonbattle.proof_of_concept import proof_of_concept from dungeonbattle.game import Game
if __name__ == "__main__": if __name__ == "__main__":
proof_of_concept() Game().init()