From 80ad07c7959021dbf3d4a5b50868d71996662200 Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Fri, 8 May 2026 17:34:52 +0200 Subject: [PATCH] feat(highscore): add draw function in highscore --- src/highscore.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/highscore.py b/src/highscore.py index 71f443b..0d84f69 100644 --- a/src/highscore.py +++ b/src/highscore.py @@ -1,9 +1,14 @@ import json +import pygame +from settings import HIGHSCORE_COLOR_FONT, HIGHSCORE_COLOR_TABLE class Highscore: def __init__(self): # fix path to json file with highscores - self.path = "./highscore.json" + self.path = "src/highscore.json" + + self.font_title = pygame.font.SysFont("Arial", 28, bold=True) + self.font_score = pygame.font.SysFont("Arial", 18, bold=True) # check if file already exists, otherwise create it try: @@ -89,4 +94,43 @@ class Highscore: formatted_scores.append(line) - return formatted_scores \ No newline at end of file + return formatted_scores + + def draw(self, screen, x, y): + # draw box background + box_rect = pygame.Rect(x, y, 500, 300) + pygame.draw.rect(screen, HIGHSCORE_COLOR_TABLE, box_rect, border_radius=12) + pygame.draw.rect(screen, HIGHSCORE_COLOR_TABLE, box_rect, border_radius=12) + + # title + title = self.font_title.render( + "ECTS Leaderboard", + True, + HIGHSCORE_COLOR_FONT + ) + screen.blit(title, (x + 20, y + 20)) + + # Get highscore lines + highscore_lines = self.get_top_scores_formatted() + + + if len(highscore_lines) == 0: + no_scores_text = self.font_score.render( + "No students ranked yet.", + True, + HIGHSCORE_COLOR_FONT + ) + screen.blit(no_scores_text, (x + 20, y + 70)) + return + + # Draw scores + score_y = y + 70 + + for line in highscore_lines: + score_text = self.font_score.render( + line, + True, + HIGHSCORE_COLOR_FONT + ) + screen.blit(score_text, (x + 20, score_y)) + score_y += 35