Non*_*one 1 python pygame coordinates degrees
我正在用pygame在python中制作一个蛇游戏,并且为了移动角色,我有一个整数,它应该是它应该移动的角度.我有什么方法可以根据度数来改变x和y的变化吗?例如:func(90) # [0, 5]或func(0) # [5, 0]
import math
speed = 5
angle = math.radians(90) # Remember to convert to radians!
change = [speed * math.cos(angle), speed * math.sin(angle)]
Run Code Online (Sandbox Code Playgroud)
角度的正弦和余弦乘以总移动量,将为您提供X和Y的变化。
import math
def func(degrees, magnitude):
return magnitude * math.cos(math.radians(degrees)), magnitude * math.sin(math.radians(degrees))
>>> func(90,5)
(3.0616169978683831e-16, 5.0)
>>> func(0,5)
(5.0, 0.0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2938 次 |
| 最近记录: |