#importing
import turtle
import random
import time
def snake_game():
#making head global so it can be used in other functions
global head
#scores
score = 0
high_score = 0
#setting up screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor('yellow')
wn.setup(width=600, height=600)
wn.tracer(0)
# making snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
#snake food
food= turtle.Turtle()
food.speed(0)
food.shape("square")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#scoreboards
sc = turtle.Turtle()
sc.speed(0)
sc.shape("square")
sc.color("black")
sc.penup()
sc.hideturtle()
sc.goto(0,260)
sc.write("score: 0 High score: 0", align = "center", font=("ds-digital", 24, "normal"))
#keyboard bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
#MainLoop
while True:
wn.update()
#check collision with border area
if head.xcor()>290 or head.xcor()290 or head.ycor()0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
for segment in segments:
if segment.distance(head)
Подробнее здесь: [url]https://stackoverflow.com/questions/68743762/i-was-using-turtle-for-making-snake-game-but-facing-problem-while-making-functio[/url]
#MainLoop while True: wn.update() #check collision with border area if head.xcor()>290 or head.xcor()290 or head.ycor()0: x = head.xcor() y = head.ycor() segments[0].goto(x,y)
move() for segment in segments: if segment.distance(head)