Add possibility to define the background color of entities (black in ASCII, white in Unicode)

This commit is contained in:
Yohann D'ANELLO 2020-11-10 22:30:55 +01:00
parent ec6b90fba2
commit a8223aab2e
5 changed files with 32 additions and 7 deletions

View File

@ -19,6 +19,13 @@ class Display:
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()
def init_pair(self, number: int, foreground: int, background: int) -> None:
return curses.init_pair(number, foreground, background) \
if self.screen else None
def color_pair(self, number: int) -> int:
return curses.color_pair(number) if self.screen else 0
def resize(self, y: int, x: int, height: int, width: int) -> None: def resize(self, y: int, x: int, height: int, width: int) -> None:
self.x = x self.x = x
self.y = y self.y = y

View File

@ -15,10 +15,13 @@ class MapDisplay(Display):
self.pad = self.newpad(m.height, self.pack.tile_width * m.width + 1) self.pad = self.newpad(m.height, self.pack.tile_width * m.width + 1)
def update_pad(self) -> None: def update_pad(self) -> None:
self.pad.addstr(0, 0, self.map.draw_string(self.pack)) self.init_pair(1, self.pack.tile_fg_color, self.pack.tile_bg_color)
self.init_pair(2, self.pack.entity_fg_color, self.pack.entity_bg_color)
self.pad.addstr(0, 0, self.map.draw_string(self.pack),
self.color_pair(1))
for e in self.map.entities: for e in self.map.entities:
self.pad.addstr(e.y, self.pack.tile_width * e.x, self.pad.addstr(e.y, self.pack.tile_width * e.x,
self.pack[e.name.upper()]) self.pack[e.name.upper()], self.color_pair(2))
def display(self) -> None: def display(self) -> None:
y, x = self.map.currenty, self.map.currentx y, x = self.map.currenty, self.map.currentx

View File

@ -1,3 +1,4 @@
import curses
from typing import Any from typing import Any
@ -6,6 +7,10 @@ class TexturePack:
name: str name: str
tile_width: int tile_width: int
tile_fg_color: int
tile_bg_color: int
entity_fg_color: int
entity_bg_color: int
EMPTY: str EMPTY: str
WALL: str WALL: str
FLOOR: str FLOOR: str
@ -19,7 +24,7 @@ class TexturePack:
self.__dict__.update(**kwargs) self.__dict__.update(**kwargs)
TexturePack._packs[name] = self TexturePack._packs[name] = self
def __getitem__(self, item) -> Any: def __getitem__(self, item: str) -> Any:
return self.__dict__[item] return self.__dict__[item]
@classmethod @classmethod
@ -30,6 +35,10 @@ class TexturePack:
TexturePack.ASCII_PACK = TexturePack( TexturePack.ASCII_PACK = TexturePack(
name="ascii", name="ascii",
tile_width=1, tile_width=1,
tile_fg_color=curses.COLOR_WHITE,
tile_bg_color=curses.COLOR_BLACK,
entity_fg_color=curses.COLOR_WHITE,
entity_bg_color=curses.COLOR_BLACK,
EMPTY=' ', EMPTY=' ',
WALL='#', WALL='#',
FLOOR='.', FLOOR='.',
@ -40,9 +49,13 @@ TexturePack.ASCII_PACK = TexturePack(
TexturePack.SQUIRREL_PACK = TexturePack( TexturePack.SQUIRREL_PACK = TexturePack(
name="squirrel", name="squirrel",
tile_width=2, tile_width=2,
tile_fg_color=curses.COLOR_WHITE,
tile_bg_color=curses.COLOR_BLACK,
entity_fg_color=curses.COLOR_WHITE,
entity_bg_color=curses.COLOR_WHITE,
EMPTY=' ', EMPTY=' ',
WALL='██', WALL='🟫',
FLOOR='..', FLOOR='██',
PLAYER='🐿 ', PLAYER='🐿 ',
HEDGEHOG='🦔', HEDGEHOG='🦔',
) )

View File

@ -13,6 +13,8 @@ class TermManager: # pragma: no cover
curses.cbreak() curses.cbreak()
# make cursor invisible # make cursor invisible
curses.curs_set(False) curses.curs_set(False)
# Enable colors
curses.start_color()
def __enter__(self): def __enter__(self):
return self return self

View File

@ -3,7 +3,7 @@ class FakePad:
In order to run tests, we simulate a fake curses pad that accepts functions In order to run tests, we simulate a fake curses pad that accepts functions
but does nothing with them. but does nothing with them.
""" """
def addstr(self, y: int, x: int, message: str) -> None: def addstr(self, y: int, x: int, message: str, color: int = 0) -> None:
pass pass
def refresh(self, pminrow: int, pmincol: int, sminrow: int, def refresh(self, pminrow: int, pmincol: int, sminrow: int,