Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Implementing Menu's For Games  (Read 2663 times)
0 Members and 1 Guest are viewing this topic.
Offline happybirthday

Senior Newbie





« Posted 2011-06-07 09:43:40 »

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 Smiley
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-06-07 10:20:01 »

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.

Offline happybirthday

Senior Newbie





« Reply #2 - Posted 2011-06-07 10:42:46 »

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 ?  Clueless
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-06-07 22:28:59 »

Inside the game loop itself:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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);
    }
}

Offline ReBirth
« Reply #4 - Posted 2011-06-08 04:24:11 »

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.

Offline happybirthday

Senior Newbie





« Reply #5 - Posted 2011-06-08 07:51:55 »

Inside the game loop itself:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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);
    }
}



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
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-06-08 08:17:58 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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.

Offline ReBirth
« Reply #7 - Posted 2011-06-09 10:23:09 »

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 Smiley

Offline happybirthday

Senior Newbie





« Reply #8 - Posted 2011-06-12 17:06:36 »

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/topics/best-way-to-organize-screen-states-in-the-code/15460/view.html

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

Please.

Thanks alot guys Smiley I am already starting to get somewhere Smiley
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-06-12 22:25:26 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2011-06-13 04:00:34 »

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

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-06-13 05:16:58 »

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.

Offline ReBirth
« Reply #12 - Posted 2011-06-14 05:35:05 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (38 views)
2014-07-18 06:55:21

Zero Volt (36 views)
2014-07-17 23:47:54

danieldean (29 views)
2014-07-17 23:41:23

MustardPeter (32 views)
2014-07-16 23:30:00

Cero (47 views)
2014-07-16 00:42:17

Riven (48 views)
2014-07-14 18:02:53

OpenGLShaders (37 views)
2014-07-14 16:23:47

Riven (37 views)
2014-07-14 11:51:35

quew8 (33 views)
2014-07-13 13:57:52

SHC (70 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!