Реализовать рост змейки в игре
помогите, пожалуйста! как я могу сделать рост змейки когда она съедает шары?
код:
import time
import random
from tkinter import *
import os
eaten = 0
x = random.randint(0, 350)
y = random.randint(0, 450)
r = 10
foodPos1 = x - r
foodPos2 = x + r
foodPos3 = y - r
foodPos4 = y + r
x1 = 80
x2 = 100
y1 = 80
y2 = 100
moveX1 = 10
moveX2 = 10
moveY1 = 10
moveY2 = 10
root = Tk()
navigation = ''
root.title('Змейка 2024')
root.geometry('350x450')
canvas = Canvas(root, width = 350, height = 450)
canvas.pack()
canvas.create_text(50,20,text='съедено: ',font="Verdana 12",fill="black")
eat = canvas.create_text(95,20,text=eaten,font="Verdana 12",fill="black")
snake = canvas.create_rectangle(x1,y1,x2,y2, fill="green")
food = canvas.create_oval(foodPos1, foodPos3, foodPos2, foodPos4)
BX = random.randint(0, 350)
BY = random.randint(0, 350)
barpos1 = BX - r
barpos2 = BX + r
barpos3 = BY - r
barpos4 = BY + r
barrier = canvas.create_rectangle(barpos1, barpos3, barpos2, barpos4, fill="gray")
def game_over():
global eaten
f = open("ваши рекорды.txt", "a")
f.write(str(eaten))
f.close()
Window = Tk()
canvas = Canvas(Window, width = 350, height = 450)
canvas.pack()
canvas.create_text(200,180,text='GAME OVER',font="Verdana 12",fill="red")
root.destroy()
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 draw():
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
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")
collision1 = canvas.find_overlapping(x1, y1, x2, y2)
if barrier in collision1:
game_over()
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
if navigation == "down":
y1 += moveY1
y2 += moveY2
if navigation == "left":
x1 -= moveX1
x2 -= moveX2
if 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(10, draw)
draw()
root.mainloop()