GameGrid: Game programming with Java

Research project PHBern  

Rotatable Sprites

In some applications it is necessary that the actor looks the same was as its direction. The class libraray JGameGrid takes care of this difficult task. Adding the parameter true to the declaration of the actor, its direction is recalculated and updated after each step.


By default the parameter is set to false, because the recalculation of the actor's position uses up a lot of processing speed. Rotoable Sprites should therefor only be used for pixel games (cell size is set to 1 pixel). Working with grid games actors should be rotated with the methods setHorzMirror(true) or setVertMirror(true).

Example 1: The car moves on a circular path. Its speed can be controlled with the speed slider of the navigation bar.

Run this example

Edit this example in the Online-Editor



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

public class JGameEx4 extends GameGrid
  public JGameEx4()
    RedCar car = new RedCar();
    addActor(car, new Location(300100));
  public static void main(String[] args)
    new JGameEx4();

// ------------- class RedCar --------------------
class RedCar extends Actor
  public RedCar()

  public void act()
Explaining the program code:
super(true,"sprites/redCar.gif"); The parameter true causes the program to constantly recalculate and update the Sprite's direction.
super(600, 600, 1) Creates a game window of 600x600 pixel with a cell size of 1 pixel. Grid lines are hidden and no background picture is set
getBg().clear(Color.darkGray) The background is described with the class GGBackground. The method getBg() returns the instance of the background and clear() overwrites the background color
move() In a window bigger than 2500 pixal, move() moves the actor 5 pixel
turn(1.5) During one simulation periode a rotation of 1.5 degrees is achieved