I am working on a little game with Pygame and I stumbled across a little thing I couldn't fix by myself. I want that the enemy moves towards the Player, but the way I do it now has some issues. One of it is, that the enemy is faster on the positive coordinates and the second problem is that the program crashes when the enemy is directly on the "player"
Here is the problematic code and an example video:
px, py = player_rect.center
sx, sy = snail_rect.center
direction = (px - sx, py - sy)
dx, dy = (px - sx, py - sy)
length = math.sqrt(dx*dx + dy*dy)
dx /= length
dy /= length
c = 3
dx = c * dx
dy = c * dy
snail_rect.center = (sx + dx, sy + dy)
I don't think the math is wrong, but I can't find why it's getting slower on the negative x and y coordinate.