py

Breakout Demo

Keine Vorschau vorhanden

Code-Snapshot

Dies ist der veröffentlichte Zustand. Spätere Änderungen am Code überschreiben diese Veröffentlichung nicht automatisch.

import pygame, time

# --- Konstanten ---
BREITE = 320
HOH = 200
WEISS = (255, 255, 255)
SCHWARZ = (0, 0, 0)
GRAU = (150, 150, 150)

# Paddle-Konstanten
PADDLE_BREITE = 40
PADDLE_HOH = 5
PADDLE_Y = HOH - PADDLE_HOH - 5

# Ball-Konstanten
BALL_GROESSE = 3
BALL_START_GESCHWINDIGKEIT = 3

# Block-Konstanten 
BLOCK_BREITE = 30
BLOCK_HOH = 10
ABSTAND = 2
ANZ_REIHEN = 5

pygame.init()
fenster = pygame.display.set_mode((BREITE, HOH))
pygame.display.set_caption("Breakout")
font = pygame.font.SysFont(None, 24)

clock = pygame.time.Clock()

# --- Hilfsfunktionen ---
def generate_blocks():
    bloecke = []
    start_y = 10
    max_bloecke_pro_reihe = (BREITE - ABSTAND) / (BLOCK_BREITE + ABSTAND)
    for reihe in range(ANZ_REIHEN):
        for spalte in range(int(max_bloecke_pro_reihe)):
            x = ABSTAND + spalte * (BLOCK_BREITE + ABSTAND)
            y = start_y + reihe * (BLOCK_HOH + ABSTAND)
            bloecke.append(pygame.Rect(x, y, BLOCK_BREITE, BLOCK_HOH))
    return bloecke

def draw_text(text, y, color=WEISS):
    surface = font.render(text, True, color)
    rect = surface.get_rect()
    rect.center = (BREITE//2, y)
    fenster.blit(surface, rect)

# --- Spielvariablen ---
speed = BALL_START_GESCHWINDIGKEIT
state = "start"   # start, playing, gameover, win
start_time = 0

# --- Hauptloop ---
running = True
while running:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
        elif e.type == pygame.KEYDOWN:
            if state == "start":
                if e.key == pygame.K_SPACE:
                    state = "playing"
                    start_time = time.time()
                    bloecke = generate_blocks()
                    ball_x, ball_y = BREITE/2, PADDLE_Y - BALL_GROESSE - 10
                    ball_dx, ball_dy = speed, -speed
                    paddle_x = BREITE/2 - PADDLE_BREITE/2
                elif e.key == pygame.K_UP:
                    speed += 1
                elif e.key == pygame.K_DOWN and speed > 1:
                    speed -= 1
            elif state in ("gameover","win"):
                if e.key == pygame.K_SPACE:
                    state = "start"
        elif e.type == pygame.MOUSEBUTTONDOWN and e.button == 1:
            # linker Mausklick = wie Space
            if state == "start":
                state = "playing"
                start_time = time.time()
                bloecke = generate_blocks()
                ball_x, ball_y = BREITE/2, PADDLE_Y - BALL_GROESSE - 10
                ball_dx, ball_dy = speed, -speed
                paddle_x = BREITE/2 - PADDLE_BREITE/2
            elif state in ("gameover","win"):
                state = "start"

    fenster.fill(SCHWARZ)

    if state == "start":
        draw_text("Breakout", 60)
        draw_text("SPACE / Klick = Start", 100)
        draw_text("Pfeil hoch/runter = Speed " + str(speed), 140)

    elif state == "playing":
        # Eingabe Paddle
        maus_x, _ = pygame.mouse.get_pos()
        paddle_x = maus_x - PADDLE_BREITE/2
        paddle_x = max(0, min(BREITE-PADDLE_BREITE, paddle_x))

        # Ball bewegen
        ball_x += ball_dx
        ball_y += ball_dy
        ball_rect = pygame.Rect(ball_x-BALL_GROESSE, ball_y-BALL_GROESSE, BALL_GROESSE*2, BALL_GROESSE*2)

        # Wände
        if ball_x - BALL_GROESSE <= 0 or ball_x + BALL_GROESSE >= BREITE:
            ball_dx *= -1
        if ball_y - BALL_GROESSE <= 0:
            ball_dy *= -1

        # Paddle
        paddle_rect = pygame.Rect(paddle_x, PADDLE_Y, PADDLE_BREITE, PADDLE_HOH)
        if ball_rect.colliderect(paddle_rect) and ball_dy > 0:
            ball_dy *= -1

        # Blöcke
        for i in range(len(bloecke)-1, -1, -1):
            if ball_rect.colliderect(bloecke[i]):
                ball_dy *= -1
                bloecke.pop(i)
                break

        # Game Over / Win
        if ball_y + BALL_GROESSE > HOH:
            state = "gameover"
        elif len(bloecke) == 0:
            state = "win"
            end_time = time.time()

        # Zeichnen
        for block in bloecke:
            pygame.draw.rect(fenster, GRAU, block)
        pygame.draw.rect(fenster, WEISS, paddle_rect)
        pygame.draw.circle(fenster, WEISS, (int(ball_x), int(ball_y)), BALL_GROESSE)

    elif state == "gameover":
        draw_text("Game Over", 80)
        draw_text("SPACE / Klick = Neustart", 120)

    elif state == "win":
        draw_text("Gewonnen!", 80)
        spielzeit = round(end_time - start_time, 2)
        draw_text(f"Spielzeit: {spielzeit} Sekunden", 120)
        draw_text("SPACE / Klick = Neustart", 160)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()