Kurs April 2026

Allgemein

Catch the Stars ⭐️

Pygame Zero Cheatsheet

Grundstruktur

WIDTH = 800
HEIGHT = 600
TITLE = "Mein Spiel"

def draw():
    screen.fill((0, 0, 0))
    # alles zeichnen

def update():
    # Logik pro Frame

Actor (Sprites)

spieler = Actor("spieler")
spieler.x = 400
spieler.y = 300
spieler.pos = (400, 300)

screen.draw.filled_rect(spieler, "red")
spieler.draw()

# Kollision prüfen
if spieler.colliderect(gegner):
    ...

Zeichnen (screen)

screen.fill((r, g, b))
screen.blit("bild", (x, y))
screen.draw.text("Hallo!", (x, y),
    color="white", fontsize=30)
screen.draw.rect(
    Rect((x,y),(b,h)), "red")
screen.draw.filled_rect(
    Rect((x,y),(b,h)), "blue")
screen.draw.circle((x,y), r, "green")
screen.draw.line((x1,y1),(x2,y2),"white")

Tastatur & Maus

# in update():
if keyboard.left:
    spieler.x -= 5
if keyboard.right:
    spieler.x += 5
if keyboard.up:
    spieler.y -= 5
if keyboard.down:
    spieler.y += 5
if keyboard.space:
    schiessen()

# Ereignis-Funktionen
def on_key_down(key):
    if key == keys.SPACE:
        ...

def on_mouse_move(pos):
    spieler.x = pos[0]

def on_mouse_down(pos, button):
    ...

Töne & Musik

# Datei in sounds/ ablegen
sounds.explosion.play()
sounds.laser.play()

# Datei in music/ ablegen
music.play("hintergrund")
music.play_once("jingle")
music.stop()
music.set_volume(0.5)  # 0.0 – 1.0

Clock / Timer

# Funktion einmalig nach 2 Sek.
clock.schedule(meine_funktion, 2.0)

# Funktion alle 1.5 Sek. wiederholen
clock.schedule_interval(
    meine_funktion, 1.5)

# Timer stoppen
clock.unschedule(meine_funktion)

Nützliche Werte & Tricks

import random

# Zufallszahl zwischen 0 und 800
x = random.randint(0, 800)

# Bildschirmgröße
WIDTH, HEIGHT

# Actor-Positionen
actor.topleft  actor.topright
actor.bottomleft  actor.bottomright
actor.center   actor.midtop

# Rect erstellen
Rect(x, y, breite, hoehe)

Dateistruktur

mein_spiel/
├── mein_spiel.py   ← Hauptdatei
├── images/
│   ├── spieler.png
│   └── gegner.png
├── sounds/
│   ├── laser.wav
│   └── explosion.wav
└── music/
    └── hintergrund.mp3
# Zufallsgenerator import random # Fenster WIDTH = 1000 HEIGHT = 500 # Hintergrund bg = Actor("background") bg.x = 500 bg.y = 250 # Korb (Spielfigur) basket = Actor("basket") basket.x = 500 basket.y = 460 basket_speed = 5 # Stern star = Actor("star") star.x = 300 star.y = -20 star_speed = 3 # Herzen / Leben hearts = [ Actor("heart", (30, 60)), Actor("heart", (90, 60)), Actor("heart", (150, 60)), ] # Spielstand score = 0 lives = 3 game_over = False # Zeichner def draw(): bg.draw() basket.draw() star.draw() #for heart in hearts: #heart.draw() # Score anzeigen screen.draw.text( "Score: " + str(score), (10, 10), color = "white", fontsize = 40 ) # Leben anzeigen screen.draw.text( "Leben: " + str(lives), (10, 40), color = "red", fontsize = 40 ) # Game Over Bildschirm anzeigen if game_over: screen.draw.filled_rect( Rect(200, 150, 600, 200), "black" ) # Regisseur def update(): global score, lives, game_over # Spiel beenden, wenn game over if game_over: return # Tastaturbefehle if keyboard.left: basket.x -= basket_speed if keyboard.right: basket.x += basket_speed # Korb geht nicht über Rand hinaus if basket.x < 30: basket.x = 30 if basket.x > 970: basket.x = 970 # Stern fällt nach unten star.y += star_speed if star.y > 550: star.y = -20 star.x = random.randint(30, 970) lives -= 1 if lives <= 0: game_over = True print(game_over) hearts.pop(lives) # Stern trifft Korb if star.colliderect(basket): score += 1 print(score) star.y = -20 star.x = random.randint(30, 970)