Files
silent-fh/src/endscreen.py
T
2026-05-08 19:23:19 +02:00

130 lines
4.1 KiB
Python

import pygame
from src.button import Button
from src.settings import (COLOR_ENDSCREEN_BACKGROUND,
COLOR_ENDSCREEN_TEXT,
COLOR_ENDSCREEN_WIN,
COLOR_ENDSCREEN_LOSE,
COLOR_BUTTON_HOVER,
COLOR_BUTTON,
FONT_ENDSCREEN)
from src.highscore import Highscore
class Endscreen:
def __init__(self, screen, player_name, score_ects, time_survived, won):
self.screen = screen
self.player_name = player_name
self.score_ects = score_ects
self.time_survived = time_survived
self.won = won
# setup fonts
self.font_title = pygame.font.SysFont(FONT_ENDSCREEN, 52, bold=True)
self.font_label = pygame.font.SysFont(FONT_ENDSCREEN, 28)
self.font_small = pygame.font.SysFont(FONT_ENDSCREEN, 22)
# setup colors
self.background_color = COLOR_ENDSCREEN_BACKGROUND
self.text_color = COLOR_ENDSCREEN_TEXT
self.win_color = COLOR_ENDSCREEN_WIN
self.lose_color = COLOR_ENDSCREEN_LOSE
self.button_color = COLOR_BUTTON
self. button_hover_color = COLOR_BUTTON_HOVER
# Buttons
self.button_restart = Button(
"[ENTER] Try Again",
500,
70,
230,
440,
self.button_color,
self.button_hover_color
)
self.button_menu = Button(
"[ESC] Back to Menu",
500,
70,
230,
530,
self.button_color,
self.button_hover_color
)
self.highscore = Highscore()
def run(self):
# get events
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
return "quit"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
return "game"
if event.key == pygame.K_ESCAPE:
return "startscreen"
# draw background
self.screen.fill(self.background_color)
# create texts depending on win/lose
if self.won:
title_text = "EXAM PASSED!"
title_color = self.win_color
message_lines = [
"Congratulations, student.",
"You survived the night shift at FH Kapfenberg.",
"Your ECTS have ben successfully submitted."
]
else:
title_text = "EXMATRICULATED!"
title_color = self.lose_color
message_lines = [
"The lecturers found you.",
"Your assignment was not submitted in time.",
"Please retake the semester."
]
# draw title/texts
title = self.font_title.render(title_text, True, title_color)
title_rect = title.get_rect(center=(480, 90))
self.screen.blit(title, title_rect)
y = 160
for line in message_lines:
message = self.font_small.render(line, True, self.text_color)
message_rect = message.get_rect(center=(480, y))
self.screen.blit(message, message_rect)
y += 32
# draw results
name_text = self.font_label.render(f"Student: {self.player_name}", True, self.text_color)
score_text = self.font_label.render(f"ECTS collected: {self.score_ects}", True, self.text_color)
time_text = self.font_label.render(f"Time survived: {self.time_survived}", True, self.text_color)
self.screen.blit(name_text, (280, 290))
self.screen.blit(score_text, (280, 330))
self.screen.blit(time_text, (280, 370))
# draw buttons
restart_pressed = self.button_restart.draw(self.screen)
menu_pressed = self.button_menu.draw(self.screen)
# draw highscore table
self.highscore.draw(self.screen, 800, 300)
# check button click events
if restart_pressed:
return "game"
if menu_pressed:
return "startscreen"
return "endscreen"