Я пишу игру змейку и при создании игрового поля self.window = Window(self.root) с помощью метода в другом методу я не могу взаимодействовать с self.window чтобы удалить его.
меню
from tkinter import*
from Window import*
class Menu():
def __init__(self):
self.root = Tk()
self.root.title("умри или кушай")
self.root.geometry("500x500")
self.root.resizable(False,False)
self.btn1 = Button(self.root, text="умри",command=self.did, font=("Arial Bold", 30))
self.btn1.grid(column=0,row=0)
self.Label = Label(self.root, text="или", font=("Arial Bold", 30))
self.Label.grid(column=1,row=0)
self.root.bind("<Button-3>",self.exet)
self.btn2 = Button(self.root, text="кушай",command=self.Iet, font=("Arial Bold", 30))
self.btn2.grid(column=2,row=0)
self.root.mainloop()
def did(self):
self.root.destroy()
def Iet(self):
self.btn1.grid_remove()
self.btn2.grid_remove()
self.Label.grid_remove()
#self.root.destroy()
self.window = Window(self.root)
def exet(self,event):
self.window.canvas.destroy()
self.btn1.grid()
self.btn2.grid()
self.Label.grid()
if __name__ == "__main__":
menu = Menu()
игровое поле
from tkinter import*
from shepe import*
from anaconda import*
class Window():
def __init__(self, root):
self.root = root
#self.root = Tk()
#self.root.title("Змейка")
#self.root.resizable(False,False)
self.canvas = Canvas(self.root,width=500,height=500)
self.canvas.pack()
self.anaconda = Anaconda(self.canvas)
self.a = Circle(self.canvas)
self.b = Circle(self.canvas)
self.c = Circle(self.canvas)
self.d = Circle(self.canvas)
self.v = Circle(self.canvas)
self.i = Circle(self.canvas)
self.f = Circle(self.canvas)
self.a1 = Circle(self.canvas)
self.b1 = Circle(self.canvas)
self.c1 = Circle(self.canvas)
self.d1 = Circle(self.canvas)
self.v1 = Circle(self.canvas)
self.i1 = Circle(self.canvas)
self.f1 = Circle(self.canvas)
self.canvas.bind_all("<KeyPress-Left>",self.button)
self.canvas.bind_all("<KeyPress-Right>",self.button)
self.canvas.bind_all("<KeyPress-Up>",self.button)
self.canvas.bind_all("<KeyPress-Down>",self.button)
self.root.mainloop()
def button(self, event):
self.anaconda.spake_move(event)
self.a.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.b.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.c.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.d.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.v.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.i.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.f.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.a1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.b1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.c1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.d1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.v1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.i1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
self.f1.coords_control(self.anaconda,self.anaconda.snake_x,self.anaconda.snake_y)
змейка
class Anaconda():
def __init__(self,canvas,width=245,height=245):
self.canvas = canvas
self.snake_item = 10
self.x_nav = 0
self.y_nav = 0
self.snake_x = 24
self.snake_y = 24
self.snake_list = []
self.snake_size = 4
self.canvas.bind_all("<KeyPress-Left>",self.spake_move)
self.canvas.bind_all("<KeyPress-Right>",self.spake_move)
self.canvas.bind_all("<KeyPress-Up>",self.spake_move)
self.canvas.bind_all("<KeyPress-Down>",self.spake_move)
def delete_blok_snake(self):
if len(self.snake_list) >= self.snake_size:
self.temp_item = self.snake_list.pop(0)
self.canvas.delete(self.temp_item[2])
#@staticmethod
def ss(self, canvas, x, y):
if x <= 0:
x = 0
elif x >=50:
x = 50
else:
pass
if y <= 0:
y = 0
elif y >=50:
y = 50
else:
pass
snake=canvas.create_rectangle(x*self.snake_item,
y*self.snake_item,
x*self.snake_item+self.snake_item,
y*self.snake_item+self.snake_item,
fill="red")
self.snake_list.append([x,y,snake])
def spake_move(self,event):
if event.keysym == "Up":
self.x_nav = 0
self.y_nav = -1
self.delete_blok_snake()
elif event.keysym == "Right":
self.x_nav = 1
self.y_nav = 0
self.delete_blok_snake()
elif event.keysym == "Left":
self.x_nav = -1
self.y_nav = 0
self.delete_blok_snake()
elif event.keysym == "Down":
self.x_nav = 0
self.y_nav = 1
self.delete_blok_snake()
self.snake_x = self.x_nav + self.snake_x
self.snake_y = self.y_nav + self.snake_y
self.ss(self.canvas, self.snake_x, self.snake_y)
def new_snake_size(self, new):
self.snake_size = self.snake_size + new
еда для змейки
import random
class Circle():
def __init__(self,canvas):
self.canvas = canvas
self.blok = 10
self.net_x = random.randint(0,50)
self.net_y = random.randint(0,50)
print(self.net_x," ",self.net_y)
self.oval = self.canvas.create_oval(self.blok*self.net_x,
self.blok*self.net_y,
self.blok*self.net_x+self.blok,
self.blok*self.net_y+self.blok,
fill="blue")
def coords_control(self, anaconda, x, y):
if (x==self.net_x) and (y==self.net_y):
anaconda.new_snake_size(1)
self.canvas.coords(self.oval,-100,-100,-100+self.blok,-100+self.blok)