简体   繁体   中英

Different Game States in Java

I'm trying to make a game in Java - just a simple one for now. What is the best way to create different game states? Should I have a seperate class for each state, each with a paint() method? Also, how should I switch between the states?

Can't comment exactly with Java, but the way I've done state handling in the past is with a separate class for each state. They all inherit from a base state and are managed by a stack.

In pseudo-c# -

abstract class GameState
{
    public bool BlockDraw = true;
    public bool BlockUpdate = true;

    abstract void Update();
    abstract void Draw();
}

Each state can block updates and draws of states below.

public class GameStateManager
{
    public LinkedList<GameState> States;

    public void Update()
    {
        // Update the top state
        Update(States.Top);
    }

    private void Update(Node<GameState> state)
    {
        state.Value.Update();

        // Move down the stack if the current state is not blocking
        if (!state.Value.BlockUpdate)
        {
            Update(state.Next);
        }
    }

    public void Draw()
    {
        Draw(States.Top);
    }

    private void Draw(Node<GameState> state)
    {
        state.Value.Draw();

        if (!state.Value.BlockDraw)
        {
            Draw(state.Next);
        }
    }
}

The idea in the manager is simply that the top state gets updated or drawn. Then, recursively, it steps down the stack if the current state is not blocking. This way you can pop up a pause state to block updates, but not draws (say you want to have the game partially visible behind the pause window). Or another pop up window will block draws, but not updates - this allows the game to continue but save on draw time.

You push and pop states onto the stack, so at play time it could look like this:

  • PlayState
  • TitleMenuState
  • OpeningScreenState

PlayState would block draws and input to everything below it. Say the user pauses, you push on a PauseState that blocks update below it. If they exit to menu, you pop down to the TitleMenuState.

I would suggest using an Enum to hold the states and some StateManager-class, which keeps the state of your game. You can then easily use a switch -statement to check different states and let every state have its own draw and update methods.

The StateManager should be accessable at all places, because you want to ask for the game state a lot of times probably: if(paused || gameOver) .

The most elegant way is to create an enum state machine . There is only one instance of each state in JVM, and it's for free. On the other hand here is more conventional way of implementing state machine. I would recommend first approach, the second is more "old school", however it runs and it is commonly used.

I would not recommend doing this yourself. There are game engines written that support this amongst many other features. Unless you are doing this for educational purposes and want to build your own game engine. If your goal however is to make a game, then you would be best served by using a third party library.

I use Java Slick . It is a wrapper around LWJGL and provides plenty of features. Game states amongst them. It is easy to use, free, and the author of it is active on the forums and provides help as well as bug fixes. I recommend it.

Here is how game states work in slick And here are some tutorials showing you how to make complete games:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM