我正在尝试用Tkinter制作一个可移动的精灵;它很有用,但我不确定用画布绑定是最好的解决方案。在我按下“w”之后会有一个延迟,例如,角色移动一次,停止几秒钟,然后开始有点迟钝。在
代码:
import Tkinter as t
tk = t.Tk()
w = t.Button()
c = t.Canvas(tk, bg = "#000000", bd = 3)
x = 20
y = 20
img = t.PhotoImage(file = "hi.png")
c.create_image(x, y, image = img)
coord = 10, 50, 240, 210
def clearboard():
c.delete("all");
def key(event):
global y
global x
pr = event.char
if(pr is "w"):
y -= 5
if(pr is "s"):
y += 5
if(pr is "a"):
x -= 5
if(pr is "d"):
x += 5
c.delete("all");
c.create_image(x, y, image = img)
w = t.Button(tk, command = clearboard, activebackground = "#000000", activeforeground = "#FFFFFF", bd = 3, fg = "#000000", bg = "#FFFFFF", text = "Clear", relief="groove")
c.focus_set()
c.bind("<Key>", key)
w.pack()
c.pack()
tk.mainloop()
我的问题是,如何消除前面提到的延迟,使移动更顺畅?在
提前谢谢。在
好吧,我找到了问题的答案。我刚刚创建了一个游戏循环并添加了一个}的绑定。在
velx
变量,并添加了<KeyPress>
和{代码:
相关问题 更多 >
编程相关推荐