https://kitfucoda.medium.com/my-pygame-evolution-embracing-asyncio-and-immutability-for-scalable-design-84eae4601717
My journey into graphical application building started unexpectedly after a friend's game jam project piqued my interest. Exploring Python ports of Processing eventually led me to Pygame, a lower-level library that surprisingly sparked my curiosity. What began as a plan to "build a reasonably functional Tic-Tac-Toe version over a weekend" soon revealed the depth of Pygame's foundational nature and the need for custom architectural solutions.
Pygame's synchronous and non-thread-safe nature posed immediate challenges for building interactive applications. Initial attempts to use asyncio.to_thread led to unexpected SIGSEGV errors, highlighting the need for a different approach. The solution emerged through iterative debugging and a suggestion from my coding companion, the chatbot, to incorporate asyncio.sleep(0). This critical addition allowed asynchronous tasks to finally run effectively, enabling a refactored main loop that separates event processing and display updates.
Managing shared data and dispatching events concurrently presented its own set of hurdles. Drawing heavily from my experience in JavaScript web development, I designed an immutable application registry, leveraging asyncio.Queue to defer changes to elements and the game state. This approach ensures all updates are processed predictably, avoiding race conditions and making it possible to register and dispatch events across the application in a robust, web-inspired manner.
As the code matured through refactoring, a clear architectural structure began to emerge. Extracting game-specific logic into a separate module became trivial, and a review with Gemini confirmed that the design could evolve into a reusable mini-framework. The Application dataclass even showed resemblances to an Entity-Component-System, adding a new concept to explore. While time may be limited, the curiosity to further develop this separation and implement web-like system events persists, motivated by a desire to build robust solutions.
#Pygame #Asyncio #Python #GameDevelopment #SoftwareArchitecture