repaired display

This commit is contained in:
eichhornchen 2020-11-09 01:33:23 +01:00
parent 8d5d9d38ff
commit a4876bb7af
7 changed files with 118 additions and 65 deletions

View File

@ -2,14 +2,28 @@ import curses
from typing import Any, Union from typing import Any, Union
from dungeonbattle.tests.screen import FakePad 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: class Display:
def __init__(self, screen: Any): map: Map
player: Player
def __init__(self, screen: Any, texture: Any):
self.screen = screen 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: def refresh(self, m : Map, p : Player) -> None:
raise NotImplementedError 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]: def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad() return curses.newpad(height, width) if self.screen else FakePad()

View File

@ -23,9 +23,10 @@ class MapDisplay:
for e in self.map.entities: for e in self.map.entities:
self.pad.addstr(e.y, e.x, self.pack.PLAYER) 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.map = m
self.player = p self.player = p
y, x = self.map.currenty, self.map.currentx
deltay, deltax = (self.height // 2) + 1, (self.width // 2) + 1 deltay, deltax = (self.height // 2) + 1, (self.width // 2) + 1
pminrow, pmincol = y - deltay, x - deltax pminrow, pmincol = y - deltay, x - deltax
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0) sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)

View File

@ -1,55 +1,48 @@
from dungeonbattle.display.display import Display
from dungeonbattle.menus import Menu from dungeonbattle.menus import Menu
from typing import Any from typing import Any
import curses
class MenuDisplay:
class MenuDisplay(Display): position: int
def __init__(self, screen: Any, menu: Menu,
topleftx: int, toplefty: int): def __init__(self, menu : Menu, screen: Any, height : int, width : int, topleftx: int, toplefty: int) :
super().__init__(screen) self.screen = screen
self.values = menu.values self.values = menu.values
self.menu = menu self.width = width
self.height = height
self.trueheight = len(menu.values) 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.topleftx = topleftx
self.toplefty = toplefty self.toplefty = toplefty
# Menu values are printed in pad #Menu values are printed in pad
self.pad = self.newpad(self.trueheight, self.truewidth + 1) 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 #Menu box
self.menubox = self.newpad(self.height, self.width) 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: def refresh(self, position : int) -> None:
self.menubox.addstr(0, 0, "" + "" * (self.width - 2) + "") self.position = position
for i in range(1, self.height - 2): if self.height-2>=position-1 :
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:
cornery = 0 cornery = 0
elif self.height - 2 >= self.trueheight - self.menu.position: elif self.height-2 >= self.trueheight-position :
cornery = self.trueheight - self.height + 2 cornery = self.trueheight-self.height+2
self.update_pad(self.menu.position)
self.menubox.refresh(0, 0, self.toplefty, self.topleftx, self.menubox.refresh(0, 0, self.toplefty, self.topleftx,
self.height + self.toplefty, self.height + self.toplefty,
self.width + self.topleftx) self.width + self.topleftx)
self.pad.refresh(cornery, 0, self.toplefty + 1, self.topleftx + 1, self.update_pad(position)
self.height - 3 + self.toplefty, self.pad.refresh(cornery, 0, self.toplefty+1, self.topleftx+1,
self.width - 2 + self.topleftx) self.height-2 + self.toplefty,
self.width-2 + self.topleftx)

View File

