sha*_*fty 4 python mouse pygame
我无法获得一个非常简单的pygame脚本来工作:
import pygame
class MainWindow(object):
def __init__(self):
pygame.init()
pygame.display.set_caption('Game')
pygame.mouse.set_visible(True)
# pygame.mouse.set_visible(False) # this doesn't work either!
screen = pygame.display.set_mode((640,480), 0, 32)
pygame.mixer.init()
while True:
print pygame.mouse.get_pos()
pygame.mixer.quit()
pygame.quit()
MainWindow()
Run Code Online (Sandbox Code Playgroud)
当我在窗口上移动鼠标时,这只是输出(0,0):
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
Run Code Online (Sandbox Code Playgroud)
有人可以检查吗?
编辑-固定代码:
import pygame
class MainWindow(object):
def __init__(self):
pygame.init()
pygame.display.set_caption('Game')
pygame.mouse.set_visible(True)
# pygame.mouse.set_visible(False) # this doesn't work either!
screen = pygame.display.set_mode((640,480), 0, 32)
pygame.mixer.init()
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
print pygame.mouse.get_pos()
pygame.mixer.quit()
pygame.quit()
MainWindow()
Run Code Online (Sandbox Code Playgroud)
Pygame在运行时会不断调度事件。这些需要以某种方式处理,否则pygame会挂起并且不执行任何操作。解决此问题的最简单方法是将其添加到主循环中:
...
while True:
for event in pygame.event.get():
pass
print pygame.mouse.get_pos()
...
Run Code Online (Sandbox Code Playgroud)