pygame.event.get() Not Returning Events Inside Thread
Issue:
🎜>在 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.
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()
PyGame event handling must occur in the main thread.
According to the PyGame event documentation event subsystem should be called from the main thread.
雖然可以從其他執行緒發送事件,但事件佇列必須在主執行緒中處理。因此,解決方案是將 event.get() 通話移至主執行緒。
以上是為什麼 pygame.event.get() 在執行緒執行時不傳回事件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!