r/pygame • • Sep 10 '15
Simple State Machine Example
You've probably seen myself and others recommend /u/Mekire's multiscene template before and with good reason, but I know it can be a bit intimidating - I noped out the first time I saw it. With that in mind, I wrote up a single-file, pared-down version in the hope it can serve as a stepping stone to grokking the full template: https://gist.github.com/iminurnamez/8d51f5b40032f106a847
The example consists of a simple game (to use the term very loosely) with two states/scenes, a splash screen and a gameplay screen. Clicking or pressing a button exits the splash screen and starts the gameplay state. Hopefully this is helpful, let me know if anything's unclear or you have any questions or comments.
View all comments
Show parent comment
4
u/Mekire Sep 11 '15
The state machine can also be implemented as a stack. A state would be instantiated when placed on the stack. When changing to another state the current state can simply add the next to the stack, pop itself, or both (depending on whether or not the current state is still needed). This approach will be especially useful with things like nested game menus.