From e84a5efee236290147bb4cfdcea7c4129cf161b9 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 17:47:52 +0200 Subject: [PATCH] More modularity, add properties in tiles --- dungeonbattle/interfaces.py | 6 ++++++ dungeonbattle/proof_of_concept.py | 19 +++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 5fc6843..02bf5eb 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -38,6 +38,12 @@ class Tile(Enum): WALL = '#' FLOOR = '.' + def is_wall(self) -> bool: + return self == Tile.WALL + + def can_walk(self) -> bool: + return not self.is_wall() + class Entity: x: int diff --git a/dungeonbattle/proof_of_concept.py b/dungeonbattle/proof_of_concept.py index 8cecc64..8db5aeb 100644 --- a/dungeonbattle/proof_of_concept.py +++ b/dungeonbattle/proof_of_concept.py @@ -19,18 +19,17 @@ def proof_of_concept() -> None: while True: key = stdscr.getkey() stdscr.addstr(cur[0], cur[1], '.') + next_pos = cur[:] if key == 'z' or key == 'KEY_UP': - if cur[0] > 0 and m.tiles[cur[0] - 1][cur[1]].value != '#': - cur[0] = cur[0] - 1 + next_pos[0] = cur[0] - 1 if key == 's' or key == 'KEY_DOWN': - if cur[0] < m.height - 1 and \ - m.tiles[cur[0] + 1][cur[1]].value != '#': - cur[0] = cur[0] + 1 + next_pos[0] = cur[0] + 1 if key == 'q' or key == 'KEY_LEFT': - if cur[1] > 0 and m.tiles[cur[0]][cur[1] - 1].value != '#': - cur[1] = cur[1] - 1 + next_pos[1] = cur[1] - 1 if key == 'd' or key == 'KEY_RIGHT': - if cur[1] < m.width and \ - m.tiles[cur[0]][cur[1] + 1].value != '#': - cur[1] = cur[1] + 1 + next_pos[1] = cur[1] + 1 + if 0 <= next_pos[0] < m.height and 0 <= next_pos[0] < m.width: + next_tile = m.tiles[next_pos[0]][next_pos[1]] + if next_tile.can_walk(): + cur = next_pos stdscr.addstr(cur[0], cur[1], '🐿️')