I have a method inside a class that I've written that moves a sprite a set distance towards another instance of the class. However, whenever I call the method, I'm told that - on the 4th line - direction hasn't been defined. This surprised me, as I defined the variable on the line before.
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))
How do I fix this issue, I've really got no idea how to?
For those wondering, I'm using the exec function as it's the only way I know to use a specific anchor point in the calculation.