From 31ae39ab155244d134f8a093eacd447e0ba6533c Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 15:34:24 +0100 Subject: [PATCH] basic display wrapper --- dungeonbattle/display/__init__.py | 0 dungeonbattle/display/display.py | 11 ++++++++++ dungeonbattle/display/mapdisplay.py | 31 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 dungeonbattle/display/__init__.py create mode 100644 dungeonbattle/display/display.py create mode 100644 dungeonbattle/display/mapdisplay.py diff --git a/dungeonbattle/display/__init__.py b/dungeonbattle/display/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dungeonbattle/display/display.py b/dungeonbattle/display/display.py new file mode 100644 index 0000000..86c76b1 --- /dev/null +++ b/dungeonbattle/display/display.py @@ -0,0 +1,11 @@ +import curses +from .mapdisplay import MapDisplay + +class Display: + def __init__(self, game, screen): + self.screen = screen + self.game = game + self.mapDisplay = MapDisplay(game.m, curses.LINES, curses.COLS * 4/5) + + def refresh(self): + self.mapDisplay.refresh() \ No newline at end of file diff --git a/dungeonbattle/display/mapdisplay.py b/dungeonbattle/display/mapdisplay.py new file mode 100644 index 0000000..471aa91 --- /dev/null +++ b/dungeonbattle/display/mapdisplay.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +import curses +from dungeonbattle.interfaces import Map + +class MapDisplay: + + def __init__(self, m: Map, height: int, width: int): + self.width = width + self.height = height + self.map = m + self.pad = curses.newpad(m.height, m.width+1) + + 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) + + def display(self, y, x): + deltay, deltax = (self.height // 2) + 1, (self.width //2) + 1 + pminrow, pmincol = y-deltay, x-deltax + sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0) + deltay, deltax = self.height - deltay, self.width - deltax + smaxrow = self.map.height - (y + deltay) + self.height -1 + smaxrow = min(smaxrow, self.height-1) + smaxcol = self.map.width - (x + deltax) + self.width -1 + smaxcol = min(smaxcol, self.width-1) + pminrow = max(0, min(self.map.height, pminrow)) + pmincol = max(0, min(self.map.width, pmincol)) + self.pad.clear() + self.update_pad() + self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)