简体   繁体   中英

pygame joystick slows down event handling

I am using a function to wait until a controller is present, but i ran into a problem. I wanted the function to stop when I press a key. Usually these events are handled just fine, but in this part it works terrible. The event seems to be added to the event queue very late or sometimes not at all. My guess is that it is because of the uninitialisation and reinitialisation of pygame.joystick . I just don't know how to solve it. I used this program to create the error:

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

I wanted to use the escape key to end the program, but only after a lot of pressing it works. The same happens with the QUIT event. When printing the events I found that most of the time no event was found. I use windows 8.1 and python 3.4 and pygame 1.9.2

I have no joystick so I can't test it but normally I would do this similar to mouse and I wouldn't wait for controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM