From b12764b355996cc83cec0db73817675183f080aa Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 14:00:38 +0200 Subject: [PATCH 1/5] 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 1674427..838cbec 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -26,6 +26,9 @@ class Map: for x, c in enumerate(line)] for y, line in enumerate(lines)] return Map(width, height, chars) + def draw_string(self) -> str: + return "\n".join("".join(tile.char for tile in line) for line in self.tiles) + class Tile: x: int 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 f3e42ae295eb1dd3ee0dd0dd7b36319ad802154d Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 16 Oct 2020 15:42:05 +0200 Subject: [PATCH 2/5] 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 2e82849395833d950f1c62e8a50397c02c361ed6 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Sun, 11 Oct 2020 15:24:51 +0200 Subject: [PATCH 3/5] Sanitizing data structure --- dungeonbattle/interfaces.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 838cbec..2cef098 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -22,31 +22,22 @@ class Map: lines = [line for line in lines if line] height = len(lines) width = len(lines[0]) - chars = [[Tile.from_char(c, x, y) - for x, c in enumerate(line)] for y, line in enumerate(lines)] - return Map(width, height, chars) + return Map(width, height, lines) def draw_string(self) -> str: return "\n".join("".join(tile.char for tile in line) for line in self.tiles) -class Tile: - x: int - y: int - char: str - - @staticmethod - def from_char(c: str, x: int, y: int): - t = Tile() - t.x = x - t.y = y - t.char = c - return c - - class Entity: - tile: Tile + y: int + x: int + img: str + + def __init__(self, y: int, x: int, img: str): + self.y = y + self.x = x + self.img = img def move(self, x: int, y: int) -> None: - self.tile.x = x - self.tile.y = y + self.x = x + self.y = y From 42c9957cebaceabb4fc8d9a86613e56d2b00634a Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 15:52:47 +0200 Subject: [PATCH 4/5] Added entities management to class Map --- dungeonbattle/interfaces.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 2cef098..a28fe1a 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -1,14 +1,16 @@ #!/usr/bin/env python + class Map: width: int height: int tiles: list - def __init__(self, width: int, height: int, tiles: list): + def __init__(self, width: int, height: int, tiles: list, entities: list): self.width = width self.height = height self.tiles = tiles + self.entities = entities @staticmethod def load(filename: str): @@ -22,7 +24,7 @@ class Map: lines = [line for line in lines if line] height = len(lines) width = len(lines[0]) - return Map(width, height, lines) + return Map(width, height, lines, []) def draw_string(self) -> str: return "\n".join("".join(tile.char for tile in line) for line in self.tiles) From 7d13a782358f1b59406d08aafab5bfd15da8a8ca Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 15:54:33 +0200 Subject: [PATCH 5/5] New class MapDisplay implements displaying maps to terminal. See #1 --- dungeonbattle/mapdisplay.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 dungeonbattle/mapdisplay.py diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py new file mode 100644 index 0000000..7ae7cc7 --- /dev/null +++ b/dungeonbattle/mapdisplay.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +import curses +from dungeonbattle.interfaces import Map + +class MapDisplay: + + def __init__(self, m: Map): + self.map = m + self.pad = curses.newpad(m.height, m.width) + + def update_pad(self): + for i in range(self.map.height): + self.pad.addstr(i, 0, self.map.tiles[i][:-1]) + for e in self.map.entities: + self.pad.addch(e.y, e.x, e.img) + + def display(self, y, x): + self.pad.clear() + deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1 + pminrow, pmincol = y-deltay, x-deltax + sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0) + deltay, deltax = curses.LINES - deltay, curses.COLS - deltay + smaxrow = self.map.height - (y + deltay) + curses.LINES + smaxrow = min(smaxrow, curses.LINES-1) + smaxcol = self.width - (x + deltax) + curses.COLS + smaxcol = min(smaxcol, curses.COLS-1) + pminrow = max(0, min(self.map.height, pminrow)) + pmincol = max(0, min(self.map.width, pmincol)) + self.update_pad() + self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)