我试图一次移动所有对象,但没有成功

时间:2017-02-16 14:15:57

标签: python-3.x tkinter

我正在开发一个项目,允许你选择一种类型的忍者并四处走动并攻击敌人。我完成了忍者选择,但我不确定如何移动所有创建忍者的对象。我已将多个对象放入c.move函数中,但我收到此错误:

c.move(self.hair,self.hair2,self.head,self.neck,self.chest,self.leg1,self.leg2,self.arm1,self.arm2,0,-5)
  File "C:\Users\mrden\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2431, in move
    self.tk.call((self._w, 'move') + args)
_tkinter.TclError: wrong # args: should be ".2417470327664 move tagOrId xAmount yAmount

这是我的示例代码:

#Ninja Game
from tkinter import *
root = Tk()
c=Canvas(width=800,height=600)
c.pack()
d = ""
class Ninja:
    def __init__(self,clan,):
        self.clan = clan
        if self.clan == "Uchiha":
         self.hair = c.create_polygon(52,485,52,452,99,452,99,485)
         self.head=c.create_rectangle(55,455,95,495,fill="white")
         self.hair2= c.create_polygon(74,452,83,477,99,452,fill="black")
        if self.clan == "Uzamaki":
         self.hair = c.create_polygon(52,485,52,452,99,452,99,485,fill="#FF3232")
         self.head=c.create_rectangle(55,455,95,495,fill="white")
        self.neck = c.create_rectangle(59,495,91,499)
        self.chest= c.create_rectangle(55,499,95,555)
        self.leg1 = c.create_rectangle(55,555,71,599)
        self.leg2 = c.create_rectangle(78,555,95,599)
        self.arm1=c.create_rectangle(42,499,55,555)
        self.arm2=c.create_rectangle(95,499,108,555)
    def move(self):
     if d == "U":
      if self.clan == "Uzamaki":
       c.move(self.hair,self.head,self.neck,self.chest,self.leg1,self.leg2,self.arm1,self.arm2,0,-5)
      if self.clan == "Uchiha":
       c.move(self.hair,self.hair2,self.head,self.neck,self.chest,self.leg1,self.leg2,self.arm1,self.arm2,0,-5)


n = Ninja("Uchiha")

def move_p(event):


    global d
    if event.keysym == "Up":
     d = "U"
     n.move()

c.bind_all("<KeyPress-Up>",move_p)

1 个答案:

答案 0 :(得分:0)

画布允许您向项目添加标签,以便您可以将整组项目视为一个。为此,请创建标记,将该标记添加到每个项目,然后在移动项目时使用该标记。

例如:

class Ninja:
    def __init__(self,clan,):
        self.clan = clan
        self.name = "ninja %d" % id(self)
        if self.clan == "Uchiha":
            self.hair = c.create_polygon(52,485,52,452,99,452,99,485, tags=[self.name, "ninja"])
            self.head=c.create_rectangle(55,455,95,495,fill="white", tags=[self.name, "ninja"])
            self.hair2= c.create_polygon(74,452,83,477,99,452,fill="black", tags=[self.name, "ninja"])
        if self.clan == "Uzamaki":
            self.hair = c.create_polygon(52,485,52,452,99,452,99,485,fill="#FF3232", tags=[self.name, "ninja"])
            self.head=c.create_rectangle(55,455,95,495,fill="white", tags=[self.name, "ninja"])
        self.neck = c.create_rectangle(59,495,91,499, tags=[self.name, "ninja"])
        self.chest= c.create_rectangle(55,499,95,555, tags=[self.name, "ninja"])
        self.leg1 = c.create_rectangle(55,555,71,599, tags=[self.name, "ninja"])
        self.leg2 = c.create_rectangle(78,555,95,599, tags=[self.name, "ninja"])
        self.arm1=c.create_rectangle(42,499,55,555, tags=[self.name, "ninja"])
        self.arm2=c.create_rectangle(95,499,108,555, tags=[self.name, "ninja"])

    def move(self):
        if d == "U":
            c.move(self.name, 0, -5)

如果你还给每个忍者一个共同的标签(例如:“忍者”),你可以立刻移动所有的忍者。标签是一个列表,因此您可以同时提供一个通用标签和一个特定于此忍者的标签。

例如:

c.create_polygon(..., tags=[self.name, "ninja"])

立即移动所有忍者

c.move("ninja", 0, -5)

移动特定的忍者:

c.move(n.name, 0, -5)