Natürlich! Hier ist ein einfaches, aber vollständig funktionierendes Snake-Spiel, programmiert in Python mit der Bibliothek Pygame. Du kannst es direkt ausführen, wenn du Python und Pygame installiert hast.
✅ Voraussetzungen
Installiere Pygame, falls es noch nicht auf deinem System vorhanden ist:
pip install pygame
🐍 Snake-Spiel (Python + Pygame)
Speichere den folgenden Code in einer Datei, z. B. snake_game.py, und führe ihn aus:
import pygame
import time
import random
# Initialisiere Pygame
pygame.init()
# Bildschirmgröße
WIDTH = 600
HEIGHT = 400
# Farben
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
RED = (200, 0, 0)
BLACK = (0, 0, 0)
# Spielgeschwindigkeit
SPEED = 10
# Fenstergröße und Titel
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Spiel")
clock = pygame.time.Clock()
# Snake-Größe und Schrittweite
BLOCK_SIZE = 20
font_style = pygame.font.SysFont("comicsansms", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def score_display(score):
value = score_font.render("Punkte: " + str(score), True, BLACK)
screen.blit(value, [10, 10])
def draw_snake(block_size, snake_list):
for block in snake_list:
pygame.draw.rect(screen, GREEN, [block[0], block[1], block_size, block_size])
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [WIDTH / 6, HEIGHT / 3])
def game_loop():
game_over = False
game_close = False
# Startposition der Schlange
x1 = WIDTH / 2
y1 = HEIGHT / 2
# Bewegungsrichtung
x1_change = 0
y1_change = 0
# Körper der Schlange
snake_list = []
length_of_snake = 1
# Erste Nahrung
foodx = round(random.randrange(0, WIDTH - BLOCK_SIZE) / 20.0) * 20.0
foody = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / 20.0) * 20.0
while not game_over:
while game_close:
screen.fill(WHITE)
message("Verloren! Drücke Q zum Beenden oder C zum Neustarten", RED)
score_display(length_of_snake - 1)
pygame.display.update()
# Beenden oder Neustarten
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
# Tastatur-Eingaben
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and x1_change == 0:
x1_change = -BLOCK_SIZE
y1_change = 0
elif event.key == pygame.K_RIGHT and x1_change == 0:
x1_change = BLOCK_SIZE
y1_change = 0
elif event.key == pygame.K_UP and y1_change == 0:
y1_change = -BLOCK_SIZE
x1_change = 0
elif event.key == pygame.K_DOWN and y1_change == 0:
y1_change = BLOCK_SIZE
x1_change = 0
# Prüfen, ob Schlange gegen Wand läuft
if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_close = True
# Bewegung aktualisieren
x1 += x1_change
y1 += y1_change
screen.fill(WHITE)
# Essen zeichnen
pygame.draw.rect(screen, RED, [foodx, foody, BLOCK_SIZE, BLOCK_SIZE])
# Schlange aktualisieren
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
# Schlange verlängern, wenn Essen gefressen
if len(snake_list) > length_of_snake:
del snake_list[0]
# Selbstkollision prüfen
for block in snake_list[:-1]:
if block == snake_head:
game_close = True
draw_snake(BLOCK_SIZE, snake_list)
score_display(length_of_snake - 1)
pygame.display.update()
# Essen gefressen?
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, WIDTH - BLOCK_SIZE) / 20.0) * 20.0
foody = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / 20.0) * 20.0
length_of_snake += 1
clock.tick(SPEED)
pygame.quit()
quit()
# Spiel starten
game_loop()
🕹️ So spielst du:
- Verwende die Pfeiltasten ↑ ↓ ← →, um die Schlange zu steuern.
- Ziel ist es, möglichst viele rote Äpfel zu fressen.
- Wenn du gegen die Wand oder dich selbst läufst, ist das Spiel vorbei.
- Nach dem Game Over kannst du mit C neu starten oder mit Q beenden.
🎮 Tipps zur Verbesserung (optional):
- Soundeffekte hinzufügen
- Hintergrundbild verwenden
- Schwierigkeitsgrad erhöhen (mehr Essen, schnellere Schlange)
- Highscore speichern
Wenn du möchtest, kann ich dir auch helfen, das Spiel zu erweitern oder in eine Webseite einzubinden (z. B. mit JavaScript/WebAssembly). Sag einfach Bescheid! 🐍💚