Hier ist ein einfaches, funktionierendes Snake-Spiel in Python mit der pygame-Bibliothek. Wenn du pygame noch nicht installiert hast, kannst du es mit folgendem Befehl installieren:
pip install pygame
Speichere den folgenden Code in einer Datei, z.B. snake_game.py, und führe sie aus:
import pygame
import time
import random
# Initialisierung von pygame
pygame.init()
# Farben definieren
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
GREEN = (0, 255, 0)
BLUE = (50, 153, 213)
# Bildschirmgröße
WIDTH = 600
HEIGHT = 400
# Blockgröße und Geschwindigkeit
BLOCK_SIZE = 10
SPEED = 15
# Schriftart
FONT_STYLE = pygame.font.SysFont("bahnschrift", 25)
SCORE_FONT = pygame.font.SysFont("comicsansms", 35)
# Richtung
UP = 'UP'
DOWN = 'DOWN'
LEFT = 'LEFT'
RIGHT = 'RIGHT'
# Spielinitialisierung
def game_init():
display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
return display, clock
# Snake initialisieren
def init_snake():
x = WIDTH // 2
y = HEIGHT // 2
snake_pos = [[x, y], [x - BLOCK_SIZE, y], [x - (2 * BLOCK_SIZE), y]]
direction = RIGHT
change_to = direction
return snake_pos, direction, change_to
# Nahrung generieren
def generate_food(snake_pos):
while True:
food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
if [food_x, food_y] not in snake_pos:
return [food_x, food_y]
# Spielende anzeigen
def show_game_over(display, score):
game_over_text = FONT_STYLE.render("Game Over! Press Q-Quit or C-Play Again", True, RED)
display.blit(game_over_text, [WIDTH / 6, HEIGHT / 3])
score_text = SCORE_FONT.render(f"Score: {score}", True, BLUE)
display.blit(score_text, [WIDTH / 5, HEIGHT / 2])
# Spielschleife
def game_loop():
display, clock = game_init()
snake_pos, direction, change_to = init_snake()
food_pos = generate_food(snake_pos)
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != DOWN:
change_to = UP
if event.key == pygame.K_DOWN and direction != UP:
change_to = DOWN
if event.key == pygame.K_LEFT and direction != RIGHT:
change_to = LEFT
if event.key == pygame.K_RIGHT and direction != LEFT:
change_to = RIGHT
if event.key == pygame.K_q:
pygame.quit()
quit()
if event.key == pygame.K_c:
return game_loop()
direction = change_to
# Snake bewegen
if direction == UP:
new_head = [snake_pos[0][0], snake_pos[0][1] - BLOCK_SIZE]
if direction == DOWN:
new_head = [snake_pos[0][0], snake_pos[0][1] + BLOCK_SIZE]
if direction == LEFT:
new_head = [snake_pos[0][0] - BLOCK_SIZE, snake_pos[0][1]]
if direction == RIGHT:
new_head = [snake_pos[0][0] + BLOCK_SIZE, snake_pos[0][1]]
# Kollisionen prüfen
if (new_head[0] >= WIDTH or new_head[0] < 0 or
new_head[1] >= HEIGHT or new_head[1] < 0 or
new_head in snake_pos[1:]):
display.fill(BLACK)
show_game_over(display, score)
pygame.display.update()
time.sleep(2)
return game_loop()
snake_pos.insert(0, new_head)
# Nahrung fressen
if snake_pos[0] == food_pos:
score += 1
food_pos = generate_food(snake_pos)
else:
snake_pos.pop()
# Bildschirm aktualisieren
display.fill(BLACK)
pygame.draw.rect(display, GREEN, [food_pos[0], food_pos[1], BLOCK_SIZE, BLOCK_SIZE])
for pos in snake_pos:
pygame.draw.rect(display, WHITE, [pos[0], pos[1], BLOCK_SIZE, BLOCK_SIZE])
# Score anzeigen
score_text = SCORE_FONT.render(f"Score: {score}", True, BLUE)
display.blit(score_text, [10, 10])
pygame.display.update()
clock.tick(SPEED)
# Spiel starten
if __name__ == "__main__":
game_loop()
Steuerung:
- Pfeiltasten: Steuerung der Schlange
- Q: Spiel beenden
- C: Spiel neu starten
Viel Spaß beim Spielen! 🐍