Preface

This chapter is the beginning of "Game Objects Tutorial". Before we dig further in this tutorial, to make you understand about this tutorial better, we will give you some intro about how actually GTGE manage objects in game.

GTGE manage sprites in three approach :
(as for the higher approach, the easier it will be to manage complex condition)

  • The first approach is where sprite is directly used in the game, each sprite must be updated, rendered into screen one by one.
  • The second approach is where all the sprites are grouped into groups, and only those groups that need to be updated, and rendered to screen one by one.
  • The last approach is where all sprites, that have been grouped into groups, are put into a big play field. In this play field, all things are working automatically (the updating, rendering, collision, removing, etc).

This tutorial will explaining each approach step by step. Alright, let's start!

Note : Every code examples in this tutorial are assumed inside game class.

Tutorial 7

SPRITE

Sprite.java [view online]
This chapter will explain what is sprite, when to use it, how to initialize it, and how to use it in game.
Objective : Understand sprite description and how to use it.
collapse/expand

Sprite Description

Sprite, in GTGE terminology, is used to describe every objects in game that has an image and has its own characteristic.

You could imagine a sprite as an image that located somewhere on the screen, whereas this image could be moving, animated, and has its own characteristic.

Sprite, in GTGE, is represented by Sprite class that reside in com.golden.gamedev.object package.

Sprite Initialization

Tutorial7_1.java [view online]

Initialization of a sprite is as followed :

  • Create new sprite object :
    class :: Sprite
    
    Syntax:
       public Sprite(BufferedImage image,
                     double x,
                     double y);
    
    whereas :
    image = the sprite image
    x     = the sprite x coordinate
    y     = the sprite y coordinate
    
    
    For example:
    create a sprite with image "player.png" and located on 100, 200 in screen
    
       BufferedImage image = getImage("player.png");
       double x = 100;
       double y = 200;
    
       Sprite hero = new Sprite(image, x, y);
    
  • Updating the sprite :
    class :: Sprite
    
    Syntax:
       public void update(long elapsedTime);
    
    whereas :
    elapsedTime = time elapsed since last update
                  this value is taken from Game class
    
  • Rendering the sprite into screen :
    class :: Sprite
    
    Syntax:
       public void render(Graphics2D g);
    
    whereas :
    g = graphics object where the game is rendered
        this value is taken from Game class too
    

Example :

file :: YourGame.java

// JFC
import java.awt.*;
import java.awt.image.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;


public class YourGame extends Game {


    Sprite  hero;


 /****************************************************************************/
 /**************************** GAME SKELETON *********************************/
 /****************************************************************************/

    public void initResources() {
        BufferedImage image = getImage("player.png");
        double x = 100;
        double y = 200;

        hero = new Sprite(image, x, y);
    }


    public void update(long elapsedTime) {
        hero.update(elapsedTime);
    }


    public void render(Graphics2D g) {
        hero.render(g);
    }


 /****************************************************************************/
 /***************************** START-POINT **********************************/
 /****************************************************************************/

    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new YourGame(), new Dimension(640,480), false);
        game.start();
    }

}

Using The Sprite

Set Sprite Location

To set sprite location into specified coordinate use :

class :: Sprite

Syntax:
   public void setLocation(double xs, double ys);

whereas :
xs = the sprite x coordinate
ys = the sprite y coordinate

Moving Sprite

There are 2 functions to move a sprite :

class :: Sprite

Syntax:
   public void setSpeed(double vx, double vy);
   public void move(double dx, double dy);

whereas :
vx = the sprite speed toward x-axis
vy = the sprite speed toward y-axis
dx = sprite delta x movement
dy = sprite delta y movement
The difference between setSpeed(double vx, double vy) and move(double dx, double dy) is :
setSpeed(double vx, double vy) move sprite continously until you set the speed back to zero; while
move(double dx, double dy) move sprite instantly and stop.

Example :

file :: YourGame.java

// JFC
import java.awt.*;
import java.awt.event.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;


