Game States

From Slick2D Wiki
Revision as of 15:37, 25 December 2013 by Samich (Talk | contribs) (Getting Started)

Jump to: navigation, search

Game states can be useful for loading in company/personal logo splash screens, as well as providing a useful way to maintain different parts of your game. This isn't normally necessary to do, but can be useful for separating all your main menu content from the actual game content.

Note that if you do plan on utilizing Basic Game State's, you should use them sparsely as they are beyond necessary in most cases.

Getting Started

First thing you will need to do, is set up your application's main gamestate loader. There are many ways to do this, but most examples will follow the same criteria.

Example of a Main Class:

public class Application extends StateBasedGame {

    // Game state identifiers
    public static final int SPLASHSCREEN = 0;
    public static final int MAINMENU     = 1;
    public static final int GAME         = 2;

    // Application Properties
    public static final int WIDTH   = 640;
    public static final int HEIGHT  = 480;
    public static final int FPS     = 60;
    public static final double VERSION = 1.0;

    // Class Constructor
    public Application(String appName) {
        super(appName);
    }

    // Initialize your game states (calls init method of each gamestate, and set's the state ID)
    public void initStatesList(GameContainer gc) throws SlickException {
        // The first state added will be the one that is loaded first, when the application is launched
        this.addState(new SplashScreen(SPLASHSCREEN));
        this.addState(new MainMenu(MAINMENU));
        this.addState(new Game(GAME));
    }

    // Main Method
    public static void main(String[] args) {
        try {
            AppGameContainer app = new AppGameContainer(new Application("My Game v" + VERSION));
            app.setDisplayMode(WIDTH, HEIGHT, false);
            app.setTargetFrameRate(FPS);
            app.setShowFPS(true);
            app.start();
        } catch(SlickException e) {
            e.printStackTrace();
        }
    }
}

Creating Game States

After you've accomplished the above and configured it to your liking, along with how many game states you will require, you can begin making your states that you will be working with.

~Will be added soon~