Merge branch 'feat/startscreen' into 'main'

feat: implement startscreen

See merge request swd25-bootcamp/silent-fh!6
This commit is contained in:
Haiden Niklas
2026-05-08 12:31:04 +00:00
4 changed files with 237 additions and 3 deletions
+67
View File
@@ -0,0 +1,67 @@
import pygame
class Button:
def __init__(self, text, width, height, pos_x, pos_y, color, hover):
"""
:param text: Text inside the button.
:param width: Width of the button.
:param height: Height of the button.
:param pos_x: X position of the button on the screen.
:param pos_y: Y position of the button on the screen.
:param color: Color of the button on the screen.
:param hover: Color of the button on the screen when mouse is hover.
"""
self.text = text
self.width = width
self.height = height
self.pos_x = pos_x
self.pos_y = pos_y
self.color = color
self.hover = hover
# create rectangle for button
self.rect = pygame.Rect(self.pos_x, self.pos_y, self.width, self.height)
# set font for button text
self.font = pygame.font.SysFont("Arial", 30)
# temp var to stor click event - so it will be executed just once
self.clicked = False
def draw(self, screen):
"""
Draw the button on the screen.
:param screen: Screen object where button will be drawn.
:return: Return True when button was clicked.
"""
action = False
# get current mous position
mouse_pos = pygame.mouse.get_pos()
# check if mouse is over button
if self.rect.collidepoint(mouse_pos):
current_color = self.hover
# check if left mouse button is pressed
if pygame.mouse.get_pressed()[0] == 1:
self.clicked = True
if pygame.mouse.get_pressed()[0] == 0 and self.clicked:
self.clicked = False
action = True
else:
current_color = self.color
self.clicked = False
# draw buttton
pygame.draw.rect(screen, current_color, self.rect, border_radius = 12)
# render button text
text_surface = self.font.render(self.text, True, (255, 255, 255))
# place text in the middle of the button
text_rect = text_surface.get_rect(center=self.rect.center)
screen.blit(text_surface, text_rect)
return action
+13 -2
View File
@@ -10,15 +10,26 @@ DOOR_BORDER_WIDTH = 3
COLOR_DOOR_BEIGE = (245, 245, 220) COLOR_DOOR_BEIGE = (245, 245, 220)
COLOR_DOOR_BORDER = (100, 90, 80) COLOR_DOOR_BORDER = (100, 90, 80)
COLOR_HP_BAR_GREEN = (0, 255, 0)
COLOR_HP_BAR_BG = (50, 50, 50)
# --- ECTS --- # --- ECTS ---
START_ECTS_VALUE = 1 # how many ECTS are generated at the beginning START_ECTS_VALUE = 1 # how many ECTS are generated at the beginning
ECTS_UPDATE_INTERVAL = 1000 # time in ms = 1 sec ECTS_UPDATE_INTERVAL = 1000 # time in ms = 1 sec
BEER_BOTTLE_ECTS_VALUE = 2 BEER_BOTTLE_ECTS_VALUE = 2
BEER_COST = 50 BEER_COST = 50
# --- BUTTONS ---
COLOR_BUTTON = (0, 255, 0)
COLOR_BUTTON_HOVER = (200, 255, 0)
# --- STARTSCREEN ---
COLOR_STARTSCREEN_BACKGROUND = ((0, 0, 0))
COLOR_STARTSCREEN_FONT_TITLE = (0, 255, 0)
COLOR_STARTSCREEN_FONT_LABEL = (0, 255, 255)
# --- COFFEE --- # --- COFFEE ---
COFFEE_COST = 30 COFFEE_COST = 30
+156
View File
@@ -0,0 +1,156 @@
import pygame
from button import Button
from settings import (COLOR_BUTTON,
COLOR_BUTTON_HOVER,
COLOR_STARTSCREEN_BACKGROUND,
COLOR_STARTSCREEN_FONT_TITLE,
COLOR_STARTSCREEN_FONT_LABEL)
from text_input import TextInput
class Startscreen:
def __init__(self, screen):
self.screen = screen
self.font_title = pygame.font.SysFont("Arial", 36)
self.font_label = pygame.font.SysFont("Arial", 20)
self.button_color = COLOR_BUTTON
self.button_hover_color = COLOR_BUTTON_HOVER
self.player_name = ""
self.wrong_name = False
self.button_start = Button("[ENTER] Matriculate",
500,
70,
20,
100,
self.button_color,
self.button_hover_color)
self.button_quit = Button("[ESC] Exmatriculate",
500,
70,
20,
190,
self.button_color,
self.button_hover_color)
self.text_input_name = TextInput(400,
40,
20,
310)
def run(self):
# set background color
self.screen.fill(COLOR_STARTSCREEN_BACKGROUND)
# set title and intro/welcome message
title = self.font_title.render("SILENT FH", True, COLOR_STARTSCREEN_FONT_TITLE)
self.screen.blit(title, (10, 10))
message_1 = self.font_label.render("Welcome to Silent FH", True, COLOR_STARTSCREEN_FONT_LABEL)
message_2 = self.font_label.render("Study with purpose. Survive with Coffee.", True,
COLOR_STARTSCREEN_FONT_LABEL)
self.screen.blit(message_1, (10, 50))
self.screen.blit(message_2, (10, 70))
# draw buttons and get click state
button_start_pressed = self.button_start.draw(self.screen)
button_quit_pressed = self.button_quit.draw(self.screen)
# draw text input box
label_input = self.font_label.render("Enter your student name or ID:", True,
COLOR_STARTSCREEN_FONT_LABEL)
self.screen.blit(label_input, (10, 280))
self.text_input_name.draw(self.screen)
# prepare error message when no input was submitted
label_input_error = self.font_label.render("Student not found. Please enter valid name/id.", True,
(255, 0, 0))
# get all events
events = pygame.event.get()
# react on button clicks
if button_start_pressed:
self.player_name = self.text_input_name.get_text()
if self.player_name.strip() != "":
print("Spielername:", self.player_name)
return "game"
else:
self.wrong_name = True
if button_quit_pressed:
return "quit"
# check events
for event in events:
if event.type == pygame.QUIT:
return "quit"
# forward event to text input box
self.text_input_name.handle_event(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return "quit"
if event.key == pygame.K_RETURN:
self.player_name = self.text_input_name.get_text()
if self.player_name.strip() != "":
print("Spielername:", self.player_name)
return "game"
else:
self.wrong_name = True
if self.wrong_name:
self.screen.blit(label_input_error, (10, 350))
return "startscreen"
# test function fro temporary testing
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((960, 640))
pygame.display.set_caption("Silent FH - Startscreen Test")
clock = pygame.time.Clock()
startscreen = Startscreen(screen)
state = "startscreen"
running = True
while running:
clock.tick(60)
if state == "startscreen":
state = startscreen.run()
elif state == "game":
screen.fill((30, 30, 30))
font = pygame.font.SysFont("Arial", 36)
text = font.render("GAME STARTED", True, (255, 255, 255))
screen.blit(text, (300, 280))
info = font.render("Press ESC to quit", True, (150, 150, 150))
screen.blit(info, (300, 330))
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "quit"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
state = "quit"
elif state == "quit":
running = False
pygame.display.flip()
pygame.quit()