86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
import pygame
|
|
import os
|
|
import math
|
|
from actors.enums import Colors
|
|
from actors.pacman import ActorPacman
|
|
|
|
class HUD:
|
|
def __init__(self, screen_width, screen_height, font_size=24, highscore_file="highscore.txt", initial_lives=3, cherry_image=None):
|
|
self.score = 0
|
|
self.lives = initial_lives
|
|
self.p_man = []
|
|
self.highscore_file = highscore_file
|
|
self.highscore = self.load_highscore()
|
|
|
|
self.font = pygame.font.Font(None, font_size)
|
|
self.color = Colors.WHITE.value
|
|
self.screen_width = screen_width
|
|
self.screen_height = screen_height
|
|
|
|
self.padding = 10
|
|
self.life_radius = 10
|
|
self.life_spacing = 30
|
|
|
|
self.cherry_image = pygame.transform.scale(cherry_image, (24, 24)) if cherry_image else None
|
|
|
|
def load_highscore(self):
|
|
if os.path.exists(self.highscore_file):
|
|
with open(self.highscore_file, 'r') as file:
|
|
try:
|
|
return int(file.read())
|
|
except ValueError:
|
|
return 0
|
|
return 0
|
|
|
|
def save_highscore(self):
|
|
with open(self.highscore_file, 'w') as file:
|
|
file.write(str(self.highscore))
|
|
|
|
def add_points(self, points):
|
|
self.score += points
|
|
if self.score > self.highscore:
|
|
self.highscore = self.score
|
|
self.save_highscore()
|
|
|
|
def lose_life(self):
|
|
self.lives = max(0, self.lives - 1)
|
|
|
|
def reset(self, reset_score=True, reset_lives=True):
|
|
if reset_score:
|
|
self.score = 0
|
|
if reset_lives:
|
|
self.lives = 3
|
|
|
|
def draw_pacman_icon(self, screen, center):
|
|
start_angle = math.radians(30)
|
|
end_angle = math.radians(330)
|
|
rect = pygame.Rect(0, 0, self.life_radius * 2, self.life_radius * 2)
|
|
rect.center = center
|
|
# Mouth: draw arc first, then overlay circle to mask
|
|
pygame.draw.arc(screen, (255, 255, 0), rect, start_angle, end_angle, self.life_radius)
|
|
pygame.draw.circle(screen, (255, 255, 0), center, self.life_radius)
|
|
|
|
def draw(self, screen):
|
|
# Score (top-left)
|
|
score_text = self.font.render(f"Score: {self.score}", True, self.color)
|
|
screen.blit(score_text, (self.padding, self.padding))
|
|
|
|
# Highscore (top-center)
|
|
highscore_text = self.font.render(f"High Score: {self.highscore}", True, self.color)
|
|
highscore_rect = highscore_text.get_rect(midtop=(self.screen_width // 2, self.padding))
|
|
screen.blit(highscore_text, highscore_rect)
|
|
|
|
# Lives (bottom-left)
|
|
for i in range(self.lives):
|
|
x = self.padding + i * self.life_spacing
|
|
y = self.screen_height - self.padding - self.life_radius
|
|
self.p_man.append(ActorPacman(screen, (x + self.life_radius, y)))
|
|
self.p_man[i].draw()
|
|
#self.draw_pacman_icon(screen, (x + self.life_radius, y))
|
|
|
|
# Level indicator (bottom-right)
|
|
if self.cherry_image:
|
|
cherry_x = self.screen_width - self.padding - self.cherry_image.get_width()
|
|
cherry_y = self.screen_height - self.padding - self.cherry_image.get_height()
|
|
screen.blit(self.cherry_image, (cherry_x, cherry_y))
|