0

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

D-Inventor
  • 450
  • 5
  • 23

1 Answers1

0

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()
furas
  • 134,197
  • 12
  • 106
  • 148
  • I have tried your program, but I got no response from my controller. I think that if there is no controller attached before `pygame.joystick.init`, the program does not register the controller events. That is why I used the `quit` and `init` commands for `pygame.joystick`. – D-Inventor Jul 14 '14 at 07:52