A Simple State Machine with Python
November 13, 2013
Developing Fractionauts for the XO is quickly becoming one of my favorite things. Git(hub) makes it ridiculously easy for our team of 6 to all contribute and minimize destruction of each-others’ work. We’ve come quite far in the past month, rising from a paper concept to a working executable with a menu system.
Like almost all games, ours requires a state machine. It is simply a variable that determines what the game is “doing,” i.e. whether the user is on the main menu, help screen, or playing. From this, we can easily determine appropriate things to render and events to listen for, hence, a state machine.
With PyGame, we’ve implemented this with an if/else if block. In this case, the state is different than the “state” of being paused or un-paused. In our implementation, being paused is a separate variable, allowing the game to be paused regardless of the screen we’re on.
It may seem simple, and in our case, it is. But without this concept, video games would be nigh impossible to program.