• Advertisement
Sign in to follow this  

How should I implement a state based game?

This topic is 1904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

[CODE]
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);
}
}
[/CODE]

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement