ARTICLE AD BOX
The Pythagorean Theorem tells the distance between points in n-dimensional space. For your case
distance = math.sqrt(sum[ (self.mouse_pos[0] - self.rect.x) ** 2 , (self.mouse_pos[1] - self.rect.y) ** 2 ])) if distance < self.speed: #remove the bullet from the sceneThis is not a great solution, because the bullet will move unrealistically, with different speeds at different angles. In fact it will move at sqrt(2) speed at a 45' angle and with a speed of 1 at 90'.
But, I assume you don't want a trigonometry lecture.
EDIT: Here's the trigonometry lecture. Your program will move the bullet realistically because it moves it by the full speed in both the x and y direction. What you need to do is to have the speed be slower in both directions, such that the hypotenuse of the triangle that the x-speed and y-speed (vectors) form is equal to 1. Since the speed is always 1, this triangle, just happens to be the unit circle.
m |\ | \ sin(A) | \ This speed is 1 | \ | A\ ------p cos(A)If your character is at p, and the mouse is at m, then you find the angle between them labeled here as A. Then the x-speed will be the cosine of A and the y-speed will be the sine of A.
How do you find the angle? you use the arctangent... in fact, here's the code to use.
import math if not self.rect.collidepoint(self.mouse_pos): angle = math.atan2( (self.mouse_pos[1] - self.rect.y) , (self.mouse_pos[0] - self.rect.x) self.rect.x += self.speed * math.cos(angle) self.rect.y += self.speed * math.sin(angle)or something more like that. I may have my symbols mixed up.
