Files
PyGame-Pacman/scoreboard.py
2025-04-17 21:40:49 +02:00

42 lines
1.3 KiB
Python

import pygame
import os
from actors.enums import Colors
class Scoreboard:
def __init__(self, font_size=24, highscore_file="highscore.txt"):
self.score = 0
self.highscore_file = highscore_file
self.highscore = self.load_highscore()
self.font = pygame.font.Font(None, font_size)
self.color = Colors.WHITE.value
self.score_pos = (10, 10)
self.highscore_pos = (10, 40)
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 reset(self):
self.score = 0
def draw(self, screen):
score_text = self.font.render(f"Score: {self.score}", True, self.color)
highscore_text = self.font.render(f"High Score: {self.highscore}", True, self.color)
screen.blit(score_text, self.score_pos)
screen.blit(highscore_text, self.highscore_pos)