Tutorial 4

STARTING THE GAME

Time to write some code, better prepare yourself.... :-)
This chapter will explain how to start making a new game (in source code), what class to use, code to type, etc.
Objective : Understand what code to type to start a new game.
collapse/expand

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.

Every game is a 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 of Game 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 :
Game 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.


Summary :

  • Every game is subclass of Game class.
  • Subclass of Game class need to do 3 tasks, game variables initialization, game update, and game render.
  • In variables initialization, put all that usually belong to constructor.
  • In game update, update the game variables to animate the game.
  • In game render, render the game objects to the screen.

Reference : Game class

Copyright © 2003-2005 Golden T Studios. All rights reserved. Use is subject to license terms.
GoldenStudios.or.id
Page 4 of 12