This is the relevant part of the code that I am having issues with. The player.x and player.y are getting "AttributeError: type object 'player' has no attribute 'x'" errors in the debug console. I have a seperate class called "player" and I want to get its x and y coordinate while it is moving around so that the enemy can move toward it. This is the player class beginning part that is also relevant:
class player(object):
def __init__(self, x, y, sprintMultiplier, fps):
self.x = x
self.y = y
self.vel = 1/fps * 150
class enemy(object):
def __init__(self, fps, difficulty):
pass
def draw(self, window):
self.moveTowardsPlayer()
window.blit(self.downStanding, (self.x, self.y))
def moveTowardsPlayer(self):
dx, dy = self.x - player.x, self.y - player.y
dist = math.hypot(dx, dy)
dx, dy = dx/dist, dy/dist
self.x += dx * self.vel
self.y += dy * self.vel
Error in your code
So the work around the problem is.
You either declare the x and y values as global variables as below and access those x and y variables as global ones in the constructor as given below.
class player(object):
x = 0
y = 0
def __init__(self, x, y, sprintMultiplier, fps):
player.x = x
player.y = y
self.vel = 1/fps * 150
or Pass a player instance to the method moveTowardsPlayer() within the enemy class if you don't want to keep the x and y variables (of player class) as global/class variables. Code is given below.
def moveTowardsPlayer(self, player1):
dx, dy = self.x - player1.x, self.y - player1.y
dist = math.hypot(dx, dy)
dx, dy = dx/dist, dy/dist
self.x += dx * self.vel
self.y += dy * self.vel