78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
import pygame
|
|
from src.settings import (
|
|
HUD_FONT, HUD_FONT_SIZE, HUD_FONT_BOLD,
|
|
COLOR_HUD_TEXT, COLOR_HP_BAR_GREEN, COLOR_HP_BAR_RED, COLOR_HP_BAR_BG,
|
|
HUD_BAR_WIDTH, HUD_BAR_HEIGHT,
|
|
)
|
|
|
|
|
|
def _draw_status_bar(screen, x, y, current, max_val, color):
|
|
# bg bar is always the same
|
|
pygame.draw.rect(screen, COLOR_HP_BAR_BG, (x, y, HUD_BAR_WIDTH, HUD_BAR_HEIGHT))
|
|
|
|
# hp bar
|
|
if max_val > 0:
|
|
current = max(0, min(current, max_val))
|
|
hp_width = int((current / max_val) * HUD_BAR_WIDTH)
|
|
pygame.draw.rect(screen, color, (x, y, hp_width, HUD_BAR_HEIGHT))
|
|
|
|
|
|
def _format_time(total_seconds):
|
|
mins = total_seconds // 60
|
|
secs = total_seconds % 60
|
|
return f"TIME {mins:02}:{secs:02}"
|
|
|
|
|
|
class HUD:
|
|
def __init__(self):
|
|
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))
|
|
|
|
# --- DOOR HP ---
|
|
door_x = 330
|
|
screen.blit(self.font.render("DOOR HP", True, COLOR_HUD_TEXT), (door_x, label_y))
|
|
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_x = 620
|
|
screen.blit(self.font.render("ZOMBIE HP", True, COLOR_HUD_TEXT), (zombie_x, label_y))
|
|
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_x = 910
|
|
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_str = _format_time(total_time)
|
|
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))
|