1

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
DrPoptart
  • 13
  • 2

1 Answers1

0

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I've tried adding that, still no change it just runs the loop once or twice and then the whole script stops. – DrPoptart Jun 04 '22 at 14:48
  • @DrPoptart Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). I can't guess what's wrong with your code. – Rabbid76 Jun 04 '22 at 14:55
  • from what I've checked it seems like after i use pygame.joystick.Joystick() after a few seconds the whole script just seems to stop working – DrPoptart Jun 04 '22 at 15:00
  • @DrPoptart No, I cannot reproduce the problem. It works fine for me. – Rabbid76 Jun 04 '22 at 15:01
  • What controller are you using? Maybe that's the problem. – AzlanCoding Jun 04 '22 at 23:18