Startseite

import pygame
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306

Konfiguration für das OLED-Display

serial = i2c(port=1, address=0x3C)
device = ssd1306(serial)

Labyrinth-Layout (0 für freie Fläche, 1 für Wand)

labyrinth = [
[0, 0, 1, 0, 0],
[1, 0, 1, 0, 1],
[0, 0, 0, 0, 0],
[1, 0, 1, 0, 1],
[0, 0, 1, 0, 0]
]

Spielerposition

player_pos = [0, 0]

Pygame initialisieren

pygame.init()

Fenstergröße

window_size = (300, 300)
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption(„Labyrinth Game“)

Spielerbild laden

player_image = pygame.image.load(„player.png“)

Hauptspiel-Schleife

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
# Nach links bewegen
if player_pos[0] > 0 and labyrinth[player_pos[1]][player_pos[0] – 1] == 0:
player_pos[0] -= 1
elif event.key == pygame.K_d:
# Nach rechts bewegen
if player_pos[0] < len(labyrinth[0]) – 1 and labyrinth[player_pos[1]][player_pos[0] + 1] == 0:
player_pos[0] += 1

# Display aktualisieren
with canvas(device) as draw:
    for y, row in enumerate(labyrinth):
        for x, cell in enumerate(row):
            if cell == 1:
                # Wand zeichnen
                draw.rectangle((x * 20, y * 20, x * 20 + 19, y * 20 + 19), outline="white", fill="white")
    # Spieler zeichnen
    draw.bitmap((player_pos[0] * 20, player_pos[1] * 20), player_image, fill="white")

pygame.display.flip()

Aufräumen

pygame.quit()