public class YourGame extends Game {


    Sprite  hero;


 /****************************************************************************/
 /**************************** GAME SKELETON *********************************/
 /****************************************************************************/

    public void initResources() {
        hero = new Sprite(getImage("player.png"), 100, 200);

        hero.setLocation(50, 50);
        hero.setSpeed(0.1, 0);
    }


    public void update(long elapsedTime) {
        hero.update(elapsedTime);

        if (keyDown(KeyEvent.VK_DOWN)) {
           hero.move(0, 0.1*elapsedTime);
        }
    }


    public void render(Graphics2D g) {
        hero.render(g);
    }


 /****************************************************************************/
 /***************************** START-POINT **********************************/
 /****************************************************************************/

    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new YourGame(), new Dimension(640,480), false);
        game.start();
    }

}

Types of Sprite

Tutorial7_2.java [view online]

There are several types of sprites, one of them is AnimatedSprite class. AnimatedSprite is subclass of Sprite class that able to animate. This kind of sprite need an array of images to make up the sprite animation. Use image engine to cut an image into a good animation.

AnimatedSprite initialization is as followed :

class :: AnimatedSprite

Syntax:
   public AnimatedSprite(BufferedImage[] image,
                         double x,
                         double y);

whereas :
image = an array of image for the sprite image
x     = sprite x coordinate
y     = sprite y coordinate


For example:
create a sprite with image "player.png"
that cutted into 4 columns 2 rows

   BufferedImage[] images = getImages("player.png", 4, 2);
   double x = 100;
   double y = 200;

   AnimatedSprite hero = new AnimatedSprite(images, x, y);

Example :

file :: YourGame.java

// JFC
import java.awt.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;


public class YourGame extends Game {


    AnimatedSprite  hero;


    public void initResources() {
        hero = new AnimatedSprite(getImages("player.png", 4, 2), 100, 200);

        hero.setAnimate(true);
        hero.setLoopAnim(true);
    }

    public void update(long elapsedTime) {
        hero.update(elapsedTime);
    }

    public void render(Graphics2D g) {
        hero.render(g);
    }


    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new YourGame(), new Dimension(640,480), false);
        game.start();
    }

}

Other Important Things

Tutorial7_3.java [view online]
Timer.java [view online]

To make a game / sprite independent of game frame rate, always use Timer class. Timer class is a real time counter class.

The Timer class initialization and how to use it :

class :: Timer

Syntax:
   public Timer(int delay);
   public boolean action(long elapsedTime);

whereas :
delay       = actual time in milliseconds (1 sec=1000 ms)
elapsedTime = time elapsed since last update

Example: making a sprite to fire every one second :

file :: YourGame.java

// JFC
import java.awt.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;


public class YourGame extends Game {


    Sprite  hero;

    Timer   fireRate;


    public void initResources() {
        hero = new Sprite(getImage("player.png"), 100, 200);

        fireRate = new Timer(1000);
    }

    public void update(long elapsedTime) {
        hero.update(elapsedTime);

        if (fireRate.action(elapsedTime)) {
           // fire!!
           playSound("fire.wav");
        }
    }

    public void render(Graphics2D g) {
        hero.render(g);
    }


    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new YourGame(), new Dimension(640,480), false);
        game.start();
    }

}

AnimatedSprite also use Timer class for controlling animation speed, that way the sprite animation is independent of game frame rate.


Summary :

  • Sprite is every objects in game that has its own image and characteristic.
  • Sprite in GTGE is represented by Sprite class.
  • Sprite initialization consists of 3 steps, create new sprite object, updating the sprite, and rendering the sprite to screen.
  • To set sprite location in specified coordinate, use Sprite.setLocation(x, y), and to move a sprite use Sprite.move(dx, dy) or Sprite.setSpeed(vx, vy).
  • AnimatedSprite class is a subclass of Sprite class that can animate.
  • To make a game / sprite independent of game frame rate, always use Timer class.

Reference : Sprite class, AnimatedSprite class, Timer class

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