Python Pygame - start button

Опубликовано: 19 Октябрь 2024
на канале: ANDREEs
123
3

Hier der code zum Mitmachen:
import pygame
pygame.init()

Fenstergröße festlegen
window_width = 800
window_height = 600
window_size = (window_width, window_height)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Mein Spiel")

Schriftart und Textgröße festlegen
font = pygame.font.SysFont("Impact", 70)


Funktion zum Zeichnen des Start-Buttons
def draw_start_button():
pygame.draw.rect(window, (0, 100, 100), (window_width // 2 - 100, window_height // 2 - 50, 200, 100),
border_radius=15, width=10)
start_text = font.render("play", True, "white")
text_rect = start_text.get_rect(center=(window_width // 2, window_height // 2))
window.blit(start_text, text_rect)


Spiel-Loop
running = True
game_started = False

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

if event.type == pygame.MOUSEBUTTONDOWN and not game_started:
mouse_pos = pygame.mouse.get_pos()
button_rect = pygame.Rect(window_width // 2 - 100, window_height // 2 - 50, 200, 100)
if button_rect.collidepoint(mouse_pos):
game_started = True

window.fill("orange")

if not game_started:
window.fill("black")
draw_start_button()

pygame.display.flip()

Pygame beenden
pygame.quit()