为什么不定义方向变量

时间:2016-12-10 20:57:39

标签: python python-3.x pygame

我在我编写的类中有一个方法,它将精灵移动到另一个类的实例。但是,每当我调用该方法时,我都会被告知 - 在第4行 - 方向尚未定义。这让我感到惊讶,因为我之前在线上定义了变量。

def move_towards(self, sprite, magnitude = 1, anchor = 'center'):
        '''Moves the sprite towards another sprite by a specified magnitude'''
        exec('direction = 360 - atan2(sprite.rect.' + anchor + '[1] - self.rect.centery, sprite.rect.' + anchor + '[0] - self.rect.centerx) * 180 / pi')
        self.rect.x += magnitude * cos(radians(direction))
        self.rect.y -= magnitude * sin(radians(direction))

如何解决这个问题,我真的不知道怎么做?

对于那些想知道的人,我正在使用exec函数,因为这是我知道在计算中使用特定锚点的唯一方法。

0 个答案:

没有答案
相关问题