Implementing Menu's For Games

Hello everyone,

I am in the process of making a Java Applet Game …

My problem is that I am not able to understand how to implement a Menu System for a Java Game.

I want an Animated Background for my Menu Page with all the buttons and all. And when the player hits the “Play” button the main game loop starts …

But I am getting confused that since an Applet has just one run() function for a thread how can I use it both for animations for my Menu System and also for the Main Game ?

I know it sounds a bit confusing … So all i want to know is how do we implement a Menu System in a game ? (specially one in which you need animated backgrounds and buttons)

Thanks a lot :slight_smile:

You could have a Menu class that has all the logic and drawing there. Your applet would use a boolean to store the state: true for menus, false for game. Your game loop would be constantly running but if your boolean is true, it would call the Menu’s update and draw methods, else it would call the actual game’s update and draw methods.

and where do i check for this game state Boolean value ? Inside the run() function or the update() or the paint() function ? :clue:

Inside the game loop itself:


public void gameLoop() {
   while(isRunning) {
        if(isMenus)
            menus.update();
        else
            game.update();
        
        Graphics2D g = get Graphics context
        if(isMenus)
            menus.draw(g);
        else
            game.draw(g);
    }
}

Or merge menus and game classes into same kind by implementing same interface. Create one more classes to put current state and update it like usual.

So basically … what i do is:

  1. Create different Runnables for the different States of a Game.
  2. Have one main Runnable whose only Job is to find out which State the Game is currently and execute its methods

right ? :persecutioncomplex:

Why different Runnables? You only need 1 game loop and it delegates the calls to the appropriate screen’s update and draw methods according to the current state.
Full class for more clarification:


public class MyApplet extends Applet implements Runnable {
    private Game game;
    private Menus menus;
    private boolean isMenus;
    
    public void init() {
        //init applet
    }
    
    public void start() {
        new Thread(this).start();
    }
    
    public void run() {
        while(isRunning) {
            if(isMenus)
                menus.update();
            else
                game.update();
            
            Graphic2D g = get graphics context
            if(isMenus)
                menus.draw(g);
            else
                game.draw(g);
            
            //Sleep
        }
    }
}

That’s it, only 1 thread should be running and that is your game loop. Later on you can use ReBirth’s suggestion and create an interface (I call it Screen) that merges the Game and Menus together through common methods like update() and draw(Graphics2D). Then your game applet could just hold the reference of the current Screen and call its methods. In fact my game library does exactly that if you want to look.

I had same thinking way like this back there before I read Glass’s tutorial
http://www.cokeandcode.com/node/315

So you have ArrayList to hold all entities that you want to update-draw each loop. When you want to plug a menu, where all entities (animation of menus, credits etc) are displayed, you create one more ArrayList to hold they all but just in different class. We do this because both entities ArrayLists are update-drawed separately not at same time. Hope help :slight_smile:

Thanx Re-Birth …

but I found this old topic in which the original post talks about creating Abstract Classes and extending the Game States with this Abstract Class

http://www.java-gaming.org/index.php/topic,15460.0.html

Can anyone explain me in detail how this method can be used.

Please.

Thanks alot guys :slight_smile: I am already starting to get somewhere :slight_smile:

That post explains the same exact method ReBirth and I were talking about and it’s the one my library uses except I called it a Screen instead of a State. The Screen interface has an update and a draw method. Your gameloop just calls the methods of the current Screen that is shown.
Here’s my Screen interface: http://code.google.com/p/java-game-utils/source/browse/src/gameutils/Screen.java

On that thread, he (or she maybe?) suggested to extend State class while me and ra4king make it an interface. You shouldknow the difference between them. Interface more strict, you must have every methods and attributes on that Interface class on the implemented class. That’s way you wont forget to add important things such as update and draw/render method :wink:

Interface makes more sense because you have to think about this way: extending a class suggests being a child of, or literally, extending its functionality and all classes that extend this class actually have something in common while implementing an interface suggests an is-a relationship where that class IS a Screen or IS a State. Classes that implement an interface may have nothing in common at all.

Extending is good when your state is identical so the super class can handle whatever which is same between those states.