From 03560f4407be11991f78dd2419c15557144fbcf1 Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Fri, 16 Oct 2020 14:12:39 +0200 Subject: [PATCH 01/17] Added a first try for player movement --- dungeonbattle/player_move_essai2.py | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 dungeonbattle/player_move_essai2.py diff --git a/dungeonbattle/player_move_essai2.py b/dungeonbattle/player_move_essai2.py new file mode 100644 index 0000000..bf56764 --- /dev/null +++ b/dungeonbattle/player_move_essai2.py @@ -0,0 +1,45 @@ +import curses +#import term_manager.py as term +import time + +filename = "map.txt" +A = open(filename) +M = [] + +i = 0 +for lines in A : + B = list(lines)[:-1] + M.append(B) + i+=1 +print(M) + +def main(stdscr): + for y in range(len(M)): #len(M) + for x in range(len(M[0])): #len(M[0]) + stdscr.addstr(y,x,M[y][x]) + stdscr.refresh() + + cur = [1,6] #(y,x) + stdscr.addstr(1,6,'@') + stdscr.refresh() + + for i in range(10) : + stdscr.addstr(cur[0],cur[1],'.') + key = stdscr.getkey() + if key == 'z' : + if cur[0]>0 and M[cur[0]-1][cur[1]] != '#' : + cur[0] = cur[0]-1 + if key == 's' : + if cur[0]0 and M[cur[0]][cur[1]-1] != '#' : + cur[1] = cur[1]-1 + if key == 'd' : + if cur[1] Date: Fri, 16 Oct 2020 14:18:33 +0200 Subject: [PATCH 02/17] Added a map example --- dungeonbattle/map.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 dungeonbattle/map.txt diff --git a/dungeonbattle/map.txt b/dungeonbattle/map.txt new file mode 100644 index 0000000..4111fae --- /dev/null +++ b/dungeonbattle/map.txt @@ -0,0 +1,17 @@ + ####### ############# + #.....# #...........# + #.....# #####...........# + #.....# #...............# + #.##### #.###...........# + #.# #.# #...........# + #.# #.# ############# + #.# #.# + #.#### #.# + #....# #.# + ####.###################.# + #.....................# ################# + #.....................# #...............# + #.....................#######...............# + #...........................................# + #.....................#######...............# + ####################### ################# From 57dd733c5469e00b9872c6cc04226bb4570b1d5f Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 14:23:11 +0200 Subject: [PATCH 03/17] Replace the dot cursor after the player moved, not before --- dungeonbattle/player_move_essai2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dungeonbattle/player_move_essai2.py b/dungeonbattle/player_move_essai2.py index bf56764..6d4d73a 100644 --- a/dungeonbattle/player_move_essai2.py +++ b/dungeonbattle/player_move_essai2.py @@ -24,8 +24,8 @@ def main(stdscr): stdscr.refresh() for i in range(10) : - stdscr.addstr(cur[0],cur[1],'.') key = stdscr.getkey() + stdscr.addstr(cur[0],cur[1],'.') if key == 'z' : if cur[0]>0 and M[cur[0]-1][cur[1]] != '#' : cur[0] = cur[0]-1 From 4b0554a9135fe123bae423a50690a90b2a1a061b Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Fri, 16 Oct 2020 15:00:33 +0200 Subject: [PATCH 04/17] Now uses TermManager --- dungeonbattle/player_move_essai2.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dungeonbattle/player_move_essai2.py b/dungeonbattle/player_move_essai2.py index 6d4d73a..ea44442 100644 --- a/dungeonbattle/player_move_essai2.py +++ b/dungeonbattle/player_move_essai2.py @@ -1,5 +1,5 @@ import curses -#import term_manager.py as term +from term_manager import TermManager import time filename = "map.txt" @@ -39,7 +39,6 @@ def main(stdscr): if cur[1] Date: Fri, 16 Oct 2020 15:23:58 +0200 Subject: [PATCH 05/17] Cleaner proof of concept --- dungeonbattle/player_move_essai2.py | 44 ------------------------ dungeonbattle/proof_of_concept.py | 40 +++++++++++++++++++++ dungeonbattle/map.txt => example_map.txt | 0 main.py | 3 +- 4 files changed, 42 insertions(+), 45 deletions(-) delete mode 100644 dungeonbattle/player_move_essai2.py create mode 100644 dungeonbattle/proof_of_concept.py rename dungeonbattle/map.txt => example_map.txt (100%) mode change 100644 => 100755 main.py diff --git a/dungeonbattle/player_move_essai2.py b/dungeonbattle/player_move_essai2.py deleted file mode 100644 index ea44442..0000000 --- a/dungeonbattle/player_move_essai2.py +++ /dev/null @@ -1,44 +0,0 @@ -import curses -from term_manager import TermManager -import time - -filename = "map.txt" -A = open(filename) -M = [] - -i = 0 -for lines in A : - B = list(lines)[:-1] - M.append(B) - i+=1 -print(M) - -def main(stdscr): - for y in range(len(M)): #len(M) - for x in range(len(M[0])): #len(M[0]) - stdscr.addstr(y,x,M[y][x]) - stdscr.refresh() - - cur = [1,6] #(y,x) - stdscr.addstr(1,6,'@') - stdscr.refresh() - - for i in range(10) : - key = stdscr.getkey() - stdscr.addstr(cur[0],cur[1],'.') - if key == 'z' : - if cur[0]>0 and M[cur[0]-1][cur[1]] != '#' : - cur[0] = cur[0]-1 - if key == 's' : - if cur[0]0 and M[cur[0]][cur[1]-1] != '#' : - cur[1] = cur[1]-1 - if key == 'd' : - if cur[1] None: + matrix = [] + with open("example_map.txt") as f: + for line in f.readlines(): + matrix.append(line[:-1]) + + with TermManager() as term_manager: + stdscr = term_manager.screen + + for y in range(len(matrix)): + for x in range(len(matrix[0])): + stdscr.addstr(y, x, matrix[y][x]) + stdscr.refresh() + + cur = [1, 6] # (y,x) + stdscr.addstr(1, 6, '@') + stdscr.refresh() + + while True: + key = stdscr.getkey() + stdscr.addstr(cur[0], cur[1], '.') + if key == 'z': + if cur[0] > 0 and matrix[cur[0] - 1][cur[1]] != '#': + cur[0] = cur[0] - 1 + if key == 's': + if cur[0] < len(matrix) - 1 and \ + matrix[cur[0] + 1][cur[1]] != '#': + cur[0] = cur[0] + 1 + if key == 'q': + if cur[1] > 0 and matrix[cur[0]][cur[1] - 1] != '#': + cur[1] = cur[1] - 1 + if key == 'd': + if cur[1] < len(matrix[0]) and \ + matrix[cur[0]][cur[1] + 1] != '#': + cur[1] = cur[1] + 1 + stdscr.addstr(cur[0], cur[1], '@') diff --git a/dungeonbattle/map.txt b/example_map.txt similarity index 100% rename from dungeonbattle/map.txt rename to example_map.txt diff --git a/main.py b/main.py old mode 100644 new mode 100755 index 1158de2..0eb26bf --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from dungeonbattle.proof_of_concept import proof_of_concept if __name__ == "__main__": - print("Hello world!") + proof_of_concept() From c8de541eee3c21c81d468cf1a354f58c6e66674a Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 15:32:15 +0200 Subject: [PATCH 06/17] Also use arrow keys in example --- dungeonbattle/proof_of_concept.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dungeonbattle/proof_of_concept.py b/dungeonbattle/proof_of_concept.py index 8fe75c1..046d687 100644 --- a/dungeonbattle/proof_of_concept.py +++ b/dungeonbattle/proof_of_concept.py @@ -23,17 +23,17 @@ def proof_of_concept() -> None: while True: key = stdscr.getkey() stdscr.addstr(cur[0], cur[1], '.') - if key == 'z': + if key == 'z' or key == 'KEY_UP': if cur[0] > 0 and matrix[cur[0] - 1][cur[1]] != '#': cur[0] = cur[0] - 1 - if key == 's': + if key == 's' or key == 'KEY_DOWN': if cur[0] < len(matrix) - 1 and \ matrix[cur[0] + 1][cur[1]] != '#': cur[0] = cur[0] + 1 - if key == 'q': + if key == 'q' or key == 'KEY_LEFT': if cur[1] > 0 and matrix[cur[0]][cur[1] - 1] != '#': cur[1] = cur[1] - 1 - if key == 'd': + if key == 'd' or key == 'KEY_RIGHT': if cur[1] < len(matrix[0]) and \ matrix[cur[0]][cur[1] + 1] != '#': cur[1] = cur[1] + 1 From 1e7ca6026f1cd019ed0bffc8315d0eeafcf8b1c4 Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 16 Oct 2020 15:41:25 +0200 Subject: [PATCH 07/17] Tiles are now an enumeration --- dungeonbattle/interfaces.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 1674427..3fa3422 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from enum import Enum, auto + class Map: width: int @@ -22,28 +24,25 @@ class Map: lines = [line for line in lines if line] height = len(lines) width = len(lines[0]) - chars = [[Tile.from_char(c, x, y) + tiles = [[Tile.from_char(c) for x, c in enumerate(line)] for y, line in enumerate(lines)] - return Map(width, height, chars) + return Map(width, height, tiles) -class Tile: - x: int - y: int - char: str +class Tile(Enum): + EMPTY = auto() + WALL = auto() + FLOOR = auto() @staticmethod - def from_char(c: str, x: int, y: int): - t = Tile() - t.x = x - t.y = y - t.char = c - return c + def from_char(c: str): + return {'#': Tile.WALL, '.': Tile.FLOOR, ' ': Tile.EMPTY}[c] class Entity: - tile: Tile + x: int + y: int def move(self, x: int, y: int) -> None: - self.tile.x = x - self.tile.y = y + self.x = x + self.y = y From a0530b67fb22a57bef3b7c60648db0be062e64d6 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 14:00:38 +0200 Subject: [PATCH 08/17] Draw a map in a string to make the render in the screen easier --- dungeonbattle/interfaces.py | 3 +++ dungeonbattle/interfaces_test.py | 1 + 2 files changed, 4 insertions(+) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 3fa3422..537b256 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -28,6 +28,9 @@ class Map: for x, c in enumerate(line)] for y, line in enumerate(lines)] return Map(width, height, tiles) + def draw_string(self) -> str: + return "\n".join("".join(tile.char for tile in line) for line in self.tiles) + class Tile(Enum): EMPTY = auto() diff --git a/dungeonbattle/interfaces_test.py b/dungeonbattle/interfaces_test.py index a987b37..479a9ac 100644 --- a/dungeonbattle/interfaces_test.py +++ b/dungeonbattle/interfaces_test.py @@ -8,3 +8,4 @@ class TestInterfaces(unittest.TestCase): m = Map.load_from_string("ab\ncd\n") self.assertEqual(m.width, 2) self.assertEqual(m.height, 2) + self.assertEqual(m.draw_string(), "ab\ncd") From 413494ca8b87142b859bcec9a68ca721369c196f Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 16 Oct 2020 15:42:05 +0200 Subject: [PATCH 09/17] Added __pycache__ to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 60cb1f8..3269e7f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ venv/ .coverage .pytest_cache/ + +__pycache__ From c0e5fe4400be4befdd704a8776ac2c3fc1702a31 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 16:41:38 +0200 Subject: [PATCH 10/17] Get a tile from its representation --- dungeonbattle/interfaces.py | 16 ++++++++++------ dungeonbattle/interfaces_test.py | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 537b256..b7229a8 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from enum import Enum, auto +from enum import Enum class Map: @@ -29,17 +29,21 @@ class Map: return Map(width, height, tiles) def draw_string(self) -> str: - return "\n".join("".join(tile.char for tile in line) for line in self.tiles) + return "\n".join("".join(tile.value for tile in line) + for line in self.tiles) class Tile(Enum): - EMPTY = auto() - WALL = auto() - FLOOR = auto() + EMPTY = ' ' + WALL = '#' + FLOOR = '.' @staticmethod def from_char(c: str): - return {'#': Tile.WALL, '.': Tile.FLOOR, ' ': Tile.EMPTY}[c] + for t in Tile: + if t.value == c: + return t + raise ValueError(f"Tile not found : {c}") class Entity: diff --git a/dungeonbattle/interfaces_test.py b/dungeonbattle/interfaces_test.py index 479a9ac..dc8932a 100644 --- a/dungeonbattle/interfaces_test.py +++ b/dungeonbattle/interfaces_test.py @@ -5,7 +5,7 @@ from dungeonbattle.interfaces import Map class TestInterfaces(unittest.TestCase): def test_map(self) -> None: - m = Map.load_from_string("ab\ncd\n") + m = Map.load_from_string(".#\n#.\n") self.assertEqual(m.width, 2) self.assertEqual(m.height, 2) - self.assertEqual(m.draw_string(), "ab\ncd") + self.assertEqual(m.draw_string(), ".#\n#.") From cae7ab3beb302ed8d8b74fdbefabc7ea794e4e80 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 16:46:40 +0200 Subject: [PATCH 11/17] Use Map interface in proof of concept --- dungeonbattle/proof_of_concept.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/dungeonbattle/proof_of_concept.py b/dungeonbattle/proof_of_concept.py index 046d687..fd3b85f 100644 --- a/dungeonbattle/proof_of_concept.py +++ b/dungeonbattle/proof_of_concept.py @@ -1,19 +1,15 @@ #!/usr/bin/env python +from .interfaces import Map from .term_manager import TermManager def proof_of_concept() -> None: - matrix = [] - with open("example_map.txt") as f: - for line in f.readlines(): - matrix.append(line[:-1]) + m = Map.load("example_map.txt") with TermManager() as term_manager: stdscr = term_manager.screen - for y in range(len(matrix)): - for x in range(len(matrix[0])): - stdscr.addstr(y, x, matrix[y][x]) + stdscr.addstr(0, 0, m.draw_string()) stdscr.refresh() cur = [1, 6] # (y,x) @@ -24,17 +20,17 @@ def proof_of_concept() -> None: key = stdscr.getkey() stdscr.addstr(cur[0], cur[1], '.') if key == 'z' or key == 'KEY_UP': - if cur[0] > 0 and matrix[cur[0] - 1][cur[1]] != '#': + if cur[0] > 0 and m.tiles[cur[0] - 1][cur[1]].value != '#': cur[0] = cur[0] - 1 if key == 's' or key == 'KEY_DOWN': - if cur[0] < len(matrix) - 1 and \ - matrix[cur[0] + 1][cur[1]] != '#': + if cur[0] < m.height - 1 and \ + m.tiles[cur[0] + 1][cur[1]].value != '#': cur[0] = cur[0] + 1 if key == 'q' or key == 'KEY_LEFT': - if cur[1] > 0 and matrix[cur[0]][cur[1] - 1] != '#': + if cur[1] > 0 and m.tiles[cur[0]][cur[1] - 1].value != '#': cur[1] = cur[1] - 1 if key == 'd' or key == 'KEY_RIGHT': - if cur[1] < len(matrix[0]) and \ - matrix[cur[0]][cur[1] + 1] != '#': + if cur[1] < m.width and \ + m.tiles[cur[0]][cur[1] + 1].value != '#': cur[1] = cur[1] + 1 stdscr.addstr(cur[0], cur[1], '@') From 795623e1c9172e6b1d90377090e68a494cf8b87e Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 17:15:17 +0200 Subject: [PATCH 12/17] :squirrel: Main character is a squirrel --- dungeonbattle/proof_of_concept.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dungeonbattle/proof_of_concept.py b/dungeonbattle/proof_of_concept.py index fd3b85f..8cecc64 100644 --- a/dungeonbattle/proof_of_concept.py +++ b/dungeonbattle/proof_of_concept.py @@ -13,7 +13,7 @@ def proof_of_concept() -> None: stdscr.refresh() cur = [1, 6] # (y,x) - stdscr.addstr(1, 6, '@') + stdscr.addstr(1, 6, 'šŸæļø') stdscr.refresh() while True: @@ -33,4 +33,4 @@ def proof_of_concept() -> None: if cur[1] < m.width and \ m.tiles[cur[0]][cur[1] + 1].value != '#': cur[1] = cur[1] + 1 - stdscr.addstr(cur[0], cur[1], '@') + stdscr.addstr(cur[0], cur[1], 'šŸæļø') From c9d136929b563f87da6010a4728e38bce9acdec9 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 17:41:37 +0200 Subject: [PATCH 13/17] Better usage of enumerations --- dungeonbattle/interfaces.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index b7229a8..5fc6843 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -24,7 +24,7 @@ class Map: lines = [line for line in lines if line] height = len(lines) width = len(lines[0]) - tiles = [[Tile.from_char(c) + tiles = [[Tile(c) for x, c in enumerate(line)] for y, line in enumerate(lines)] return Map(width, height, tiles) @@ -38,13 +38,6 @@ class Tile(Enum): WALL = '#' FLOOR = '.' - @staticmethod - def from_char(c: str): - for t in Tile: - if t.value == c: - return t - raise ValueError(f"Tile not found : {c}") - class Entity: x: int From e84a5efee236290147bb4cfdcea7c4129cf161b9 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 17:47:52 +0200 Subject: [PATCH 14/17] 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], 'šŸæļø') From 8818073fc040ba32297fe34659ddacd0360b03b4 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 18:05:49 +0200 Subject: [PATCH 15/17] Add some comments --- dungeonbattle/interfaces.py | 17 +++++++++++++++++ dungeonbattle/interfaces_test.py | 3 +++ dungeonbattle/proof_of_concept.py | 12 +++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 02bf5eb..a312d34 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -3,6 +3,10 @@ from enum import Enum class Map: + """ + Object that represents a Map with its width, height + and the whole tiles, with their custom properties. + """ width: int height: int tiles: list @@ -14,12 +18,18 @@ class Map: @staticmethod def load(filename: str): + """ + Read a file that contains the content of a map, and build a Map object. + """ with open(filename, "r") as f: file = f.read() return Map.load_from_string(file) @staticmethod def load_from_string(content: str): + """ + Load a map represented by its characters and build a Map object. + """ lines = content.split("\n") lines = [line for line in lines if line] height = len(lines) @@ -29,6 +39,10 @@ class Map: return Map(width, height, tiles) def draw_string(self) -> str: + """ + Draw the current map as a string object that can be rendered + in the window. + """ return "\n".join("".join(tile.value for tile in line) for line in self.tiles) @@ -42,6 +56,9 @@ class Tile(Enum): return self == Tile.WALL def can_walk(self) -> bool: + """ + Check if an entity (player or not) can move in this tile. + """ return not self.is_wall() diff --git a/dungeonbattle/interfaces_test.py b/dungeonbattle/interfaces_test.py index dc8932a..34a50f9 100644 --- a/dungeonbattle/interfaces_test.py +++ b/dungeonbattle/interfaces_test.py @@ -5,6 +5,9 @@ from dungeonbattle.interfaces import Map class TestInterfaces(unittest.TestCase): def test_map(self) -> None: + """ + Create a map and check that it is well parsed. + """ m = Map.load_from_string(".#\n#.\n") self.assertEqual(m.width, 2) self.assertEqual(m.height, 2) diff --git a/dungeonbattle/proof_of_concept.py b/dungeonbattle/proof_of_concept.py index 8db5aeb..2034cbf 100644 --- a/dungeonbattle/proof_of_concept.py +++ b/dungeonbattle/proof_of_concept.py @@ -1,22 +1,29 @@ -#!/usr/bin/env python from .interfaces import Map 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], '.') next_pos = cur[:] @@ -28,8 +35,11 @@ def proof_of_concept() -> None: 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], 'šŸæļø') From 2947ffd0c112b0646ef4543c845d777cc0cd2623 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 18:20:26 +0200 Subject: [PATCH 16/17] Replace # by walls --- dungeonbattle/interfaces.py | 2 +- dungeonbattle/proof_of_concept.py | 8 ++++---- example_map.txt | 34 +++++++++++++++---------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index a312d34..8b107a6 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -49,7 +49,7 @@ class Map: class Tile(Enum): EMPTY = ' ' - WALL = '#' + WALL = 'ā–ˆ' FLOOR = '.' def is_wall(self) -> bool: diff --git a/dungeonbattle/proof_of_concept.py b/dungeonbattle/proof_of_concept.py index 2034cbf..9bd2dea 100644 --- a/dungeonbattle/proof_of_concept.py +++ b/dungeonbattle/proof_of_concept.py @@ -1,4 +1,4 @@ -from .interfaces import Map +from .interfaces import Map, Tile from .term_manager import TermManager @@ -19,13 +19,13 @@ def proof_of_concept() -> None: cur = [1, 6] # (y,x) # We are a squirrel - stdscr.addstr(1, 6, 'šŸæļø') + stdscr.addstr(1, 6, 'šŸæ') stdscr.refresh() while True: # Get movements of the user key = stdscr.getkey() - stdscr.addstr(cur[0], cur[1], '.') + 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 @@ -42,4 +42,4 @@ def proof_of_concept() -> None: if next_tile.can_walk(): cur = next_pos # Draw the squirrel - stdscr.addstr(cur[0], cur[1], 'šŸæļø') + stdscr.addstr(cur[0], cur[1], 'šŸæ') diff --git a/example_map.txt b/example_map.txt index 4111fae..bc0c464 100644 --- a/example_map.txt +++ b/example_map.txt @@ -1,17 +1,17 @@ - ####### ############# - #.....# #...........# - #.....# #####...........# - #.....# #...............# - #.##### #.###...........# - #.# #.# #...........# - #.# #.# ############# - #.# #.# - #.#### #.# - #....# #.# - ####.###################.# - #.....................# ################# - #.....................# #...............# - #.....................#######...............# - #...........................................# - #.....................#######...............# - ####################### ################# + ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ + ā–ˆ.....ā–ˆ ā–ˆ...........ā–ˆ + ā–ˆ.....ā–ˆ ā–ˆā–ˆā–ˆā–ˆā–ˆ...........ā–ˆ + ā–ˆ.....ā–ˆ ā–ˆ...............ā–ˆ + ā–ˆ.ā–ˆā–ˆā–ˆā–ˆā–ˆ ā–ˆ.ā–ˆā–ˆā–ˆ...........ā–ˆ + ā–ˆ.ā–ˆ ā–ˆ.ā–ˆ ā–ˆ...........ā–ˆ + ā–ˆ.ā–ˆ ā–ˆ.ā–ˆ ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ + ā–ˆ.ā–ˆ ā–ˆ.ā–ˆ + ā–ˆ.ā–ˆā–ˆā–ˆā–ˆ ā–ˆ.ā–ˆ + ā–ˆ....ā–ˆ ā–ˆ.ā–ˆ + ā–ˆā–ˆā–ˆā–ˆ.ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ.ā–ˆ + ā–ˆ.....................ā–ˆ ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ + ā–ˆ.....................ā–ˆ ā–ˆ...............ā–ˆ + ā–ˆ.....................ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ...............ā–ˆ + ā–ˆ...........................................ā–ˆ + ā–ˆ.....................ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ...............ā–ˆ + ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ From a0027ece641c904765cc2912c0dbc96f0f942766 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 18:22:20 +0200 Subject: [PATCH 17/17] Test got broken --- dungeonbattle/interfaces_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dungeonbattle/interfaces_test.py b/dungeonbattle/interfaces_test.py index 34a50f9..849e28b 100644 --- a/dungeonbattle/interfaces_test.py +++ b/dungeonbattle/interfaces_test.py @@ -8,7 +8,7 @@ class TestInterfaces(unittest.TestCase): """ Create a map and check that it is well parsed. """ - m = Map.load_from_string(".#\n#.\n") + m = Map.load_from_string(".ā–ˆ\nā–ˆ.\n") self.assertEqual(m.width, 2) self.assertEqual(m.height, 2) - self.assertEqual(m.draw_string(), ".#\n#.") + self.assertEqual(m.draw_string(), ".ā–ˆ\nā–ˆ.")