pygame游戏杆减慢事件处理速度

2024-09-29 00:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想用escape键结束程序,但只有在多次按下后才有效。QUIT事件也会发生同样的情况。当打印事件时,我发现大部分时间没有找到事件。 我使用windows8.1和python3.4和pygame1.9.2


Tags: inimport程序eventgetifinittype
1条回答
网友
1楼 · 发布于 2024-09-29 00:28:22

我没有操纵杆,所以我不能测试它,但通常我会做类似于鼠标的操作,我不会等待控制器:

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

相关问题 更多 >