GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Animated actors

In many games, actors not only move, but also change their appearance while moving. The player has to feel like the actor of the game is eating, swimming, running, etc. To realize this, it is possible to asign multiple Sprites to one actor. The number of Sprites is declared in the initialization parameter. If nothing spezial is declared, each Sprite is called by its idSptire in an increasing order after each simulation cycle. If the actor is rotatable, all its Sprites will rotate automatically.

Example 1: The actor packman has two sprites asigned which are always shwon one after the other.


Run this example

Edit this example in the Online-Editor

 

// JGameEx8a.java

import ch.aplu.jgamegrid.*;

public class JGameEx8a extends GameGrid
{
  public JGameEx8a()
  {
    super(404015);
    addActor(new Pacman(), new Location(28, 10));
    show();
  }

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

// ------------  class Pacman -----------------------
class Pacman extends Actor
{
  public Pacman()
  {
    super(true"sprites/pacman.gif"2);
  }

  public void act()
  {
    move();
    if (getNbCycles() % 20 == 0)
      turn(90);
    showNextSprite();
  }
}

Explaing the program code:
super("sprites/packman.gif", 2); The number of Sprites is declared in the initialization parameter. If the path to the Sprites is packman.gif,, the two pictures have to have the name packman_0.gif and packman_1.gif and need to be placed inside the directory sprites
if (idSprite == 0)
   idSprite = 1
else
   idSprite = 0
show(idSprite)

The two sprites are shown one after the other

 

Example 2: In this example the actor has 3 differnt Sprites asigned, which are shown in a periodical order. Smooth movements of the actors can be achieved when working inside a grid with a cell size of 1 pixel.

The head moves left and right while its phisique changes.

Run this example

Edit this example in the Online-Editor

 

// JGameEx8.java

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

public class JGameEx8 extends GameGrid
{
  public JGameEx8()
  {
    super(6006001);
    setBgColor(Color.darkGray);
    addActor(new Head()new Location(136200));
    show();
  }

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

// ------------  class Head -----------------------
class Head extends Actor
{
  private double dir = 30;

  public Head()
  {
    super("sprites/head.gif"3);
    setDirection(dir);
  }

  public void act()
  {
    showNextSprite();
    if (getX() > 450)
    {
      setDirection(180 + dir);
    }
    if (getX() < 138)
    {
      setDirection(dir);
    }
    move();
  }
}

Explaining the program code:
super("sprites/head.gif", 3) The 3 Sprites head_0.gif, head_1.gif and head_2.gif need to be saved inside the directory sprites
showNextSprite() simplifies the run through of the Sprites

This program uses Sprites from the program distribution of the book "Brackeen, Developing Games in Java".