I am writing a top-down shooter game. The player should hold a gun, and I want it to point in the direction of the mouse. Here is the code I have to calculate the angle it needs to be displayed:
rel_x, rel_y = target_x - self.x, target_y - self.y
self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
self.weapon_copy = pygame.transform.rotate(self.primary_weapon.image, self.angle)
display.blit(self.weapon_copy, (self.x + self.primary_weapon.x_offset -
int(self.primary_weapon.image.get_width() / 2 + display_scroll[0]),
self.y + self.primary_weapon.y_offset - int(self.weapon_copy.get_height() / 2)
- display_scroll[1]))
It works, but there is one issue: If I am pointing the gun to the right of the player, it looks normal, but if I point it to the left of the player, the gun still points towards the mouse, but it becomes upside-down (all my weapon images show the gun pointing to the right by default). What code can I add to make the gun right-side-up at all times?