помогите пожалуйста со змейкой на python. не получается сделать рост змейки
В коде ниже реализовано всё, кроме роста змейки. можете написать код, который будет отвечать за рост змейки, пожалуйста. А то я задолбался третий день подряд экспериментировать. Буду очень признателен за помощь
import time
import random
from tkinter import *
import os
eaten = 0
x = random.randint(65, 335)
y = random.randint(65, 435)
r = 10
move = False
foodPos1 = x - r
foodPos2 = x + r
foodPos3 = y - r
foodPos4 = y + r
x1 = 40
x2 = 60
y1 = 40
y2 = 60
moveX1 = 10
moveX2 = 10
moveY1 = 10
moveY2 = 10
root = Tk()
navigation = ''
root.title('Змейка 2024')
root.geometry('345x450')
canvas = Canvas(root, width = 350, height = 450, bg="black")
canvas.pack()
canvas.create_text(70,30,text='съедено: ',font="Verdana 12",fill="white")
canvas.focus_set()
eat = canvas.create_text(115,30,text=eaten,font="Verdana 12",fill="white")
snake = canvas.create_rectangle(x1,y1,x2,y2, fill="green")
snake1 = canvas.create_rectangle(600,y1,600,y2, fill="green")
snake2 = canvas.create_rectangle(600,y1,600,y2, fill="green")
food = canvas.create_oval(foodPos1, foodPos3, foodPos2, foodPos4, fill="red")
BX = random.randint(65, 335)
BY = random.randint(65, 435)
barpos1 = BX - r
barpos2 = BX + r
barpos3 = BY - r
barpos4 = BY + r
barrier = canvas.create_rectangle(barpos1, barpos3, barpos2, barpos4, fill="gray")
bx1 = 0
bx2 = 15
by1 = 0
by2 = 15
minus = 15
canvas.create_rectangle(bx1, by1, bx2, by2, fill="gray")
for i in range(29):
by1 += minus
by2 += minus
canvas.create_rectangle(bx1, by1, bx2, by2, fill="gray")
minus*2
minus = 15
for i in range(22):
bx1 += minus
bx2 += minus
canvas.create_rectangle(bx1, by1, bx2, by2, fill="gray")
minus*2
minus = 15
for i in range(29):
by1 -= minus
by2 -= minus
canvas.create_rectangle(bx1, by1, bx2, by2, fill="gray")
minus*2
minus = 15
for i in range(29):
bx1 -= minus
bx2 -= minus
canvas.create_rectangle(bx1, by1, bx2, by2, fill="gray")
minus*2
death = False
def loop():
global death
death = True
def move_up(event):
global navigation
if navigation != "down":
navigation = "up"
def move_down(event):
global navigation
if navigation != "up":
navigation = "down"
def move_right(event):
global navigation
if navigation != "left":
navigation = "right"
def move_left(event):
global navigation
if navigation != "right":
navigation = "left"
def tail():
global x1
global x2
global y1
global y2
global navigation
global eaten
global snake1
for i in range(eaten):
if navigation == "up":
snake1 = canvas.create_rectangle(x1,y1+21,x2,y2+21, fill="green")
elif navigation == "down":
snake1 = canvas.create_rectangle(x1,y1-21,x2,y2-21, fill="green")
elif navigation == "left":
snake1 = canvas.create_rectangle(x1+21,y1,x2+21,y2, fill="green")
elif navigation == "right":
snake1 = canvas.create_rectangle(x1-21,y1,x2-21,y2, fill="green")
def draw():
global death
global navigation
global moveX1
global moveX2
global moveY1
global moveY2
global x1
global x2
global y1
global y2
global snake
global x
global y
global food
global eaten
global eat
global barrier
global move
global snake1
global snake2
a = 10
collision = canvas.find_overlapping(x1, y1, x2, y2)
if food in collision:
canvas.delete(food)
eaten += 1
canvas.itemconfig(eat, text=eaten)
x = random.randint(50, 300)
y = random.randint(50, 400)
food = canvas.create_oval(x - r, y - r, x + r, y + r, fill="red")
# tail()
if eaten != 0:
move = True
collision1 = canvas.find_overlapping(x1, y1, x2, y2)
if barrier in collision1 or snake1 in collision1 or x1 == 10 or x2 == 340 or y2 == 440 or y1 == 10:
f = open("ваши рекорды.txt", "a")
f.write(str(eaten) + '\n')
f.close()
canvas.delete('all')
canvas.create_text(160,180,text='GAME OVER',font="Verdana 12",fill="red")
root.unbind('<Up>')
root.unbind('<Down>')
root.unbind('<Left>')
root.unbind('<Right>')
canvas.delete(snake)
return()
root.bind('<Up>', move_up)
root.bind('<Down>', move_down)
root.bind('<Left>', move_left)
root.bind('<Right>', move_right)
if navigation == "up":
y1 -= moveY1
y2 -= moveY2
elif navigation == "down":
y1 += moveY1
y2 += moveY2
elif navigation == "left":
x1 -= moveX1
x2 -= moveX2
elif navigation == "right":
x1 += moveX1
x2 += moveX2
canvas.delete(snake)
time.sleep(0.1)
snake = canvas.create_rectangle(x1,y1,x2,y2, fill="green")
root.after(a, draw)
if death == False:
draw()
root.mainloop()