我正在嘗試使用Tkinter編寫一個Snake遊戲程序 我的蛇由下面定義的Circle列表組成。但是,當我調用移動方法時,最後一個不移動。問題是什麼?Tkinter - Canvas.move()不移動列表中的最後一項
class Circle:
def __init__(self,canv,x,y,index,direc):
self._x = x
self._y = y
self._index = index
self._direction = direc
self._canvas = canv
coordinat = (self._x)-5,(self._y)-5,(self._x)+5,(self._y)+5
if index==0:
color = "gold"
else:
color = "green"
self._circle = canv.create_oval(coordinat ,fill=color, tags=str(self._index))
def _move(self):
if self._direction == "up":
delta = (0,-5)
elif self._direction == "down":
delta = (0,5)
elif self._direction == "left":
delta = (-5,0)
elif self._direction == "right":
delta = (5,0)
tag = str(self._index)
self._canvas.move(tag, delta[0],delta[1])
她是我如何稱呼它
self._canvas = Canvas(self._playArea,width=750,height=450,bg="#1C1C1C")
x = int(self._parent.winfo_width()/4.)
y = int(self._parent.winfo_height()/ 4.)
circle = Circle(self._canvas,x,y,0,"right")
self._circleList.append(circle)
self._addCircle()
self._addCircle()
self._addCircle()
self._addCircle()
self._addCircle()
self._canvas.place(x=0, y=0)
for i in range(0,500):
for x in self._circleList:
x._move()
root.update()
root.after(10)
,這是addCircle方法
length = len(self._circleList)
if self._circleList[length-1]._direction == "right":
x = (self._circleList[length-1]._x)-10
y = self._circleList[length-1]._y
d = "right"
elif self._circleList[length-1]._direction == "left":
x = (self._circleList[length-1]._x) + 10
y = self._circleList[length-1]._y
d = "left"
elif self._circleList[length-1]._direction == "up":
x = self._circleList[length-1]._x
y = (self._circleList[length-1]._y)+10
d = "up"
elif self._circleList[length-1]._direction == "down":
x = self._circleList[length-1]._x
y = (self._circleList[length-1]._y)-10
d = "down"
newCircle = Circle(self._canvas,x,y,length,d)
self._circleList.append(newCircle)
千萬不要調用'update'。另外,如果您將您的代碼列表更改爲自包含,則有人可能會查看它。 – mmgp