258 lines
7.0 KiB
Python
Executable File
258 lines
7.0 KiB
Python
Executable File
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 = 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)
|
|
|
|
p_size = 100
|
|
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
|
|
|
|
def show_score(score_p1, score_p2):
|
|
score_text = font.render(
|
|
f"Punkte P1: {score_p1} Punkte P2: {score_p2}",
|
|
True,
|
|
black
|
|
)
|
|
screen.blit(score_text, (10, 10))
|
|
|
|
def show_timer():
|
|
timer_text = font.render(f"Zeit: {int(timer_sec)}", True, black)
|
|
screen.blit(timer_text, (10, 40))
|
|
|
|
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)
|
|
|
|
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
|
|
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]:
|
|
p1.x += speed
|
|
if keys[K_DOWN]:
|
|
p1.y += speed
|
|
if keys[K_UP]:
|
|
p1.y -= speed
|
|
|
|
if keys[K_a]:
|
|
p2.x -= speed
|
|
if keys[K_d]:
|
|
p2.x += speed
|
|
if keys[K_s]:
|
|
p2.y += speed
|
|
if keys[K_w]:
|
|
p2.y -= speed
|
|
|
|
p1.clamp_ip(border)
|
|
p2.clamp_ip(border)
|
|
|
|
if p1.colliderect(p2):
|
|
p1.topleft = (
|
|
random.randint(0, width - p_size),
|
|
random.randint(0, height - p_size)
|
|
)
|
|
p2.topleft = (
|
|
random.randint(0, width - p_size),
|
|
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)
|
|
c = Rect(c_x, c_y, c_d, c_d)
|
|
points_p1 += 1
|
|
|
|
if p2.colliderect(c):
|
|
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)
|
|
points_p2 += 1
|
|
|
|
timer_sec -= clock.get_time() / 1000
|
|
if timer_sec <= 0:
|
|
# 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:
|
|
show_timer()
|
|
|
|
display.flip()
|
|
clock.tick(60)
|
|
|
|
def main():
|
|
global running, game_started, in_game
|
|
|
|
running = True
|
|
|
|
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()
|
|
|
|
if __name__ == "__main__":
|
|
main() |