Add my first interface element: a Bar!

It renders a bar of a certain width and a percentage full. Use it to render HP.

Add a new Python package called interface. The Bar class lives there. Also add a
bunch of color defintions to a module called interface.color.
This commit is contained in:
Eryn Wells 2022-05-15 00:11:52 -07:00
parent 388754e5dd
commit 5a9df0a322
4 changed files with 46 additions and 2 deletions

View file

27
erynrl/interface/bar.py Normal file
View file

@ -0,0 +1,27 @@
# Eryn Wells <eryn@erynwells.me>
from . import color
from ..geometry import Point
class Bar:
def __init__(self, *, position: Point, width: int):
self.position = position
self.width = width
self._percent_filled = 1.0
@property
def percent_filled(self) -> float:
'''The percentage of this bar that should be filled, as a value between 0.0 and 1.0.'''
return self._percent_filled
@percent_filled.setter
def percent_filled(self, value):
self._percent_filled = min(1, max(0, value))
def render_to_console(self, console):
console.draw_rect(x=self.position.x, y=self.position.y, width=self.width, height=1, ch=1, bg=color.GREY10)
if self._percent_filled > 0:
filled_width = round(self._percent_filled * self.width)
console.draw_rect(x=self.position.x, y=self.position.y, width=filled_width, height=1, ch=1, bg=color.GREY50)

10
erynrl/interface/color.py Normal file
View file

@ -0,0 +1,10 @@
# Eryn Wells <eryn@erynwells.me>
'''
A bunch of colors.
'''
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY10 = (26, 26, 26)
GREY50 = (128, 128, 128)