Game States

From Slick2D Wiki
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[edit]

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) {

    // 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());
        this.addState(new MainMenu());
        this.addState(new 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);
        } catch(SlickException e) {

Creating Game States[edit]

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.

This in an example of the main menu that got called with this.addState(new MainMenu());:

public class MainMenu extends BasicGameState {

	// ID we return to class 'Application'
	public static final int ID = 0;

	// init-method for initializing all resources
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

	// render-method for all the things happening on-screen
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

	// update-method with all the magic happening in it
	public void update(GameContainer gc, StateBasedGame sbg, int arg2) throws SlickException {

	// Returning 'ID' from class 'MainMenu'
	public int getID() {
		return MainMenu.ID;