gpt4 book ai didi

python - 如何在pygame中检查未定义方向的鼠标运动?

转载 作者:行者123 更新时间:2023-12-01 04:33:13 24 4
gpt4 key购买 nike

我想检查鼠标是否按照未定义的方向移动,如下所示:

for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
"do something"

然后我想打印方向。这可能吗?

最佳答案

另一种可能是 pygame.mouse.get_rel() ,它提供自上次调用此函数以来鼠标的移动量。这样您就可以避免存储以前的位置。

简单的例子:

import sys, pygame,time

FPS=30
fpsClock=pygame.time.Clock()

screen = pygame.display.set_mode((650,650))
screen.fill(255,255,255)
done = False

while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
print pygame.mouse.get_rel()

pygame.display.update()
fpsClock.tick(FPS)

您将得到如下所示的输出,并且如您所见,您可以将其解释为描述运动方向的 (x,y) 形式的向量:

(0, 0)
(-1, 0)
(0, -8)
(0, 0)

关于python - 如何在pygame中检查未定义方向的鼠标运动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com