【问题标题】:pygame joystick slows down event handlingpygame 操纵杆减慢事件处理速度
【发布时间】:2014-07-13 18:35:41
【问题描述】:

我正在使用一个函数来等待控制器出现,但我遇到了问题。当我按下一个键时,我希望该功能停止。通常这些事件处理得很好,但在这部分它工作得很糟糕。该事件似乎很晚才被添加到事件队列中,有时甚至根本没有。我的猜测是这是因为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()

我想使用退出键来结束程序,但只有在多次按下后才有效。 QUIT 事件也是如此。在打印事件时,我发现大多数时候都没有找到任何事件。 我使用 windows 8.1 和 python 3.4 和 pygame 1.9.2

【问题讨论】:

    标签: python events pygame joystick


    【解决方案1】:

    我没有操纵杆,所以我无法测试它,但通常我会像鼠标一样执行此操作,我不会等待控制器:

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

    【讨论】:

    • 我已经尝试过您的程序,但我的控制器没有响应。我认为如果pygame.joystick.init之前没有附加控制器,程序不会注册控制器事件。这就是为什么我对pygame.joystick 使用quitinit 命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多