簡體   English   中英

Tkinter-Canvas.move()不會移動列表中的最后一項

[英]Tkinter - Canvas.move() doesn't move the last item in the list

我正在嘗試使用Tkinter編寫Snake Game程序。我的蛇由下面定義的Circle列表組成。 但是,當我調用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)

問題是您要創建第一個index=0 ,然后使用該index作為標記來標識該項目。 您不應該使用整數作為標記,它是為項目的ID保留的,但是,畢竟您可以使用它。 除了Tcl將0評估為false之外,因此您沒有有效地為其定義任何標簽。 事實證明,當您調用canvas.move(0, ...)您不會移動任何已創建的圓。 但是,您創建的下一個項目被分配了標簽“ 1”,並且當您調用canvas.move(1, ...)您實際上是在移動先前創建的項目(帶有“金色”顏色的項目),因為它是自動進行的由Tcl分配ID為“ 1”。 對創建的所有其他圈子重復上述步驟。

解決此問題的快速方法是更改​​代碼,以對要傳遞的所有這些索引使用index + 1 但是您包含的代碼中有幾個問題,因此這里有一個修改后的代碼,可以執行您需要執行的操作:

import Tkinter

UP, RIGHT, DOWN, LEFT = range(4)

class Circle:
    def __init__(self, canv, x, y, direc, color):
        self.x, self.y = x, y
        self.direction = direc

        self._canvas = canv
        coord = (self.x)-5, (self.y)-5, (self.x)+5, (self.y)+5
        self._index = canv.create_oval(coord, fill=color)

    def move(self):
        y_sign = 1 if self.direction == DOWN else -1
        x_sign = 1 if self.direction == RIGHT else -1
        if self.direction in (UP, DOWN):
            delta = (0, y_sign * 5)
        else:
            delta = (x_sign * 5, 0)
        self._canvas.move(self._index, delta[0], delta[1])


def add_circle(canvas, l):
    d = l[-1].direction
    if d in (UP, DOWN):
        x = l[-1].x
        y = (1 if d == UP else -1) * 10 + l[-1].y
    else:
        x = (1 if d == LEFT else -1) * 10 + l[-1].x
        y = l[-1].y

    circle = Circle(canvas, x, y, d, "green")
    l.append(circle)


def move_circles(circle_l, root):
    for c in circle_l:
        c.move()
    root.after(50, lambda: move_circles(circle_l, root))

root = Tkinter.Tk()
width, height = 750, 450

canvas = Tkinter.Canvas(width=width, height=height, bg="#1C1C1C")
canvas.pack(fill=None, expand=False)

circle_l = []
circle = Circle(canvas, width / 4, height / 4, RIGHT, "gold")
circle_l.append(circle)
for _ in range(5):
    add_circle(canvas, circle_l)

move_circles(circle_l, root)

root.mainloop()

這並不是真正的答案,但是您可以使最后一段代碼更短/更高效:

length = len(self._circleList)
    x = self._circleList[length-1]._x
    y = self._circleList[length-1]._y
    d = self._circleList[length-1]._direction

    if d == "right":
        x -= 10
    elif d == "left":
        x += 10
    elif d == "up":
        y += 10
    elif d == "down":
        y -= 10

    newCircle = Circle(self._canvas,x,y,length,d)
    self._circleList.append(newCircle)

首先設置默認值,然后僅在必須時修改xy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM