把所有的乌龟集合在一起

2024-06-16 11:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码。该守则的最后一个功能是把孩子们召集到他们的母亲身边。在

class MotherTurtle(Turtle):  
    def __init__(self, home):
        Turtle.__init__(self, home)
        self.children = []
        self.home = home
        self.setName("Mum")

    def giveBirth(self, name):
        newborn = Turtle(self.home)
        newborn.setName (name)
        self.children.append(newborn)
        return newborn

    def greetChildren(self):
        for child in self.children:
            print "Hi %s" %(child.name)

    def gatherChildren(self):
        for child in self.children:
            child.moveTo(self.home)

我要把孩子们叫去见他们的母亲。在

{1美元^

这是我运行程序时遇到的错误:

^{pr2}$

错误是:

^{3}$

Tags: nameinselfchildhomeforinitdef
1条回答
网友
1楼 · 发布于 2024-06-16 11:28:45
def gatherChildren(self):

    # get the position of the mother turtle    
    mumX = self.getXPos()
    mumY = self.getYPos()
    # use an offset so not all turtles are on top of each other
    spacing = 10
    offset = spacing
    # loop through the list of children to place each child close to the mother
    for child in self.children:
      child.moveTo(mumX + offset, mumY + offset)
      offset = offset + spacing 

相关问题 更多 >