Pygame 制作一个精灵面对鼠标 [英] Pygame Making A Sprite Face The Mouse
问题描述
我是 Pygame 的新手,但在 Python 上还不错,我正在创建一个带有俯视图的僵尸射击游戏.
按箭头键时,我设法使角色移动.但现在我需要让玩家面对鼠标/光标,而不是一直点击屏幕.
有什么帮助吗?
Im New To Pygame But Kinda OK On Python, Im Creating A Zombie Shooting Game With an overhead view.
I managed to make the character move when pressing the arrow keys. But now i need to get the player to FACE the mouse/cursor without clicking the screen all the time.
Any Help?
推荐答案
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
[1] - 如何求两个向量之间的角度:http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
[1] - How to Find the Angle Between Two Vectors: http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
以小角度旋转时,您的图像可能会降低质量.Pygame 文档页面中对此进行了讨论:http://pygame.org/docs/ref/transform.html#pygame.transform.rotate
Your image may lose quality when rotated at a small angle. It's discussed in Pygame documentation page: http://pygame.org/docs/ref/transform.html#pygame.transform.rotate
这篇关于Pygame 制作一个精灵面对鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!