for event in pygame.event.get():
if event.type == MOUSEMOTION:
mousex, mousey = event.pos
# build a vector between player position and mouse position
moveVector = (mousex-playerx, mousey-playery)
"""
compute the angle of moveVector from current vector that player is facing (faceVector).
you should be keeping and updating this unit vector, with each mouse motion
assume you have initial facing vector as (1,0) - facing East
"""
# compute angle as in [1]
# rotate the image to that angle and update faceVector
您可能需要修改减法的顺序(例如,它可能是鼠标位置playerPosition)或x和y参数到atan2的顺序(例如,您可能需要将y差异作为第一个参数而不是x传递),但这取决于您的坐标系。
[1]-如何找到两个向量之间的角度: http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
当以小角度旋转时,图像可能会失去质量。Pygame文档页中讨论了这个问题:http://pygame.org/docs/ref/transform.html#pygame.transform.rotate
工作代码:
相关问题 更多 >
编程相关推荐