From a4876bb7affc87d36ec96d9903c624452c993b23 Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Mon, 9 Nov 2020 01:33:23 +0100 Subject: [PATCH] repaired display --- dungeonbattle/display/display.py | 22 ++++++-- dungeonbattle/display/mapdisplay.py | 3 +- dungeonbattle/display/menudisplay.py | 79 ++++++++++++--------------- dungeonbattle/display/statsdisplay.py | 15 +++-- dungeonbattle/game.py | 19 +++---- dungeonbattle/test_display.py | 28 ++++++++++ example_map2.txt | 17 ++++++ 7 files changed, 118 insertions(+), 65 deletions(-) create mode 100644 dungeonbattle/test_display.py create mode 100644 example_map2.txt diff --git a/dungeonbattle/display/display.py b/dungeonbattle/display/display.py index 7604812..ac85211 100644 --- a/dungeonbattle/display/display.py +++ b/dungeonbattle/display/display.py @@ -2,14 +2,28 @@ import curses from typing import Any, Union from dungeonbattle.tests.screen import FakePad - +from dungeonbattle.display.mapdisplay import MapDisplay +from dungeonbattle.display.statsdisplay import StatsDisplay +from dungeonbattle.display.menudisplay import MenuDisplay +from dungeonbattle.interfaces import Map +from .entities.player import Player class Display: - def __init__(self, screen: Any): + map: Map + player: Player + + def __init__(self, screen: Any, texture: Any): self.screen = screen + self.texture = texture + self.mapdisplay = MapDisplay(self.screen, self.texture, curses.LINES * 4//5, curses.COLS) + self.statsdisplay = StatsDisplay(self.screen, curses.LINES//5, curses.COLS) - def refresh(self) -> None: - raise NotImplementedError + def refresh(self, m : Map, p : Player) -> None: + self.map = m + self.player = p + self.mapdisplay.refresh(m, p, ) + self.statsdisplay.refresh(self.player) + self.menudisplay.refresh(self.position) def newpad(self, height: int, width: int) -> Union[FakePad, Any]: return curses.newpad(height, width) if self.screen else FakePad() diff --git a/dungeonbattle/display/mapdisplay.py b/dungeonbattle/display/mapdisplay.py index 6ddc6d9..2bb59cf 100644 --- a/dungeonbattle/display/mapdisplay.py +++ b/dungeonbattle/display/mapdisplay.py @@ -23,9 +23,10 @@ class MapDisplay: for e in self.map.entities: self.pad.addstr(e.y, e.x, self.pack.PLAYER) - def display(self, m : Map, p : Player y: int, x: int) -> None: + def display(self, m : Map, p : Player) -> None: self.map = m self.player = p + y, x = self.map.currenty, self.map.currentx deltay, deltax = (self.height // 2) + 1, (self.width // 2) + 1 pminrow, pmincol = y - deltay, x - deltax sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0) diff --git a/dungeonbattle/display/menudisplay.py b/dungeonbattle/display/menudisplay.py index cc7a1f8..c8c6a3c 100644 --- a/dungeonbattle/display/menudisplay.py +++ b/dungeonbattle/display/menudisplay.py @@ -1,55 +1,48 @@ -from dungeonbattle.display.display import Display from dungeonbattle.menus import Menu from typing import Any +import curses - -class MenuDisplay(Display): - def __init__(self, screen: Any, menu: Menu, - topleftx: int, toplefty: int): - super().__init__(screen) +class MenuDisplay: + position: int + + def __init__(self, menu : Menu, screen: Any, height : int, width : int, topleftx: int, toplefty: int) : + self.screen = screen self.values = menu.values - self.menu = menu + self.width = width + self.height = height self.trueheight = len(menu.values) - self.truewidth = max(len(item.value) for item in menu.values) + self.truewidth = max(map(len,self.values)) self.topleftx = topleftx self.toplefty = toplefty - # Menu values are printed in pad - self.pad = self.newpad(self.trueheight, self.truewidth + 1) + #Menu values are printed in pad + self.pad = curses.newpad(self.trueheight,self.truewidth+1) + for i in range(self.trueheight-1) : + self.pad.addstr(i,0," " + self.values[i]) - # Menu box - self.menubox = self.newpad(self.height, self.width) + #Menu box + self.menubox = curses.newpad(self.height,self.width) + self.menubox.addstr(0,0,"┏"+"━"*(self.width-2)+"┓") + for i in range(1,self.height-2) : + self.menubox.addstr(i,0,"┃"+" "*(self.width-2)+"┃") + self.menubox.addstr(self.height-2, 0, "┗"+"━"*(self.width-2)+"┛") + + def update_pad(self) -> None: + for i in range(self.trueheight) : + self.pad.addstr(i,0," ") + self.pad.addstr(self.position,0,">") #set a marker on the selected line - def update_pad(self, position: int) -> None: - self.menubox.addstr(0, 0, "┏" + "━" * (self.width - 2) + "┓") - for i in range(1, self.height - 2): - self.menubox.addstr(i, 0, "┃" + " " * (self.width - 2) + "┃") - self.menubox.addstr(self.height - 2, 0, - "┗" + "━" * (self.width - 2) + "┛") - - for i in range(self.trueheight): - self.pad.addstr(i, 0, " " + self.values[i].value) - - for i in range(self.trueheight): - self.pad.addstr(i, 0, " ") - # set a marker in front of the selected line - self.pad.addstr(position, 0, ">") - - def refresh(self) -> None: - with open("/tmp/log", "a") as f: - f.write(f"{self.width}x{self.height}\n") - - self.ensure_resized(self.menubox, self.pad) - - if self.height - 2 >= self.menu.position - 1: + def refresh(self, position : int) -> None: + self.position = position + if self.height-2>=position-1 : cornery = 0 - elif self.height - 2 >= self.trueheight - self.menu.position: - cornery = self.trueheight - self.height + 2 - - self.update_pad(self.menu.position) + elif self.height-2 >= self.trueheight-position : + cornery = self.trueheight-self.height+2 + self.menubox.refresh(0, 0, self.toplefty, self.topleftx, - self.height + self.toplefty, - self.width + self.topleftx) - self.pad.refresh(cornery, 0, self.toplefty + 1, self.topleftx + 1, - self.height - 3 + self.toplefty, - self.width - 2 + self.topleftx) + self.height + self.toplefty, + self.width + self.topleftx) + self.update_pad(position) + self.pad.refresh(cornery, 0, self.toplefty+1, self.topleftx+1, + self.height-2 + self.toplefty, + self.width-2 + self.topleftx) diff --git a/dungeonbattle/display/statsdisplay.py b/dungeonbattle/display/statsdisplay.py index 9867181..b990ee9 100644 --- a/dungeonbattle/display/statsdisplay.py +++ b/dungeonbattle/display/statsdisplay.py @@ -5,19 +5,22 @@ from dungeonbattle.entities.player import Player class StatsDisplay(Display): - def __init__(self, screen: Any, player: Player, + self.player: Player + + def __init__(self, screen: Any, height: int, width: int, topleftx: int, toplefty: int): super().__init__(screen) + self.width = width + self.height = height self.topleftx = topleftx self.toplefty = toplefty - self.player = player - self.pad = self.newpad(self.height, self.width) + + self.pad = self.newpad(height, width) def update_pad(self) -> None: string = "" for i in range(self.width - 1): string = string + "-" - string = string self.pad.addstr(0, 0, string) string2 = "Player -- LVL {} EXP {}/{} HP {}/{}"\ .format(self.player.level, self.player.current_xp, @@ -34,8 +37,8 @@ class StatsDisplay(Display): string3 = string3 + " " self.pad.addstr(2, 0, string3) - def refresh(self) -> None: - self.ensure_resized(self.pad) + def refresh(self, p : Player) -> None: + self.player = p self.pad.clear() self.update_pad() self.pad.refresh(0, 0, self.toplefty, self.topleftx, diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 8d5258d..b67d38f 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -33,7 +33,7 @@ class Game: player: Player menu_display: MenuDisplay map_display: MapDisplay - current_display: Display + display: Display def __init__(self) -> None: """ @@ -54,11 +54,8 @@ class Game: self.player = Player() self.player.move(1, 6) self.map.add_entity(self.player) - self.menu_display = MenuDisplay(screen, self.main_menu, 0, 0) - self.map_display = MapDisplay( - screen, self.map, self.player, - TexturePack.get_pack(self.settings.TEXTURE_PACK)) - self.current_display = self.menu_display + self.display = Display(screen, TexturePack.get_pack(self.settings.TEXTURE_PACK)) +# self.menu_display = MenuDisplay(screen, self.main_menu, 0, 0) @staticmethod def load_game(filename: str) -> None: @@ -74,7 +71,7 @@ class Game: while True: screen.clear() screen.refresh() - self.current_display.refresh() + self.display.refresh() key = screen.getkey() self.handle_key_pressed(self.translate_key(key)) @@ -110,7 +107,7 @@ class Game: self.handle_key_pressed_main_menu(key) elif self.state == GameMode.SETTINGS: self.handle_key_pressed_settings(key) - self.current_display.refresh() + self.display.refresh() def handle_key_pressed_play(self, key: KeyValues) -> None: """ @@ -126,7 +123,7 @@ class Game: self.player.move_right() elif key == KeyValues.SPACE: self.state = GameMode.MAINMENU - self.current_display = self.menu_display + self.display = self.menu_display def handle_key_pressed_main_menu(self, key: KeyValues) -> None: """ @@ -140,7 +137,7 @@ class Game: option = self.main_menu.validate() if option == menus.MainMenuValues.START: self.state = GameMode.PLAY - self.current_display = self.map_display + self.display = self.map_display elif option == menus.MainMenuValues.SETTINGS: self.state = GameMode.SETTINGS elif option == menus.MainMenuValues.EXIT: @@ -152,4 +149,4 @@ class Game: """ if key == KeyValues.SPACE: self.state = GameMode.MAINMENU - self.current_display = self.menu_display + self.display = self.menu_display diff --git a/dungeonbattle/test_display.py b/dungeonbattle/test_display.py new file mode 100644 index 0000000..1225fc6 --- /dev/null +++ b/dungeonbattle/test_display.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +from dungeonbattle.interfaces import Map +from dungeonbattle.display.mapdisplay import MapDisplay +from dungeonbattle.display.statsdisplay import StatsDisplay +from dungeonbattle.settings import Settings +from dungeonbattle.display.texturepack import TexturePack +from dungeonbattle.entities.player import Player +import curses +import time + +def test(screen) : + s = Settings() + s.load_settings() + s.write_settings() + p = Player() + p.y = 1 + p.x = 6 + p.health = 15 + p.intelligence = 4 + p.charisma = 2 + p.dexterity = 3 + p.constitution = 4 + m = Map.load("example_map2.txt") + MD = MapDisplay(m, TexturePack.ASCII_PACK, curses.LINES * 4//5, curses.COLS) + MD.display(p.y,p.x) + SD = StatsDisplay(p,curses.LINES * 1//5, curses.COLS, 0, curses.LINES * 4//5) + SD.refresh() + time.sleep(6) diff --git a/example_map2.txt b/example_map2.txt new file mode 100644 index 0000000..4111fae --- /dev/null +++ b/example_map2.txt @@ -0,0 +1,17 @@ + ####### ############# + #.....# #...........# + #.....# #####...........# + #.....# #...............# + #.##### #.###...........# + #.# #.# #...........# + #.# #.# ############# + #.# #.# + #.#### #.# + #....# #.# + ####.###################.# + #.....................# ################# + #.....................# #...............# + #.....................#######...............# + #...........................................# + #.....................#######...............# + ####################### #################