Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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  
  Question about Design  (Read 411 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Posted 2013-03-22 19:50:37 »

Hey, I'm currently re-writing all of the code for my game once again in an attempt to start doing things correctly. I'd like to ask a question about how the framework of a game should be set up, I know this is pretty basic but I've messed up on this a few times.

Example Game Structure:
http://imgur.com/cORcu9x

Questions:
  • Is the diagram that I linked to above correct, if not what should be changed?
  • Should the game loop call a method in a different class that then gets all of the changes for every single thing and sends it off to all of the other classes that require variables or is there a better way to do it?
  • Should I have separate classes for the player, sprites, map, etc...?
Offline opiop65

Senior Member


Medals: 4


Nishu Studios Dev


« Reply #1 - Posted 2013-03-22 19:57:18 »

I always have a interface that all my classes extends; it holds a update and render method. All my classes have the ability to render or update something. I then call the update and render methods in my main gameloop. Graphics are rendered and then I update the positions before doing it all over again. I would be against your 2nd bullet point, I think your code would get way to dependent on its self, and if you change one thing, you'll screw it all up. I would look into OOP, I think games really benefit from it. You are also correct on your 3rd point, you need to make separate classes for everything. You need a base sprite class for all of your sprites to have one common place to operate from. So, your Player class would extend Sprite, your Enemy class would extend Sprite, etc. If you try to throw everything into the same class, you will again clutter your classes up way to much, and I guarantee you you will have some very buggy code on your hands!

No one ever fully understands coding
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« Reply #2 - Posted 2013-03-22 20:10:17 »

1. Updating is one action. Rendering is one action. Why are there four parts to your loop?
2. Objects should collect the variables themselves unless it is a non-field variable. I do not mean to make EVERYTHING a field. Work out whether to pass the variable or collect it AFTER you decide whether it's a field or not.
3. Java is OOP. The whole point is to have loads of classes (within reason) to make it easier to code.

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Reply #3 - Posted 2013-03-22 20:18:06 »

1. Updating is one action. Rendering is one action. Why are there four parts to your loop?
2. Objects should collect the variables themselves unless it is a non-field variable. I do not mean to make EVERYTHING a field. Work out whether to pass the variable or collect it AFTER you decide whether it's a field or not.
3. Java is OOP. The whole point is to have loads of classes (within reason) to make it easier to code.

It's actually just updating/rendering, the two extra parts are just descriptive text but I guess they looked more like their own step. I'm somewhat bad at drawing diagrams.  Tongue

Thanks for the replies.
Offline ctomni231

JGO Knight


Medals: 28
Projects: 1


Not a glitch. Just have a lil' pixelexia...


« Reply #4 - Posted 2013-03-22 20:31:01 »

Just a quick suggestion.

I think that your diagram is spot on. When I build my code, I usually do all the updating first followed by the rendering. Splitting up everything into classes helps immensely in that regard. If you give all your objects and scenes an update function. It'll give you a lot of control on which order they are rendered, and what appears above and below certain details on the screen.

Remember, your code is supposed to help you design easier. So, always strive to do what you are comfortable with. (You'll probably learn good OOP eventually.) The basic rule is, the more that you split up your logic code from your graphic code, the easier it'll be to control things graphically within the code. Control is really all your after, so keep that in mind as you make your decisions.
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2013-03-22 22:40:33 »

I just thought up another question to ask after I did a few tests with inheritance which I've never used before. If I have a base Sprite class and all of the other Sprite classes inherit from it, what should I put in the base Sprite class?
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« Reply #6 - Posted 2013-03-22 22:49:14 »

I just thought up another question to ask after I did a few tests with inheritance which I've never used before. If I have a base Sprite class and all of the other Sprite classes inherit from it, what should I put in the base Sprite class?

Everything that applies to all sprites. (eg: position, tex coords, velocity, life, etc.)

Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Reply #7 - Posted 2013-03-22 23:12:23 »

Everything that applies to all sprites. (eg: position, tex coords, velocity, life, etc.)

I did a test to see if I could do this before I asked. I made two classes, one was Entity and the other was Player, and in the Entity class I created a variable 'public int testInt = 1;'; after that I had Player extend Sprite and then made a constructor method for each class, in both constructors was the statement 'System.out.println(tempInt);'. When I tested the classes by calling both of the constructors they both printed 1 to the console. Because of this I thought that it wouldn't be possible to use a Sprite class for something such as holding all of the variables.

The way that I thought it would work is if Player extended Entity then player would create it's own instances of all of the public variables in the Entity class without me having to specificly create them.
Offline Agro
« Reply #8 - Posted 2013-03-22 23:12:34 »

I do something similar to you guys: have an interface called Active that has basic things like update and render. Entity class implements that so all things can be updated and rendered.

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (94 views)
2013-05-17 21:29:12

alaslipknot (102 views)
2013-05-16 21:24:48

gouessej (132 views)
2013-05-16 00:53:38

gouessej (127 views)
2013-05-16 00:17:58

theagentd (138 views)
2013-05-15 15:01:13

theagentd (126 views)
2013-05-15 15:00:54

StreetDoggy (168 views)
2013-05-14 15:56:26

kutucuk (189 views)
2013-05-12 17:10:36

kutucuk (192 views)
2013-05-12 15:36:09

UnluckyDevil (199 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.091 seconds with 21 queries.