So I'm trying to test out a simple combat system in Pygame where the player can basically shoot a projectile towards an area based on where the mouse position is. So for example when he clicks on the top left of his screen, the projectile moves towards there at a steady speed. I've created a function that will move each bullet in a list, here's the function.
def move_bullet(bullet_pos, direction):
# TODO Make the speed of the bullet the same no matter which direction it's being fired at
bullet_pos[0] += direction[0]/50
bullet_pos[1] += direction[1]/50
bullet_rect = pygame.Rect((bullet_pos[0], bullet_pos[1]), BULLET_SIZE)
return bullet_rect
The direction is calculated by subtracting the mouse's vector position by the player's vector position when the mousebuttondown event is triggered.
However, I have noticed that the closer I get to the player/origin of the bullet, the slower the bullet goes because the direction vector is smaller so the speed is differs depending on your mouse's position. I've heard of Vector normalization but I have no idea how to implement it because after doing a bit of research, apparently you normalize Vectors by getting it's magnitude and dividing the X and Y values by the magnitude? I got it from Khan Academy but it doesn't actually work. And I'm pulling my hair out over this so I have no choice but to ask this question here.
TL; DR
How do I normalize a Vector in Pygame?