Skip to main content
 首页 » 编程设计

Python Pygame 鼠标位置不更新

2025年04月02日15tintown

我正在尝试使用鼠标位置来实现一个游戏,以查看用户是否单击了按钮。不知何故,鼠标位置在几秒钟内不会更新,并在另外几秒钟内更改为新位置,然后重复。我在屏幕的不同位置移动并按下鼠标,但鼠标位置根本没有改变。 (在 python3.5.1 和 pygame 1.9.2 上工作,使用 IDE PyCharm)知道吗? 这是我的代码:

done = False 
while not done: 
   for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
           done = True 
   mouse = pygame.mouse.get_pos() 
   click = pygame.mouse.get_pressed() 
   if click[0]==1: 
        print(mouse) 
   pygame.display.update() 

请您参考如下方法:

电话

mouse = pygame.mouse.get_pos() 

除非执行事件 MouseMotion,否则不会更新位置。

如果您在 MAC 的窗口中执行程序,则必须按下、按住并移动鼠标(如果您要按下、按住,然后移动鼠标,pygame.mouse.get_pos() 将返回当前鼠标位置)。