diff --git a/scoreboard.py b/scoreboard.py index 560ed6a..1304553 100644 --- a/scoreboard.py +++ b/scoreboard.py @@ -1,18 +1,42 @@ import pygame +import os +from enums import Color class Scoreboard: - def __init__(self, font_size=24): + 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 = (255, 255, 255) - self.position = (10, 10) + self.color = Color.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) - screen.blit(score_text, self.position) \ No newline at end of file + 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) \ No newline at end of file