So again and better:
from turtle import Turtle, Screen import time import random score = 0 highest_score = 0 # Vytvoří screen screen = Screen() screen.bgcolor("black") screen.title("Vítejte ve Snake Game!") screen.setup(width=600, height=600) screen.tracer(False) # Hadí hlava head = Turtle("square") head.color("green") head.speed(0) head.penup() head.goto(0, 0) head.direction = "stop" # Krmení pro hada apple = Turtle("circle") apple.color("red") apple.penup() x = random.randint(-280, 280) y = random.randint(-280, 280) apple.goto(x, y) # Lišta se skóre score_sign = Turtle() score_sign.color("light green") score_sign.speed() score_sign.penup() score_sign.hideturtle() score_sign.goto(0, 265) score_sign.write("Skóre: 0 Nejvyžší skóre: 0", align="center", font=("Arial", 18)) # Tělo hada body_parts = [] # Funkce def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) def move_up(): if head.direction != "down": head.direction = "up" def move_down(): if head.direction != "up": head.direction = "down" def move_right(): if head.direction != "left": head.direction = "right" def move_left(): if head.direction != "right": head.direction = "left" # Kliknutí na klávesy screen.listen() screen.onkeypress(move_up, "w") screen.onkeypress(move_down, "s") screen.onkeypress(move_right, "d") screen.listen() screen.onkeypress(move_left, "a") # Hlavní cyklus while True: screen.update() # kontrola kolize s plátnem if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(2) head.goto(0, 0) head.direction = "Stop" for one_body_part in body_parts: one_body_part.goto(1000, 1000) # vyčistí list s tělem hada body_parts.clear() score = 0 score_sign.clear() score_sign.write(f"Skóre: {score} Nejvyžší skóre: {highest_score}", align="center", font=("Arial", 18)) # Sežrání krmení if head.distance(apple) < 20: x = random.randint(-280, 280) y = random.randint(-280, 280) apple.goto(x, y) # Přidání části těla new_body_part = Turtle("square") new_body_part.speed(0) new_body_part.color("light green") new_body_part.penup() body_parts.append(new_body_part) # Zvýšení skóre score += 10 if highest_score < score: highest_score = score score_sign.clear() score_sign.write(f"Skóre: {score} Nejvyžší skóre: {highest_score}", align="center", font=("Arial", 18)) # Skládání částí těla za sebe for index in range(len(body_parts) -1, 0, -1): x = body_parts[index - 1].xcor() y = body_parts[index - 1].ycor() body_parts[index].goto(x, y) # Zařazení 1. části těla za hlavu if len(body_parts) > 0: x = head.xcor() y = head.ycor() body_parts[0].goto(x, y) move() # Kontrola kolize hlavy s tělem hada for one_body_part in body_parts: if one_body_part.distance(head) < 20: time.sleep(2) head.goto(0, 0) head.direction = "stop" for one_body_part in body_parts: one_body_part.goto(1000, 1000) # vyčistí list s tělem hada body_parts.clear() score = 0 score_sign.clear() score_sign.write(f"Skóre: {score} Nejvyžší skóre: {highest_score}", align="center", font=("Arial", 18)) time.sleep(0.1)