From cc4eaf7d2fcb43531d3c6800d1fe481bb8094a51 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sun, 6 Mar 2016 01:25:25 +0200 Subject: [PATCH] Working HUD. --- koperkapel/hud.py | 35 ++++++++++++++++++++++++++++++----- koperkapel/serums.py | 6 ++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/koperkapel/hud.py b/koperkapel/hud.py index 4bce86c..8668b6c 100644 --- a/koperkapel/hud.py +++ b/koperkapel/hud.py @@ -1,5 +1,7 @@ """ Roach up display. """ +import math +from pygame.surface import Surface from .actors.surf import SurfActor from .serums import default_serums @@ -10,12 +12,35 @@ class HudActor(SurfActor): self.stats = stats super().__init__(surf=self._surf, **kw) + def _build_bar(self, value, *args, **kw): + icon = default_serums.assemble_icon(*args, **kw) + rect = icon.get_rect() + value = math.ceil(value) + surf = Surface((rect.w, rect.h * value)).convert_alpha() + surf.fill((255, 255, 255, 0)) + x, y = 0, (surf.get_rect().h - rect.h) + for i in range(value): + surf.blit(icon, (x, y)) + y -= rect.h / 4 + return surf + def _rebuild_hud_surf(self): - smart = default_serums.assemble_icon("smart") - fast = default_serums.assemble_icon("fast") - strong = default_serums.assemble_icon("strong") - health = default_serums.assemble_icon("strong") - return smart + stats = self._stats + bars = [ + self._build_bar(stats.smart, "smart"), + self._build_bar(stats.fast, "fast"), + self._build_bar(stats.strong, "strong"), + self._build_bar(stats.health / 5, "strong", color="health"), + ] + rects = [b.get_rect() for b in bars] + h, w = max(r.h for r in rects), sum(r.w for r in rects) + x = (w - rects[0].w) / 2 + surf = Surface((w, h)).convert_alpha() + surf.fill((255, 255, 255, 0)) + for i, bar in enumerate(bars): + surf.blit(bar, (x, h - rects[i].h)) + x += rects[i].w / 2 + return surf @property def stats(self): diff --git a/koperkapel/serums.py b/koperkapel/serums.py index 5162180..f801639 100644 --- a/koperkapel/serums.py +++ b/koperkapel/serums.py @@ -19,6 +19,7 @@ SERUM_OVERLAY_COLORS = { "smart": (0, 0, 255, 255), # blue "fast": (0, 255, 0, 255), # green "strong": (255, 0, 255, 255), # purple + "health": (255, 0, 0, 255), # red "none": (170, 68, 0, 255), # brown } @@ -41,12 +42,13 @@ class SerumFactory: def __init__(self, suffix): self.suffix = suffix - def assemble_icon(self, name): + def assemble_icon(self, name, color=None): assert name in SERUMS + color = color or name serum_icon = images.load(safepath("serum%s/%s") % ( self.suffix, SERUM_TILENAME_MAP[name],)) frame = serum_icon.copy() - frame.fill(SERUM_OVERLAY_COLORS[name], None, BLEND_RGBA_MULT) + frame.fill(SERUM_OVERLAY_COLORS[color], None, BLEND_RGBA_MULT) return frame def assemble(self, name): -- 2.34.1