Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3dd3bc5a6e | ||
|
|
45bd5ae2c5 |
@@ -1,22 +1,27 @@
|
||||
from pygame import *
|
||||
import random
|
||||
from pygame.font import SysFont
|
||||
|
||||
init()
|
||||
|
||||
running = False
|
||||
game_started = False
|
||||
in_game = False # Neue Variable: Sind wir im Spiel oder im Menü?
|
||||
|
||||
width, height = 1920, 1000
|
||||
screen = display.set_mode((width, height))
|
||||
display.set_caption("Game")
|
||||
|
||||
clock = time.Clock()
|
||||
font = font.SysFont(None, 40)
|
||||
font = SysFont(None, 40)
|
||||
title_font = SysFont(None, 80)
|
||||
|
||||
white = (255,255,255)
|
||||
blue = (0,0,255)
|
||||
red = (255,0,0)
|
||||
green = (0,255,0)
|
||||
black = (0, 0, 0)
|
||||
gray = (128, 128, 128)
|
||||
|
||||
border = Rect(0, 0 , width, height)
|
||||
|
||||
@@ -26,19 +31,15 @@ speed = 10
|
||||
points_p1 = 0
|
||||
points_p2 = 0
|
||||
|
||||
|
||||
c_r = 25
|
||||
c_d = c_r * 2
|
||||
c_x = random.randint(0, width - c_d)
|
||||
c_y = random.randint(0, height - c_d)
|
||||
|
||||
c = Rect(c_x, c_y, c_d, c_d)
|
||||
|
||||
|
||||
p1 = Rect(100, 200, p_size, p_size)
|
||||
p2 = Rect(600, 250, p_size, p_size)
|
||||
|
||||
|
||||
timer_sec = 120
|
||||
timer_bool = True
|
||||
|
||||
@@ -54,18 +55,124 @@ def show_timer():
|
||||
timer_text = font.render(f"Zeit: {int(timer_sec)}", True, black)
|
||||
screen.blit(timer_text, (10, 40))
|
||||
|
||||
def game():
|
||||
global running, points_p1, points_p2, c_x, c_y, c, timer_sec
|
||||
def reset_game():
|
||||
"""Setzt alle Spielvariablen zurück"""
|
||||
global points_p1, points_p2, timer_sec, c_x, c_y, c, p1, p2
|
||||
|
||||
points_p1 = 0
|
||||
points_p2 = 0
|
||||
timer_sec = 120
|
||||
|
||||
c_x = random.randint(0, width - c_d)
|
||||
c_y = random.randint(0, height - c_d)
|
||||
c = Rect(c_x, c_y, c_d, c_d)
|
||||
|
||||
p1.topleft = (100, 200)
|
||||
p2.topleft = (600, 250)
|
||||
|
||||
while running:
|
||||
def start_screen():
|
||||
global game_started, in_game
|
||||
|
||||
start_button = Rect(width // 2 - 100, height // 2 + 50, 200, 50)
|
||||
quit_button = Rect(width // 2 - 100, height // 2 + 120, 200, 50)
|
||||
|
||||
while not game_started:
|
||||
for e in event.get():
|
||||
if e.type == QUIT:
|
||||
return False
|
||||
if e.type == MOUSEBUTTONDOWN:
|
||||
mouse_pos = e.pos
|
||||
if start_button.collidepoint(mouse_pos):
|
||||
reset_game() # Setze Spiel zurück bevor es startet
|
||||
in_game = True
|
||||
return True
|
||||
if quit_button.collidepoint(mouse_pos):
|
||||
return False
|
||||
|
||||
# Zeichne Startbildschirm
|
||||
screen.fill(blue)
|
||||
|
||||
# Titel
|
||||
title_text = title_font.render("CATCH THE SQUARE", True, white)
|
||||
title_rect = title_text.get_rect(center=(width // 2, height // 3))
|
||||
screen.blit(title_text, title_rect)
|
||||
|
||||
# Spieler-Steuerung anzeigen
|
||||
controls_text = font.render("Steuerung:", True, white)
|
||||
screen.blit(controls_text, (width // 2 - 150, height // 2 - 100))
|
||||
|
||||
p1_controls = font.render("Spieler 1: Pfeiltasten", True, white)
|
||||
screen.blit(p1_controls, (width // 2 - 150, height // 2 - 60))
|
||||
|
||||
p2_controls = font.render("Spieler 2: W A S D", True, white)
|
||||
screen.blit(p2_controls, (width // 2 - 150, height // 2 - 20))
|
||||
|
||||
# Buttons
|
||||
draw.rect(screen, green, start_button)
|
||||
draw.rect(screen, red, quit_button)
|
||||
|
||||
start_text = font.render("START", True, white)
|
||||
start_text_rect = start_text.get_rect(center=start_button.center)
|
||||
screen.blit(start_text, start_text_rect)
|
||||
|
||||
quit_text = font.render("QUIT", True, white)
|
||||
quit_text_rect = quit_text.get_rect(center=quit_button.center)
|
||||
screen.blit(quit_text, quit_text_rect)
|
||||
|
||||
display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
def show_game_over():
|
||||
"""Zeigt das Game Over an und kehrt nach 5 Sekunden zum Menü zurück"""
|
||||
global game_started, in_game
|
||||
|
||||
screen.fill(white)
|
||||
|
||||
# Ergebnis anzeigen
|
||||
end_text = font.render(
|
||||
f"Ende! P1: {points_p1} | P2: {points_p2}",
|
||||
True,
|
||||
black
|
||||
)
|
||||
screen.blit(end_text, (width // 2 - 200, height // 2 - 50))
|
||||
|
||||
# Wer hat gewonnen?
|
||||
if points_p1 > points_p2:
|
||||
winner_text = font.render("Spieler 1 gewinnt!", True, blue)
|
||||
elif points_p2 > points_p1:
|
||||
winner_text = font.render("Spieler 2 gewinnt!", True, green)
|
||||
else:
|
||||
winner_text = font.render("Unentschieden!", True, black)
|
||||
|
||||
screen.blit(winner_text, (width // 2 - 150, height // 2))
|
||||
|
||||
# Zurück zum Menü Text
|
||||
back_text = font.render("Zurück zum Hauptmenü...", True, gray)
|
||||
screen.blit(back_text, (width // 2 - 150, height // 2 + 100))
|
||||
|
||||
display.flip()
|
||||
time.wait(5000)
|
||||
|
||||
# Kehre zum Startmenü zurück
|
||||
in_game = False
|
||||
game_started = False
|
||||
|
||||
def game():
|
||||
global running, points_p1, points_p2, c_x, c_y, c, timer_sec, in_game
|
||||
|
||||
while running and in_game:
|
||||
for e in event.get():
|
||||
if e.type == QUIT:
|
||||
running = False
|
||||
print("Programm closed by user input")
|
||||
return
|
||||
# Optional: ESC-Taste zum Zurückkehren zum Menü
|
||||
if e.type == KEYDOWN:
|
||||
if e.key == K_ESCAPE:
|
||||
in_game = False
|
||||
return
|
||||
|
||||
keys = key.get_pressed()
|
||||
|
||||
|
||||
if keys[K_LEFT]:
|
||||
p1.x -= speed
|
||||
if keys[K_RIGHT]:
|
||||
@@ -75,7 +182,6 @@ def game():
|
||||
if keys[K_UP]:
|
||||
p1.y -= speed
|
||||
|
||||
|
||||
if keys[K_a]:
|
||||
p2.x -= speed
|
||||
if keys[K_d]:
|
||||
@@ -88,7 +194,6 @@ def game():
|
||||
p1.clamp_ip(border)
|
||||
p2.clamp_ip(border)
|
||||
|
||||
|
||||
if p1.colliderect(p2):
|
||||
p1.topleft = (
|
||||
random.randint(0, width - p_size),
|
||||
@@ -99,7 +204,6 @@ def game():
|
||||
random.randint(0, height - p_size)
|
||||
)
|
||||
|
||||
|
||||
if p1.colliderect(c):
|
||||
c_x = random.randint(0, width - c_d)
|
||||
c_y = random.randint(0, height - c_d)
|
||||
@@ -112,19 +216,17 @@ def game():
|
||||
c = Rect(c_x, c_y, c_d, c_d)
|
||||
points_p2 += 1
|
||||
|
||||
|
||||
timer_sec -= clock.get_time() / 1000
|
||||
if timer_sec <= 0:
|
||||
running = False
|
||||
# Zeit abgelaufen - zeige Game Over und kehre zum Menü zurück
|
||||
show_game_over()
|
||||
return
|
||||
|
||||
# Zeichnen
|
||||
screen.fill(white)
|
||||
draw.rect(screen, blue, p1)
|
||||
draw.rect(screen, green, p2)
|
||||
|
||||
|
||||
draw.circle(screen, red, (c_x + c_r, c_y + c_r), c_r)
|
||||
|
||||
show_score(points_p1, points_p2)
|
||||
|
||||
if timer_bool:
|
||||
@@ -133,22 +235,24 @@ def game():
|
||||
display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
screen.fill(white)
|
||||
end_text = font.render(
|
||||
f"Ende! P1: {points_p1} | P2: {points_p2}",
|
||||
True,
|
||||
black
|
||||
)
|
||||
screen.blit(end_text, (width // 2 - 200, height // 2))
|
||||
display.flip()
|
||||
time.wait(7000)
|
||||
|
||||
def start():
|
||||
global running
|
||||
print("Starting Game")
|
||||
def main():
|
||||
global running, game_started, in_game
|
||||
|
||||
running = True
|
||||
game()
|
||||
print("Game ended")
|
||||
|
||||
while running:
|
||||
# Zeige Startbildschirm
|
||||
if not start_screen():
|
||||
break # Spieler hat Quit gewählt
|
||||
|
||||
# Starte das Spiel
|
||||
game()
|
||||
|
||||
# Nach dem Spiel: Wenn running noch True ist, gehe zurück zum Menü
|
||||
if not running:
|
||||
break
|
||||
|
||||
quit()
|
||||
|
||||
start()
|
||||
quit()
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user