我想在pygame中做一个自上而下的射击游戏.当我添加代码以使玩家朝他们面对的方向移动时.玩家会朝奇怪的方向移动.这是我正在使用的代码:
if pygame.key.get_pressed()[K_UP]:
playerX = playerX - math.cos(playerFacing)
playerY = playerY - math.sin(playerFacing)
if pygame.key.get_pressed()[K_DOWN]:
playerX = playerX + math.cos(playerFacing)
playerY = playerY + math.sin(playerFacing)
Run Code Online (Sandbox Code Playgroud)
我尝试输入math.cos(90)并且它等于-0.299515394756但是我的计算器告诉我它等于0.我可能只是犯了一个愚蠢的错误,但任何人都可以告诉我我做错了什么.谢谢Xeno
math.sin
,math.cos
等采取的角度在弧度.
您可以使用math.radians
将度数转换为弧度.所以:
math.sin(math.radians(90)) == 0
Run Code Online (Sandbox Code Playgroud)
你可以在发布的代码片段中修复它:
if pygame.key.get_pressed()[K_UP]:
playerX = playerX - math.cos(math.radians(playerFacing))
playerY = playerY - math.sin(math.radians(playerFacing))
if pygame.key.get_pressed()[K_DOWN]:
playerX = playerX + math.cos(math.radians(playerFacing))
playerY = playerY + math.sin(math.radians(playerFacing))
Run Code Online (Sandbox Code Playgroud)
不过我建议到处使用弧度.