@ -5,19 +5,22 @@ from dungeonbattle.entities.player import Player
class StatsDisplay(Display): 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): topleftx: int, toplefty: int):
super().__init__(screen) super().__init__(screen)
self.width = width
self.height = height
self.topleftx = topleftx self.topleftx = topleftx
self.toplefty = toplefty 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: def update_pad(self) -> None:
string = "" string = ""
for i in range(self.width - 1): for i in range(self.width - 1):
string = string + "-" string = string + "-"
string = string
self.pad.addstr(0, 0, string) self.pad.addstr(0, 0, string)
string2 = "Player -- LVL {} EXP {}/{} HP {}/{}"\ string2 = "Player -- LVL {} EXP {}/{} HP {}/{}"\
.format(self.player.level, self.player.current_xp, .format(self.player.level, self.player.current_xp,
@ -34,8 +37,8 @@ class StatsDisplay(Display):
string3 = string3 + " " string3 = string3 + " "
self.pad.addstr(2, 0, string3) self.pad.addstr(2, 0, string3)
def refresh(self) -> None: def refresh(self, p : Player) -> None:
self.ensure_resized(self.pad) self.player = p
self.pad.clear() self.pad.clear()
self.update_pad() self.update_pad()
self.pad.refresh(0, 0, self.toplefty, self.topleftx, self.pad.refresh(0, 0, self.toplefty, self.topleftx,

View File

@ -33,7 +33,7 @@ class Game:
player: Player player: Player
menu_display: MenuDisplay menu_display: MenuDisplay
map_display: MapDisplay map_display: MapDisplay
current_display: Display display: Display
def __init__(self) -> None: def __init__(self) -> None:
""" """
@ -54,11 +54,8 @@ class Game:
self.player = Player() self.player = Player()
self.player.move(1, 6) self.player.move(1, 6)
self.map.add_entity(self.player) self.map.add_entity(self.player)
self.menu_display = MenuDisplay(screen, self.main_menu, 0, 0) self.display = Display(screen, TexturePack.get_pack(self.settings.TEXTURE_PACK))
self.map_display = MapDisplay( # self.menu_display = MenuDisplay(screen, self.main_menu, 0, 0)
screen, self.map, self.player,
TexturePack.get_pack(self.settings.TEXTURE_PACK))
self.current_display = self.menu_display
@staticmethod @staticmethod
def load_game(filename: str) -> None: def load_game(filename: str) -> None:
@ -74,7 +71,7 @@ class Game:
while True: while True:
screen.clear() screen.clear()
screen.refresh() screen.refresh()
self.current_display.refresh() self.display.refresh()
key = screen.getkey() key = screen.getkey()
self.handle_key_pressed(self.translate_key(key)) self.handle_key_pressed(self.translate_key(key))
@ -110,7 +107,7 @@ class Game:
self.handle_key_pressed_main_menu(key) self.handle_key_pressed_main_menu(key)
elif self.state == GameMode.SETTINGS: elif self.state == GameMode.SETTINGS:
self.handle_key_pressed_settings(key) self.handle_key_pressed_settings(key)
self.current_display.refresh() self.display.refresh()
def handle_key_pressed_play(self, key: KeyValues) -> None: def handle_key_pressed_play(self, key: KeyValues) -> None:
""" """
@ -126,7 +123,7 @@ class Game:
self.player.move_right() self.player.move_right()
elif key == KeyValues.SPACE: elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU 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: def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
""" """
@ -140,7 +137,7 @@ class Game:
option = self.main_menu.validate() option = self.main_menu.validate()
if option == menus.MainMenuValues.START: if option == menus.MainMenuValues.START:
self.state = GameMode.PLAY self.state = GameMode.PLAY
self.current_display = self.map_display self.display = self.map_display
elif option == menus.MainMenuValues.SETTINGS: elif option == menus.MainMenuValues.SETTINGS:
self.state = GameMode.SETTINGS self.state = GameMode.SETTINGS
elif option == menus.MainMenuValues.EXIT: elif option == menus.MainMenuValues.EXIT:
@ -152,4 +149,4 @@ class Game:
""" """
if key == KeyValues.SPACE: if key == KeyValues.SPACE:
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
self.current_display = self.menu_display self.display = self.menu_display

View File

@ -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)

17
example_map2.txt Normal file
View File

@ -0,0 +1,17 @@
####### #############
#.....# #...........#
#.....# #####...........#
#.....# #...............#
#.##### #.###...........#
#.# #.# #...........#
#.# #.# #############
#.# #.#
#.#### #.#
#....# #.#
####.###################.#
#.....................# #################
#.....................# #...............#
#.....................#######...............#
#...........................................#
#.....................#######...............#
####################### #################