How should I implement a state based game?

Started by
1 comment, last by Nick Karasch 11 years, 5 months ago
I would like to handle game states the way Slick does, without using Slick. I think that is the best solution.

I would want a state for the main menu, options screen, each level, game over and the ending.

I'm pretty new to developing games. I currently only have one "level" and I'm accessing it like this


public class StateHub {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new LevelOne());
frame.setVisible(true);
frame.setSize(800, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


For my last game I had a huge series of booleans changing what was drawn to the panel, it got unmanageable fast. Just trying to clean things up. Is there a simple way to turn this into a state-like system where I could do something like destroy the instance of LevelOne and add LevelTwo instead? Is there an easier solution?

Thanks a ton.
Advertisement
Hello
How about a finite state machine (FSM)? Each machine can have some states defined by you, and each state will have an action to perform.

You want to have "main menu, options screen, each level, game over and the ending", so you can use multiple state machines. So I would have 2 state machines:

screen machine - have the following states: main menu state, options state, game over state, ending state, gameplay state
gameplay machine - this is a state machine inside a state machine, only active when screen machine is in gameplay state. this will have level1 state, level2 state, ...

What do you think of that? ;o
Very cool, I tried reading about it on google before I even posted here but I wasn't entirely sure that was what I wanted. The machine term threw me off. I'll work on it some more now, thank you.

This topic is closed to new replies.

Advertisement