标记删除其他小部件

时间:2018-05-14 19:44:12

标签: python tkinter

Python tkinter蛇游戏(对python不是很有经验)。当要制作苹果时,画布上没有显示任何内容。然后当你移动身体后再增加几个动作。每增加一个苹果,这一增长前的移动量就会增加1。我认为这是因为制作和删除了蛇块上的标签,但我不知道如何解决这个问题。还有其他原因,我该如何解决?

from tkinter import *
import random

window=Tk()
window.title("snake")
window.geometry("600x600")
can=Canvas(window,height=600,width=600,background="white")
can.pack()

num=1
x3=100
y3=100
decide=3
etch_len=10
etch_wid=10
body_len=2
apple_x1=0
apple_y1=0
colour=["black"]

def move_up(self):
    global x3
    global y3
    global num
    global apple_x1
    global apple_y1
    global body_len
    if x3==apple_x1 and y3==apple_y1:
        body_len=body_len+1
        apple_make(self)
    num=num+1
    x=str(num)
    can.create_line(x3,y3,x3,(y3-etch_len),width=etch_wid,fill=colour[0],tag=x)
    y3=y3-etch_len
    remove_last(self)


def move_down(self):
    global x3
    global y3
    global num
    global apple_x1
    global apple_y1
    global body_len
    if x3==apple_x1 and y3==apple:
        body_len=body_len+1
        apple_make(self)
    num=num+1
    x=str(num)
    can.create_line(x3,y3,x3,(y3+etch_len),width=etch_wid,fill=colour[0],tag=x)
    y3=y3+etch_len
    remove_last(self)


def move_left(self):
    global x3
    global y3
    global num
    global apple_x1
    global apple_y1
    global body_len
    if x3==apple_x1 and y3==apple_y1:
        body_len=body_len+1
        apple_make(self)
    num=num+1
    x=str(num)
    can.create_line((x3-etch_wid),y3,x3,y3,width=etch_wid,fill=colour[0],tag=x)
    x3=x3-etch_len
    remove_last(self)


def move_right(self):
    global x3
    global y3
    global num
    global apple_x1
    global apple_y1
    global body_len
    if x3==apple_x1 and y3==apple_y1:
        body_len=body_len+1
        apple_make(self)
    num=num+1
    x=str(num)
    can.create_line(x3,y3,(x3+etch_wid),y3,width=etch_wid,fill=colour[0],tag=x)
    x3=x3+etch_len
    remove_last(self)


def remove_last(self):
    global num
    global body_len
    i=num-body_len
    x=str(i)
    can.delete(x)


def apple_make(self):
    global apple_x1
    global apple_y1
    apple_x1=random.randint(10,590)
    apple_y1=10
    can.create_line(apple_x1,apple_y1,apple_x1,apple_y1,width=10,fill="red")


window.bind("w",move_up)
window.bind("s",move_down)
window.bind("a",move_left)
window.bind("d",move_right)
window.bind("<Up>",apple_make)
window.mainloop()

0 个答案:

没有答案