Search code examples
pythonpygame

Initiating a controller in pygame causes script to stop


I've tried a few examples of pygame being used with controllers and they all seem to encounter the same issue, I can't figure out if I haven't installed pygame correctly or if im doing something else wrong.

import pygame

pygame.joystick.init()
for index in range(pygame.joystick.get_count()):
    joystick = pygame.joystick.Joystick(index)
    print(joystick) # <- does actually return a controller


while True:
    pass # <- runs a few times and then stops

Solution

  • See PyGame window not responding after a few seconds. You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

    For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

    e.g.:

    while True:
        pygame.event.pump()
    

    or

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False