From 5a9df0a3223a7e0a0c6aba1f5287e17f4c8d04c8 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 15 May 2022 00:11:52 -0700 Subject: [PATCH] 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. --- erynrl/engine.py | 11 +++++++++-- erynrl/interface/__init__.py | 0 erynrl/interface/bar.py | 27 +++++++++++++++++++++++++++ erynrl/interface/color.py | 10 ++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 erynrl/interface/__init__.py create mode 100644 erynrl/interface/bar.py create mode 100644 erynrl/interface/color.py diff --git a/erynrl/engine.py b/erynrl/engine.py index 4b5125b..db237f1 100644 --- a/erynrl/engine.py +++ b/erynrl/engine.py @@ -13,7 +13,9 @@ from . import monsters from .actions import Action, ActionResult from .ai import HostileEnemy from .events import GameOverEventHandler, MainGameEventHandler -from .geometry import Size +from .geometry import Point, Size +from .interface.bar import Bar +from .interface import color from .map import Map from .object import Actor, Entity, Hero, Monster @@ -76,12 +78,17 @@ class Engine: self.update_field_of_view() + self.hit_points_bar = Bar(position=Point(4, 47), width=20) + def print_to_console(self, console): '''Print the whole game to the given console.''' self.map.print_to_console(console) + console.print(x=1, y=47, string=f'HP:') hp, max_hp = self.hero.fighter.hit_points, self.hero.fighter.maximum_hit_points - console.print(x=1, y=47, string=f'HP: {hp}/{max_hp}') + self.hit_points_bar.percent_filled = hp / max_hp + self.hit_points_bar.render_to_console(console) + console.print(x=6, y=47, string=f'{hp}/{max_hp}', fg=color.WHITE) for ent in sorted(self.entities, key=lambda e: e.render_order.value): # Only print entities that are in the field of view diff --git a/erynrl/interface/__init__.py b/erynrl/interface/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/erynrl/interface/bar.py b/erynrl/interface/bar.py new file mode 100644 index 0000000..3bc12df --- /dev/null +++ b/erynrl/interface/bar.py @@ -0,0 +1,27 @@ +# Eryn Wells + +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) diff --git a/erynrl/interface/color.py b/erynrl/interface/color.py new file mode 100644 index 0000000..3dfd44f --- /dev/null +++ b/erynrl/interface/color.py @@ -0,0 +1,10 @@ +# Eryn Wells + +''' +A bunch of colors. +''' + +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +GREY10 = (26, 26, 26) +GREY50 = (128, 128, 128) \ No newline at end of file