我在 pygame 中运行一个游戏,发现了一个缺陷。按照我编码的方式,如果鼠标移出窗口,游戏就会开始原地转圈。当您获取 时pygame.mouse.get_pos(),它将返回鼠标在窗口中检测到的最后一个值,但否则不会指示鼠标已离开窗口。那么,有什么方法可以检查鼠标是否已停止悬停在 pygame 窗口上并离开它?
我正在使用 pygame 和 math 模块尝试围绕两点绘制一个矩形。这就是我的意思:
从点到矩形末端的距离被指定为类的属性。我已经尝试了很多方法来达到它的位置,并且显示的角度可能是唯一的工作角度。这是我的矩形类:
def Pol(x, y):
"""Converts rectangular coordinates into polar ones"""
if x == 0: # This might be the source of my problems, but without it, it raises ZeroDivisionErrors at certain places
if y >= 0:
return [y, 90]
else:
return [-y, 270]
r = math.sqrt(x**2+y**2)
angle = (math.degrees(math.atan((y/x))))%360
return [r, angle]
class Path(pygame.sprite.Sprite):
def __init__(self, start, end, color, width, **options):
self.__dict__.update(options)
self.start = start
self.end = end
self.color=color
# Call the parent class (Sprite) …Run Code Online (Sandbox Code Playgroud)