Merge branch 'feat/startscreen' into 'main'
feat: implement highscore display in startscreen See merge request swd25-bootcamp/silent-fh!23
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
|
||||||
|
|||||||
@@ -120,3 +120,7 @@ HUD_BAR_X = 150 # left startpoint for hp bars
|
|||||||
HUD_Y_DOOR = 20
|
HUD_Y_DOOR = 20
|
||||||
HUD_Y_ZOMBIE = 50
|
HUD_Y_ZOMBIE = 50
|
||||||
HUD_Y_ECTS = 85
|
HUD_Y_ECTS = 85
|
||||||
|
|
||||||
|
# --- HIGHSCORE ---
|
||||||
|
HIGHSCORE_COLOR_FONT = (0, 0, 0)
|
||||||
|
HIGHSCORE_COLOR_TABLE = (150, 255, 0)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from src.settings import (
|
|||||||
COLOR_STARTSCREEN_FONT_LABEL,
|
COLOR_STARTSCREEN_FONT_LABEL,
|
||||||
)
|
)
|
||||||
from src.text_input import TextInput
|
from src.text_input import TextInput
|
||||||
|
from src.highscore import Highscore
|
||||||
|
|
||||||
class Startscreen:
|
class Startscreen:
|
||||||
def __init__(self, screen):
|
def __init__(self, screen):
|
||||||
@@ -42,6 +43,8 @@ class Startscreen:
|
|||||||
20,
|
20,
|
||||||
310)
|
310)
|
||||||
|
|
||||||
|
self.highscore = Highscore()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# set background color
|
# set background color
|
||||||
self.screen.fill(COLOR_STARTSCREEN_BACKGROUND)
|
self.screen.fill(COLOR_STARTSCREEN_BACKGROUND)
|
||||||
@@ -70,6 +73,9 @@ class Startscreen:
|
|||||||
label_input_error = self.font_label.render("Student not found. Please enter valid name/id.", True,
|
label_input_error = self.font_label.render("Student not found. Please enter valid name/id.", True,
|
||||||
(255, 0, 0))
|
(255, 0, 0))
|
||||||
|
|
||||||
|
# draw highscore table
|
||||||
|
self.highscore.draw(self.screen, 560, 90)
|
||||||
|
|
||||||
# get all events
|
# get all events
|
||||||
events = pygame.event.get()
|
events = pygame.event.get()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user