feat(text_input): add text input on start screen and temporary test
This commit is contained in:
+80
-1
@@ -1,5 +1,6 @@
|
||||
import pygame
|
||||
from button import Button
|
||||
from text_input import TextInput
|
||||
|
||||
class Startscreen:
|
||||
def __init__(self, screen):
|
||||
@@ -10,6 +11,8 @@ class Startscreen:
|
||||
self.button_color = (0, 255, 0)
|
||||
self.button_hover_color = (200, 255, 0)
|
||||
|
||||
self.player_name = ""
|
||||
|
||||
self.button_start = Button("[ENTER] Immatrikulieren",
|
||||
500,
|
||||
70,
|
||||
@@ -26,7 +29,34 @@ class Startscreen:
|
||||
self.button_color,
|
||||
self.button_hover_color)
|
||||
|
||||
self.text_input_name = TextInput(400,
|
||||
70,
|
||||
20,
|
||||
240)
|
||||
|
||||
def run(self):
|
||||
# get all events
|
||||
events = pygame.event.get()
|
||||
|
||||
# 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"
|
||||
|
||||
# set background color
|
||||
self.screen.fill((0, 0, 0))
|
||||
|
||||
@@ -39,6 +69,9 @@ class Startscreen:
|
||||
button_start_pressed = self.button_start.draw(self.screen)
|
||||
button_quit_pressed = self.button_quit.draw(self.screen)
|
||||
|
||||
# draw text input box
|
||||
self.text_input_name.draw(self.screen)
|
||||
|
||||
# react on button clicks
|
||||
if button_start_pressed:
|
||||
return "game"
|
||||
@@ -56,4 +89,50 @@ class Startscreen:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
return "quit"
|
||||
|
||||
return "start"
|
||||
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()
|
||||
Reference in New Issue
Block a user