feat(highscore): add draw function in highscore
This commit is contained in:
+46
-2
@@ -1,9 +1,14 @@
|
|||||||
import json
|
import json
|
||||||
|
import pygame
|
||||||
|
from settings import HIGHSCORE_COLOR_FONT, HIGHSCORE_COLOR_TABLE
|
||||||
|
|
||||||
class Highscore:
|
class Highscore:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# fix path to json file with highscores
|
# 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
|
# check if file already exists, otherwise create it
|
||||||
try:
|
try:
|
||||||
@@ -89,4 +94,43 @@ class Highscore:
|
|||||||
|
|
||||||
formatted_scores.append(line)
|
formatted_scores.append(line)
|
||||||
|
|
||||||
return formatted_scores
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user