Structure of a game framework?

Started by
1 comment, last by DanDannyPantry 12 years ago
Hey,

I wanted to ask if there are any good ressources on how to structure a basic game framework.

I am not completely new to game programming, but all my games tend to have the same "ugly" structure, i.e:

-Player Class (which holds the attributes for the player, i.e. health, movementspeed, position,...)
-Enemy Class (almost the same as player, only with some sort of an AI)
-Game Class (extended from a jpanel, has an instance of player and a collection of enemies, handles input, drawing, looping, etc.)

My problem here is, that the "Game" class becomes bloated rather fast and with that it gets more and more difficult to add things to it.

So, are there any ressources you can recommend to look at, for a decent game framework structure?
Or can you give me some hints and tips on how to structure my games better? How to separate Input from Drawing and everything else?

PS: I doesn't need to be specifically for Java, I would appreciate it though.
Advertisement
Put stuff in classes and functions if you use it more than once.

A class should only handle 1 thing and you should try to keep dependencies between classes at minimum. If 2 classes need same functionality make a class with that functionality and make the 2 classes inherit from that.

o3o


Hey,

I wanted to ask if there are any good ressources on how to structure a basic game framework.

I am not completely new to game programming, but all my games tend to have the same "ugly" structure, i.e:

-Player Class (which holds the attributes for the player, i.e. health, movementspeed, position,...)
-Enemy Class (almost the same as player, only with some sort of an AI)
-Game Class (extended from a jpanel, has an instance of player and a collection of enemies, handles input, drawing, looping, etc.)

My problem here is, that the "Game" class becomes bloated rather fast and with that it gets more and more difficult to add things to it.

So, are there any ressources you can recommend to look at, for a decent game framework structure?
Or can you give me some hints and tips on how to structure my games better? How to separate Input from Drawing and everything else?

PS: I doesn't need to be specifically for Java, I would appreciate it though.

First of all you should use inheritance. Your Enemy and Player classes should extend from a common [abstract] class, seeing as how they are so similar. You could call this "Entity" or "Object". You could create interfaces to support the AI which individual extensions of the class (ie, WolfEnemy) could inherit and edit to their liking.

As for keeping the input/logic and display seperate, you're going to want to use threads and java's concurrency packages. You could use a CyclicBarrier to signal the display thread to start rendering, and then reset it when you're done rendering so the input/logic thread can Sleep and then run a new iteration. Display and Logic/Input are generally handled on seperate threads in bigger games, *however* if you are doing something simple like Pong, or something that doesn't have very intensive math calculations, you could just stick Display and Logic/input on the same thread to simplify things (do *not* do this in a team setting) while you get the hang of things.

A few things for you to look at:
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
http://en.wikipedia.org/wiki/Object-oriented_programming

This topic is closed to new replies.

Advertisement