The start point of making game with GTGE is by subclassing Game
class that reside in com.golden.gamedev
package, or in other words every game is subclass of Game
class.
There are 3 tasks that must to do of every subclass of Game
class :
- Game Variables Initialization
In this first task, subclass ofGame
class need to initialize game variables.
For example: initialize sprite, background, etc.
Note:Game
class is prohibited to have overloading constructor, therefore put all that usually belong in constructor in this method. - Game Update
Updating the game variables.
For example: updating sprite location, animate the game, etc. - Game Render
Render the game to the screen.
For example: render game objects, sprite, background, etc.
class :: Game Syntax: public abstract void initResources(); public abstract update(long elapsedTime); public abstract render(Graphics2D g); whereas : elapsedTime = time elapsed since last update g = graphics object to render the game
Those 3 abstract methods are passed in this sequence :
Now let we see the proper way to subclass Game
class :
(as has been explain before, to use a class in a package use import
keyword).
Tutorial4.java [view online]
file :: YourGame.java // GTGE API import com.golden.gamedev.Game; // Java Foundation Classes (JFC) import java.awt.Graphics2D; public class YourGame extends Game { public void initResources() { // initialization of game variables } public void update(long elapsedTime) { // updating the game variables } public void render(Graphics2D g) { // rendering to the screen } }This is the blue prints or the skeleton of *every* game.
Now as we have know how to code the game skeleton, let's try to run it.
In chapter 2 we know the first thing to do to run Java application is compile the source code :
Compiling YourGame.java.... Success! YourGame.java is converted to YourGame.class bytecode.
Next step is run the bytecode : Running YourGame.class.... Failed!!! What's wrong??
Every Java application starts from this function :
public static void main(String[] args) { // application start point }And because this function is not exists in the game skeleton we code above, Java could not run the game.
On next chapter will be explained how to run the game skeleton.