This is the 22nd day of my participation in Gwen Challenge

preface

This chapter uses the built-in StateBasedGame class of the Slick2D engine to write a simple mini-game as an example, covering basic concepts such as frame-by-frame redrawing, data updating, and the application of some of the techniques that will be used throughout development.

The basic technology

The idea of StateBasedGame is that a game has several states, each of which is like a BasicGame, and you can switch between them. Thus, a StateBase game no longer has update, render, and so on itself (which later appear in each State), but only main and initStatesList methods. For example, the code framework is as follows:

public class SetupClass extends StateBasedGame {
    public SetupClass(String title) {
        super(title);
    }
    public static void main(String[] args) throws SlickException {
        // ...
    }
    public void initStatesList(GameContainer gc) throws SlickException {}}Copy the code

The GameState contained in each StateBase Game will be initialized in initStatesList. Each GameState is an object based on a subclass of the BasicGameState class. It needs to implement at least one of these methods: getID, init, Update, render. Code framework:

public class GameState extends BasicGameState {
    public void init(GameContainer gc, StateBasedGame game) 
            throws SlickException {}public void update(GameContainer gc, StateBasedGame game, int delta) 
            throws SlickException {}public void render(GameContainer gc, StateBasedGame game, Graphics g) 
            throws SlickException {}public int getID(a) {
        return 0; }}Copy the code

The return value of the getID method is the ID of the State, and each State must have a different ID in order to switch between states.

Once we have created a State class (called GameState in this example), we can add the State to initStatesList:

this.addState(new GameState());
Copy the code

Multiple states can also be added, and the game loads the first State by default. If you want to switch between States, you should execute the following code in the corresponding State update function:

game.enterState(1);
Copy the code

The above code causes the game to switch to state with ID 1.

In the game, when we switch to a new State, the old State is suspended and its update and render functions are no longer called. This is crucial, as it is easy to create a “pause” State, for example, and then switch to that State to pause the game.

Switch State

We can also use an animation to switch between different states: game.EnterState (1, new FadeOutTransition(), New FadeInTransition()); The first parameter is the ID of the target State, and the last two are the transition effects of leaving and entering.

Here we’ll have the general structure of the slick2d, for Java program ape, the game engine is very simple, very easy to use, but slick2d haven’t updated for almost 10 years, for the interest want to play, can be used to to with the development of commercial game is about to consider other engines.

In the next chapter we will look at developing a small game demo using Slick2D.