From 1d8539ee82032a1d938806704fd5e3228d40b9f7 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 10 Jan 2022 14:49:59 -0500 Subject: [PATCH] Render health bar --- color.py | 14 ++++++++++++++ engine.py | 10 ++++++---- render_functions.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 color.py create mode 100644 render_functions.py diff --git a/color.py b/color.py new file mode 100644 index 0000000..a31803d --- /dev/null +++ b/color.py @@ -0,0 +1,14 @@ +white = (0xFF, 0xFF, 0xFF) +black = (0x0, 0x0, 0x0) + +player_atk = (0xE0, 0xE0, 0xE0) +enemy_atk = (0xFF, 0xC0, 0xC0) + +player_die = (0xFF, 0x30, 0x30) +enemy_die = (0xFF, 0xA0, 0x30) + +welcome_text = (0x20, 0xA0, 0xFF) + +bar_text = white +bar_filled = (0x0, 0x60, 0x0) +bar_empty = (0x40, 0x10, 0x10) diff --git a/engine.py b/engine.py index 7d2c0b0..8f8a2c9 100644 --- a/engine.py +++ b/engine.py @@ -7,6 +7,7 @@ from tcod.console import Console from tcod.map import compute_fov from input_handlers import MainGameEventHandler +from render_functions import render_bar if TYPE_CHECKING: from entity import Actor @@ -39,10 +40,11 @@ class Engine: def render(self, console: Console, context: Context) -> None: self.game_map.render(console) - console.print( - x=1, - y=47, - string=f"HP: {self.player.fighter.hp}/{self.player.fighter.max_hp}", + render_bar( + console, + current_value=self.player.fighter.hp, + maximum_value=self.player.fighter.max_hp, + total_width=20, ) # Actually output to screen diff --git a/render_functions.py b/render_functions.py new file mode 100644 index 0000000..0651e5a --- /dev/null +++ b/render_functions.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +import color + +if TYPE_CHECKING: + from tcod import Console + + +def render_bar( + console: Console, + current_value: int, + maximum_value: int, + total_width: int, +) -> None: + bar_width = int(float(current_value) / maximum_value * total_width) + + console.draw_rect(x=0, y=45, width=20, height=1, ch=1, bg=color.bar_empty) + + if bar_width > 0: + console.draw_rect( + x=0, + y=45, + width=bar_width, + height=1, + ch=1, + bg=color.bar_filled + ) + + console.print( + x=1, + y=45, + string=f"HP: {current_value}/{maximum_value}", + fg=color.bar_text, + )