Python pygame鼠标。get_pos()不工作

Python pygame鼠标。get_pos()不工作,python,mouse,pygame,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 wo

我无法使用非常简单的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()
当我在窗口周围移动鼠标时,仅输出(0,0):

(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
有人能查一下吗

编辑-固定代码:

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()

Pygame在运行时会不断地发送事件。这些需要以某种方式处理,否则pygame将挂起而不做任何事情。最简单的修复方法是将其添加到主循环:

...
    while True:
        for event in pygame.event.get():
            pass                                             
        print pygame.mouse.get_pos()
...

我以前从未用过这个,但我发现

您需要等待事件发生。我假设这会清空堆栈,并允许您稍后获取数据

for event in pygame.event.get()

注意到了,谢谢。顺便说一句,下次你可以编辑答案我总是忘记那件事!我认为您可能只需要pygame.event.get()就可以成功,并且避免同时使用for循环。