0

I'm attempting to make a Pygame game where the player holds a weapon that points towards the mouse, the movement is fine, but I cannot get the weapon to point to the cursor! I took and edited a code snippet from somewhere else but it doesn't seem to be working; here's the code:

    # weapons ---------------------------------------------------------------
    mouse = pygame.mouse.get_pos()
    angle = 360-math.atan2(mouse[0]-x,mouse[1]-y)*180/math.pi
    rotimage = pygame.transform.rotate(shotgun_sprite,angle)
    WIN.blit(rotimage,(250-rotimage.get_width()/2,250-rotimage.get_height()/2))
    # end of loop --------------------------------------------------------------
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • please include a complete example ...afaik `atan2` takes `dy` as first arg and `dx` as second? – Joran Beasley Jun 20 '22 at 20:12
  • 1
    try `math.degrees(math.atan2(y0-mouse.y,x0-mouse.x)) + offset` or whatever variant you use for pygame ... (notice `atan2` takes **dy** first and dx second...) – Joran Beasley Jun 20 '22 at 20:14
  • `math.atan2` returns the angle in radians. You need to convert the angle to degrees: `math.degrees(math.atan2(...))` – Rabbid76 Jun 20 '22 at 20:14

0 Answers0