【问题标题】:Python (Pygame) return angle in specific intervalPython(Pygame)在特定间隔内返回角度
【发布时间】:2012-12-18 04:22:07
【问题描述】:

我有这个代码:

def getAngle(x1, y1, x2, y2):
rise = y1 - y2
run = x1 - x2
angle = math.atan2(run, rise) # get the angle in radians
angle = angle * (180 / math.pi) # convert to degrees
angle = (angle) % 360 # adjust for a right-facing sprite
return angle

...根据鼠标在屏幕上的位置返回一个角度。

我想设置一个时间间隔,我的对象的旋转将在特定点停止。例如:如果角度大于 90°,我希望我的对象停止获得更高的角度。在这种情况下,90° 应该像旋转停止的某个边界。

我认为我需要 2 个条件,因为角度不应高于左右 90°。

有人知道如何解决这个问题吗?

这部分代码在游戏循环中(它使用定义的getAngle):

    mousex, mousey = pygame.mouse.get_pos()
    for cannonx, cannony in (((width/2)-45, height-25), ((width/2)-45, height-25)):
        degrees = getAngle(cannonx, cannony, mousex, mousey)
        rotcannonImg = pygame.transform.rotate(cannonImg, degrees)
        rotcannonRect = rotcannonImg.get_rect()
        rotcannonRect.center = (cannonx, cannony)
        windowSurface.blit(rotcannonImg, rotcannonRect)

【问题讨论】:

  • 您可以使用math.degrees()将弧度转换为度数

标签: python rotation pygame intervals angle


【解决方案1】:

“角度不应该高于左右90°”这句话在英语中没有明确的含义,所以我不确定你的意思。但是,下图显示了当点 (x1, y1) 位于线交叉处且 (x2, y2) 尤其是八分圆时,各个角度与上升和运行符号之间的关系。注意,dy = 上升,dx = 运行。也就是说,您可能能够测试上升迹象并运行以获得您想要的信息。

【讨论】:

  • 感谢您的回答。但是,我设置了 while 循环,以获得这样的角度: while math.fabs(run) > 0 and rise > math.fabs(run): return angle 但是现在,每次当角度大于该角度时,我的程序都会退出并停止。我在做什么错'
  • 我认为他想将这些值限制在 -90 度到 90 度之间。
  • 成功了!解决方案:如果 math.fabs(run) > 0 and rise > math.fabs(run): return angle elif run 0: angle = 45 return angle 所以每次我的“大炮”到达max of 0 谢谢你,jwpat7! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多