feat: update hud for usage in game
This commit is contained in:
+45
-15
@@ -3,20 +3,20 @@ from src.settings import (
|
|||||||
HUD_FONT, HUD_FONT_SIZE, HUD_FONT_BOLD,
|
HUD_FONT, HUD_FONT_SIZE, HUD_FONT_BOLD,
|
||||||
COLOR_HUD_TEXT, COLOR_HP_BAR_GREEN, COLOR_HP_BAR_RED, COLOR_HP_BAR_BG,
|
COLOR_HUD_TEXT, COLOR_HP_BAR_GREEN, COLOR_HP_BAR_RED, COLOR_HP_BAR_BG,
|
||||||
HUD_BAR_WIDTH, HUD_BAR_HEIGHT,
|
HUD_BAR_WIDTH, HUD_BAR_HEIGHT,
|
||||||
HUD_X_OFFSET, HUD_BAR_X, HUD_Y_DOOR, HUD_Y_ZOMBIE, HUD_Y_ECTS
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# draw status bar (background bar and the colored hp bar)
|
|
||||||
def _draw_status_bar(screen, x, y, current, max_val, color):
|
def _draw_status_bar(screen, x, y, current, max_val, color):
|
||||||
# bg bar is always the same
|
# bg bar is always the same
|
||||||
pygame.draw.rect(screen, COLOR_HP_BAR_BG, (x, y, HUD_BAR_WIDTH, HUD_BAR_HEIGHT))
|
pygame.draw.rect(screen, COLOR_HP_BAR_BG, (x, y, HUD_BAR_WIDTH, HUD_BAR_HEIGHT))
|
||||||
|
|
||||||
# hp bar
|
# hp bar
|
||||||
if max_val > 0:
|
if max_val > 0:
|
||||||
hp_width = (current / max_val) * HUD_BAR_WIDTH
|
current = max(0, min(current, max_val))
|
||||||
pygame.draw.rect((screen, color), (x, y, hp_width, HUD_BAR_HEIGHT))
|
hp_width = int((current / max_val) * HUD_BAR_WIDTH)
|
||||||
|
pygame.draw.rect(screen, color, (x, y, hp_width, HUD_BAR_HEIGHT))
|
||||||
|
|
||||||
|
|
||||||
# time formatter for MM:SS
|
|
||||||
def _format_time(total_seconds):
|
def _format_time(total_seconds):
|
||||||
mins = total_seconds // 60
|
mins = total_seconds // 60
|
||||||
secs = total_seconds % 60
|
secs = total_seconds % 60
|
||||||
@@ -26,22 +26,52 @@ def _format_time(total_seconds):
|
|||||||
class HUD:
|
class HUD:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.font = pygame.font.SysFont(HUD_FONT, HUD_FONT_SIZE, bold=HUD_FONT_BOLD)
|
self.font = pygame.font.SysFont(HUD_FONT, HUD_FONT_SIZE, bold=HUD_FONT_BOLD)
|
||||||
|
self.small_font = pygame.font.SysFont(HUD_FONT, max(16, HUD_FONT_SIZE - 4), bold=False)
|
||||||
|
|
||||||
|
def draw(self, screen, player_name, door, zombie, ects, total_time):
|
||||||
|
"""Draw the game HUD at the bottom of the screen."""
|
||||||
|
|
||||||
|
panel_height = 86
|
||||||
|
panel_rect = pygame.Rect(0, screen.get_height() - panel_height, screen.get_width(), panel_height)
|
||||||
|
pygame.draw.rect(screen, (10, 10, 15), panel_rect)
|
||||||
|
pygame.draw.rect(screen, (70, 70, 80), panel_rect, 2)
|
||||||
|
|
||||||
|
top = panel_rect.top + 12
|
||||||
|
label_y = top
|
||||||
|
bar_y = top + 34
|
||||||
|
|
||||||
|
# --- PLAYER ---
|
||||||
|
player_text = f"PLAYER: {player_name or 'Student'}"
|
||||||
|
screen.blit(self.font.render(player_text, True, COLOR_HUD_TEXT), (20, label_y))
|
||||||
|
|
||||||
def draw(self, screen, door, zombie, ects, total_time):
|
|
||||||
# --- DOOR HP ---
|
# --- DOOR HP ---
|
||||||
_draw_status_bar(screen, HUD_BAR_X, HUD_Y_DOOR, door.current_hp, COLOR_HP_BAR_GREEN)
|
door_x = 330
|
||||||
# -5 px, small off-set to align the text better with hp bar
|
screen.blit(self.font.render("DOOR HP", True, COLOR_HUD_TEXT), (door_x, label_y))
|
||||||
screen.blit(self.font.render("DOOR HP ", True, COLOR_HUD_TEXT), (HUD_X_OFFSET, HUD_Y_DOOR - 5))
|
if door is not None:
|
||||||
|
_draw_status_bar(screen, door_x, bar_y, door.current_hp, door.max_hp, COLOR_HP_BAR_GREEN)
|
||||||
|
hp_text = f"{door.current_hp}/{door.max_hp}"
|
||||||
|
else:
|
||||||
|
_draw_status_bar(screen, door_x, bar_y, 0, 1, COLOR_HP_BAR_GREEN)
|
||||||
|
hp_text = "--/--"
|
||||||
|
screen.blit(self.small_font.render(hp_text, True, COLOR_HUD_TEXT), (door_x + HUD_BAR_WIDTH + 10, bar_y - 2))
|
||||||
|
|
||||||
# --- ZOMBIE HP ---
|
# --- ZOMBIE HP ---
|
||||||
if zombie:
|
zombie_x = 620
|
||||||
_draw_status_bar(screen, HUD_BAR_X, HUD_Y_ZOMBIE, zombie.current_hp, zombie.max_hp, COLOR_HP_BAR_RED)
|
screen.blit(self.font.render("ZOMBIE HP", True, COLOR_HUD_TEXT), (zombie_x, label_y))
|
||||||
screen.blit(self.font.render("Zombie HP:", True, COLOR_HUD_TEXT), (HUD_X_OFFSET, HUD_Y_ZOMBIE - 5))
|
if zombie is not None:
|
||||||
|
_draw_status_bar(screen, zombie_x, bar_y, zombie.current_hp, zombie.max_hp, COLOR_HP_BAR_RED)
|
||||||
|
zombie_text = f"{zombie.current_hp}/{zombie.max_hp}"
|
||||||
|
else:
|
||||||
|
_draw_status_bar(screen, zombie_x, bar_y, 0, 1, COLOR_HP_BAR_RED)
|
||||||
|
zombie_text = "--/--"
|
||||||
|
screen.blit(self.small_font.render(zombie_text, True, COLOR_HUD_TEXT), (zombie_x + HUD_BAR_WIDTH + 10, bar_y - 2))
|
||||||
|
|
||||||
# --- ECTS ---
|
# --- ECTS ---
|
||||||
ects_info = f"ECTS: {ects.ects_balance} ({ects.ects_per_second}/s)"
|
ects_x = 910
|
||||||
screen.blit(self.font.render(ects_info, True, COLOR_HUD_TEXT), (HUD_X_OFFSET, HUD_Y_ECTS))
|
ects_text = f"ECTS: {ects.ects_balance} ({ects.ects_per_second}/s)"
|
||||||
|
screen.blit(self.font.render(ects_text, True, COLOR_HUD_TEXT), (ects_x, label_y))
|
||||||
|
|
||||||
# --- TIME ---
|
# --- TIME ---
|
||||||
time_str = _format_time(total_time)
|
time_str = _format_time(total_time)
|
||||||
screen.blit(self.font.render(time_str, True, COLOR_HUD_TEXT), (screen.get_width() - 150, HUD_Y_DOOR - 5))
|
time_surface = self.font.render(time_str, True, COLOR_HUD_TEXT)
|
||||||
|
screen.blit(time_surface, (screen.get_width() - time_surface.get_width() - 20, label_y))
|
||||||
|
|||||||
Reference in New Issue
Block a user