我正在尝试检查鼠标左键按下时是否移动了鼠标,然后返回方向。我尝试在“ while True”循环中两次检查鼠标位置,但这不起作用。这是我的代码:
import pygame
from pygame import*
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
try:
X, Y = pygame.mouse.get_pos()
print(X,Y) #just a check
pygame.time.wait(20)
try:
x, y = pygame.mouse.get_pos()
print(x,y) #just a check
if x-X != 0 or y-Y != 0:
print('moved')
print(x-X, y-Y)
elif x-X == 0 and y-Y == 0:
print('not moved')
except AttributeError:
pass
except AttributeError:
pass
但是它总是返回不动。我究竟做错了什么?
您需要用另一种方法来做,检查是否运动,然后按下按钮:
if event.type == pygame.MOUSEMOTION and event.buttons[0]:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句