首頁 >後端開發 >Python教學 >為什麼 pygame.event.get() 在執行緒執行時不傳回事件?

為什麼 pygame.event.get() 在執行緒執行時不傳回事件?

DDD
DDD原創
2024-11-13 06:14:021084瀏覽

Why is pygame.event.get() not returning events when executed within a thread?

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()
Resolution:

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn