简体   繁体   中英

finding the angle between to points with pygame

I am making a game with pygame and i want the player the face the cross hair, this is the code I have, I made it print out the angle in my idle and I noticed that the angles where all wrong and the player didnt rotate at all anyway. I've seen some other questions like this and I've followed them but i still get the wrong angle and the image dosnt rotate anyway

here is my code for the angle and rotating image:

mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_x -= crosshair.get_width() / 2
mouse_y -= crosshair.get_height() / 2
player_atan = math.atan2(player_x-player_y, mouse_x-mouse_y)
player_angle = degrees(player_atan)
pygame.transform.rotate(screen, player_angle)

您必须从不同的点减去相同的分量。

player_atan = math.atan2(mouse_y - player_y, mouse_x - player_x)

You should check out the python docs: it has some valuable info.

The reason why your sprite does not rotate is because rotate returns a new rotated surface.

A link to the docs: docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM