Змейка на Python не хочет сниматься с паузы

Я хочу сделать возможность ставить игру на паузу клавишей p, игра ставится, но не снимается, а вылетает.

import turtle
import time
import random

# Set up the screen
win = turtle.Screen()
win.title("Змейка")
win.bgcolor("black")
win.setup(width=600, height=680)
win.tracer(0)
win.cv._rootwindow.resizable(False, False)

# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "stop"

# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
x = random.randint(-14, 14)
y = random.randint(-14, 14)
x = x * 20
y = y * 20
food.goto(x, y)

# Score
score = 0
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.hideturtle()
score_pen.goto(190, 299)  # Позиция счета поверх змейки и яблок
score_pen.write("Счет: 0", align="center", font=("Courier", 24, "normal"))

hscore = 0
hscore_pen = turtle.Turtle()
hscore_pen.speed(0)
hscore_pen.color("white")
hscore_pen.penup()
hscore_pen.hideturtle()
hscore_pen.goto(-160, 299)  # Позиция счета поверх змейки и яблок
hscore_pen.write("Максимум: 0", align="center", font=("Courier", 24, "normal"))

s_pen = turtle.Turtle()
s_pen.speed(0)
s_pen.color("white")
s_pen.penup()
s_pen.hideturtle()
s_pen.goto(0, -335)  # Позиция счета поверх змейки и яблок
s_pen.write("Змейка на Python", align="center", font=("Courier", 21, "normal"))

# Segments of the snake
segments = []

# Functions
def go_up():
    if head.direction != "down":
        head.direction = "up"

def go_down():
    if head.direction != "up":
        head.direction = "down"

def go_left():
    if head.direction != "right":
        head.direction = "left"

def go_right():
    if head.direction != "left":
        head.direction = "right"

def toggle_pause():
    global paused
    if paused == False:
        paused = True  # Если игра была на паузе, снимаем паузу и возобновляем движение
    else:
        paused = False
def move():
    global paused
    if not paused:  # Проверяем, не находится ли игра на паузе
        if head.direction == "up":
            y = head.ycor()
            head.sety(y + 20)
        elif head.direction == "down":
            y = head.ycor()
            head.sety(y - 20)
        elif head.direction == "left":
            x = head.xcor()
            head.setx(x - 20)
        elif head.direction == "right":
            x = head.xcor()
            head.setx(x + 20)

def dash():
    for i in range(3):
        move()
        time.sleep(0.05)  # Интервал для более плавного рывка
def add_segment():
    new_segment = turtle.Turtle()
    new_segment.speed(0)
    new_segment.shape("square")
    new_segment.color("white")
    new_segment.penup()
    segments.append(new_segment)

def move_segments():
    for index in range(len(segments) - 1, 0, -1):
        x = segments[index - 1].xcor()
        y = segments[index - 1].ycor()
        segments[index].goto(x, y)
    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)

def check_collision():
    global score
    global hscore
    for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"
            # Clear the segments
            for segment in segments:
                segment.goto(1000, 1000)
            segments.clear()
            if hscore < score:
                hscore = score
                hscore_pen.clear()
                hscore_pen.write(f"Максимум:{hscore}", align="center", font=("Courier", 24, "normal"))
            # Reset the score
            score = 0
            score_pen.clear()
            score_pen.write("Счет: 0", align="center", font=("Courier", 24, "normal"))
            return True
    return False

# Клавиши
win.listen()
win.onkeypress(go_up, "w")
win.onkeypress(go_down, "s")
win.onkeypress(go_left, "a")
win.onkeypress(go_right, "d")
win.onkeypress(dash, "q")

# Границы
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("red")
border_pen.penup()
border_pen.goto(-300, -300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
border_pen.hideturtle()

# Main game loop
paused = False
while True:
    if paused == False:  # Проверяем, не находится ли игра на паузе
        win.update()

        # Check for collision with food
        if head.distance(food) < 20:
            # Move the food to a random spot
            x = random.randint(-14, 14)
            y = random.randint(-14, 14)
            x = x * 20
            y = y * 20
            food.goto(x, y)
            # Увеличить счет
            score += 1
            score_pen.clear()  # Очищаем предыдущий текст
            score_pen.write(f"Счет: {score}", align="center", font=("Courier", 24, "normal"))
            # Add a segment
            add_segment()
        # Check for collision with border
        if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"
            x = random.randint(-14, 14)
            y = random.randint(-14, 14)
            x = x * 20
            y = y * 20
            food.goto(x, y)
            if hscore < score:
                hscore = score
                hscore_pen.clear()
                hscore_pen.write(f"Максимум:{hscore}", align="center", font=("Courier", 24, "normal"))
            # Сброс счета
            score = 0
            score_pen.clear()
            score_pen.write(f"Счет:{score}", align="center", font=("Courier", 24, "normal"))
            # Clear the segments
            for segment in segments:
                segment.goto(1000, 1000)
            segments.clear()

        # Check for collision with self
        if check_collision():
            continue  # Skip to the next iteration of the loop

        move_segments()
        move()
        time.sleep(0.1)

    # Клавиша для паузы
    def toggle_pause():
        global paused
        paused = not paused

    win.onkeypress(toggle_pause, "p")

Ответы (0 шт):