Files
silent-fh/src/startscreen.py
T

156 lines
5.1 KiB
Python

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()