GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

The game window (game grid)


The most important classes of the class library JGameGrid are GameGrid and Actor. With the help of GameGrid the game window is build and shown. All moving characters and objects shown in the game grid are actors.

The class GameGrid provides a graphical window. The window is set up with a grid containing a certain amount of horizontal and vertical aligned square cells. The cell size and the number of horizontal and vertical cells can be defined by the user. Some applications are favorably created with big cells (grid games), others with small cells with the size of only one pixel (pixel games). To position something inside the game grid, cell coordinates are used.

In most examples, the application class is derived from the class GameGrid with the code extends GameGrid. This way all methods of the class GameGrid can be used immediately. Calling super() the constructor of GameGrid is initialized. This example uses the standard template MyGameGrid.java from the Online-Editor. The navigation bar containing the buttons Step, Run, Reset and the speed slider do not have any purpose in this example. Its functions are described in the next turtorial Actors.

Run this example

// MyGameGrid.java

import ch.aplu.jgamegrid.*;
import java.awt.Color;

public class MyGameGrid extends GameGrid
{
  public MyGameGrid()
  {
    super(101060Color.red);
    show();
  }

  public static void main(String[] args)
  {
    new MyGameGrid();
  }
}

 

 
Explaining the program code:
public class ActorEx1 extends GameGrid The application class derives from the class GameGrid and inherits all methods (not private methods)
super(10, 10, 60, Color.red) Initializes the game window with 10 horizontal and 10 vertical cells, cell size is 60 pixel and the grid lines are red
show() The game window only shows up if the method show() is called

The constructor of the class GameGrid, which is used for the initialiazion of the game window is loaded several times.

Own examples:

super(20, 20, 30, Color.green);   super(10, 10, 60, Color.red,  "sprites/reef.gif");
Window with 20x20 cells (size: 30 pixel), green grid lines   The fourth parameter contains the path of the background image (view...). The image must have the size 601x601 pixel (more...).


 
     
super(10, 10, 60Color.red, "sprites/reef.gif", false);   super(600, 600, 1, null"sprites/town.jpg", false);
The 6. parameter false hides the navigation bar.   The fourth parameter null hides the grid lines.
 

Own background images can be downloaded from the picture library. It is also possible to use your own pictures.(more...).