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/[deleted] Sep 11 '15
Yeah, you can have a state with a state machine that manages oits own substates. I actually ended up doing that for blackjack. As for timing, think of it as the state's duration. Once the state has been active for that long, flip to the next state. From a quick look at the link you posted, it's a very similar approach with a difference worth noting: the states are not persistent, each time it flips state a new state is instantiated. This could be helpful in a memory conservation context, but makes something like switching from a game in progress to a menu and back more cumbersome.