pygame.event.get() Not Returning Events Inside Thread
Issue:
In a pac-man style game using PyGame, the receiving_inputs function is not retrieving any keyboard events when executed within a thread, while mouse events are still registered.
Code Snippet:
def receiving_inputs(self): while True: events = pg.event.get() for event in events: if event.type == pg.KEYDOWN: # Handle keyboard input time.sleep(1/60) threading.Thread(target=self.receiving_inputs).start()
Resolution:
PyGame event handling must occur in the main thread.
According to the PyGame event documentation:
The event subsystem should be called from the main thread.
Although events can be sent from other threads, the event queue must be processed in the main thread. So the solution is to move the event.get() call into the main thread.
The above is the detailed content of Why is pygame.event.get() not returning events when executed within a thread?. For more information, please follow other related articles on the PHP Chinese website!