我正在使用 Pygame 用 Python 编写游戏。
(我的显示窗口的)坐标是
( 0 , 0 )
在左上角和
(640,480)
在右下角。
角度是
0°
向上时,
90°
指向右侧时。
我有一个位于中心位置的玩家 Sprite ,我希望枪支上的炮塔指向玩家。我该怎么做?
说,
x1
,y1
是炮塔坐标
x2
,y2
是玩家坐标
a
是角度的量度
最佳答案
首先,math
有一个方便的atan2(denominator, numerator)
函数。通常,您会使用 atan2(dy,dx)
但因为 Pygame 会相对于笛卡尔坐标翻转 y 轴(如您所知),您需要制作 dy
负角,然后避免负角。 (“dy”仅表示“y 的变化”。)
from math import atan2, degrees, pi
dx = x2 - x1
dy = y2 - y1
rads = atan2(-dy,dx)
rads %= 2*pi
degs = degrees(rads)
degs
应该是您要查找的内容。
关于python - 我如何找到pygame中两点之间的角度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10473930/