pygame摇杆导致事件处理变慢

0 投票
1 回答
592 浏览
提问于 2025-04-18 13:09

我在用一个函数来等待控制器出现,但遇到了问题。我希望这个函数在我按下一个键的时候能停止。通常情况下,这种事件处理得很好,但在这个部分却表现得很糟糕。事件似乎很晚才被加入到事件队列中,有时候甚至根本没有加入。我猜这可能是因为pygame.joystick没有初始化或者重新初始化。我就是不知道该怎么解决这个问题。我用这个程序来制造错误:

import pygame, sys
from pygame.locals import *

pygame.init()

SCREEN = pygame.display.set_mode((500,500))

ChangeRun = False

pygame.joystick.init()
if pygame.joystick.get_count() == 0:
    while pygame.joystick.get_count() == 0:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type in [KEYUP, KEYDOWN]:
                if event.key == K_ESCAPE:
                    ChangeRun = True
        if ChangeRun == True:
            break
        pygame.joystick.quit()
        pygame.joystick.init()

pygame.quit()
sys.exit()

我想用Esc键来结束程序,但只有在按了很多次之后才有效。QUIT事件也是这样。当我打印事件时,发现大部分时间都没有找到任何事件。我使用的是Windows 8.1,Python 3.4和Pygame 1.9.2。

1 个回答

0

我没有操纵杆,所以没法测试。不过通常我会像处理鼠标那样来做这件事,我不会等着控制器。

import pygame
from pygame.locals import *

pygame.init()
pygame.joystick.init()

screen = pygame.display.set_mode((500,500))

running = True

while running:

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

        elif event.type == JOYBUTTONDOWN:
            print 'joy:', event.joy, 'button:', event.button

# the end
pygame.joystick.quit()
pygame.quit()

撰写回答