Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Dividing my game into classes?  (Read 1171 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Posted 2012-01-11 05:18:11 »

So, my game is starting to get a bit cluttered and I've been thinking about trying to divide parts into separate classes, and I must admit that my inexperience with Java is catching up to me.  I have Character and Projectile classes, but what I would like to have is an InGame class, Menu class, and LevelLoader class which are currently methods inside the main class.  The problems that I don't already know the answers to:

-The levelLoader methods create images which are declared in the main declaration section, and are used in the main loop run() for inGame purposes.  Can Images/any variable or object be passed as arguments as not read only, or can I access the parrent class's global variables some how?

-The Menu method shares the same keyboard and mouse listeners as the in game code.  I'm assuming I can have a Menu class with mouse and keyboard listeners, I don't know if I need to remove the in game listeners before adding listeners in the Menu class, or the proper way to do this.  Also, the update() and paint() methods check to see what to draw (game/menu) and does so accordingly.  If I want the them to have separate drawing methods how would I do that?

Thanks in advance.

Offline Fokusas

Senior Member


Medals: 3
Projects: 1



« Reply #1 - Posted 2012-01-11 08:15:52 »

I think you should read this http://docs.oracle.com/javase/tutorial/java/concepts/index.html
http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
-The levelLoader methods create images which are declared in the main declaration section, and are used in the main loop run() for inGame purposes.  Can Images/any variable or object be passed as arguments as not read only, or can I access the parrent class's global variables some how?
YES! Cheesy
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-01-11 23:04:12 »

How about making a state-based machine? Each "state" is a different screen, such as the Menu, InGame, GameOver, etc...

I found this older post that should explain how this works, and this is what your Screen/State class should look like.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #3 - Posted 2012-01-12 05:05:06 »

@ra4king, this looks like what I'm looking for, but now I'm even more confused. 
What I think I understand now:
-The idea behind this is I can have my main class create a couple of states, and depending on what the current state in use is, I can call the appropriate states identical method.   My update() method of the main class can call the update() method of the menu class. 
-I only need to have listeners in my main class then and then have corresponding methods in the states.

I'm not 100% sure why I need to use interfaces,  I assume it's for simplicity.  And how would I share variables between these?

Offline aazimon
« Reply #4 - Posted 2012-01-12 17:25:36 »

Your main class would have a State variable, that your game loop calls the update on. Then when you change state, you replace the variable in main with the new state class. Kind of like this:
(pseudo code)
1  
2  
3  
4  
5  
6  
7  
changeState() {
 if (mainmenu) {
  state = mainMenuObject;
}
else if (game) {
  state = gameOjbect;
}
Offline tackman

Senior Newbie





« Reply #5 - Posted 2012-01-12 19:59:38 »

This is how I would do it, something that Android does good with states:

I would have a constant integer for the states, like:
1  
2  
3  
4  
5  
const int MAINMENUSTATE = 1;
const int GAMEPLAYSTATE = 2;
const int HIGHSCORESTATE = 3;

int currentState = MAINMENUSTATE;


Then whenever I want to switch between the states, I just call a method, something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
changeStates(int state) {
switch(state) {
 case MAINMENUSTATE:
     someFunctionOrClassThing();
     break;
 case GAMEPLAYSTATE:
     youGetTheIdea();
     break;
 case HIGHSCORESTATE:
     whyDidIMakeThreeStates();
     break;
 default:
     meh();
 }
}


And then just handle all of the necessary codes in their respective classes / functions.
Offline JayTech

Junior Member


Medals: 1



« Reply #6 - Posted 2012-01-12 21:54:00 »

Lvl Class is a Jpanel object load all images here;

Main program- creates a jframe and loads a new lvlclass();

Sprite class--hold all data about the sprite coordinates etc mouse events.

etc etc, you want everything to be object orienated to be able to reuse instead of a cluster f**k program in  one class, you need to divide everything logically.

And just create threads for updating and repainting which get called when the Lvlclass gets painted to the MainFrame program.


Main Game Class- is a frame
             _
Sprite        Lvl class-panel    projectile class extends sprite class
 -                  -
 _   _            


Pretty much make it like that IMO, hope that helps.
Offline cylab

JGO Ninja


Medals: 43



« Reply #7 - Posted 2012-01-13 09:34:27 »

I'm not 100% sure why I need to use interfaces,  I assume it's for simplicity. 
You need some common type to assign a screen to a variable, so you need to extend from a base class or implement a common interface.

Use case is something like that:
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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
// Available Screens
MainMenu mainMenu = new MainMenu(game);
Highscore highscore = new Highscore(game);
Level level1= new Level1(game);
Level level2= new Level2(game);
...
Level levelN= new LevelN(game);

// The screen currently shown to the user
Screen currentlyShowing;

...

// Switching screens because of some useraction or whatever
switch(nextScreen) {
 case MAINMENU:
     currentlyShowing=mainMenu;
     // You will need some kind of input delegation thingy, that calls keyPressed() or something on the screens
    inputManager.delegateInputTo(mainMenu);
     break;
 case LEVEL1:
     currentlyShowing=level1;
     inputManager.delegateInputTo(level1);
     break;
 case LEVEL2:
     currentlyShowing=level2;
     inputManager.delegateInputTo(level2);
     break;
 case LEVELN:
     currentlyShowing=levelN;
     inputManager.delegateInputTo(levelN);
     break;
 case HIGHSCORE:
     currentlyShowing=highscore;
     inputManager.delegateInputTo(highscore);
     break;
 }
}

...

// Your gameloop
while(notExited)
{
    // you might want to pass the gametime to methods, too
   currentlyShowing.update();                    
    currentlyShowing.render(renderTarget);
    // your sync-method shoud sync to some fixed fps
   sync();                                                  
}


And how would I share variables between these?
Thats the purpose of passing game to each screen, so you can read out and manipulate common variables contained in the game class.

Mathias - I Know What [you] Did Last Summer!
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.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (68 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!