0% found this document useful (0 votes)
87 views

Sprites: Sprite Animation

The document discusses sprites and sprite animation techniques in video games. It describes sprites as animated graphic images that move around in a game to form gameplay. There are two main ways to animate sprites: 1) loading individual frames stored in separate files or 2) loading frames stored in a single bitmap with rows and columns. The key to animating sprites from a tiled bitmap is to calculate the frame's column and row location algorithmically based on the frame number and bitmap properties. BufferedImages are recommended over regular Images for sprite manipulation due to their accessible pixel data and automatic memory management by the JVM.

Uploaded by

ale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Sprites: Sprite Animation

The document discusses sprites and sprite animation techniques in video games. It describes sprites as animated graphic images that move around in a game to form gameplay. There are two main ways to animate sprites: 1) loading individual frames stored in separate files or 2) loading frames stored in a single bitmap with rows and columns. The key to animating sprites from a tiled bitmap is to calculate the frame's column and row location algorithmically based on the frame number and bitmap properties. BufferedImages are recommended over regular Images for sprite manipulation due to their accessible pixel data and automatic memory management by the JVM.

Uploaded by

ale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Alejandro Gleason Méndez A01703013 Videojuegos

Sprites

A sprite represents an animated graphic image that moves around in a


game and is involved with other images to form the gameplay of a game,
which is the purpose of the game -the set of steps, missions and objectives-.
The difference between a regular image and a sprite is often that a sprite will
encapsulate the image data (in matrix, arrays, files) as well as the methods
needed to manipulate it (imageLoader, grabImage).

A sprite cannot draw itself without the JFrame and Graphics2D objects in a
main program.

SPRITE ANIMATION
Essential ways to animate a graphic object on the screen:
1) Loading individual frames, each stored in its own bitmap file (in sequence),
in my opinion not quite viable.
2) Loading a single bitmap containing rows and columns of animation frames
(as tiles), in my opinion a better way, since you can easily add more and
more images.

Drawing Individual Frames


 The key to drawing a single frame from an animation sequence stored
in a tiled bitmap is to figure out where each frame is located
algorithmically.
 You can calculate the column (that is, the number of frames across)
by dividing the frame number by the number of columns and
multiplying that by the height of each frame. This calculation focuses
on the quotient as the answer we want, example:

frameY = (frameNumber / columns) * height;

This will give you the correct row down into the image where your desired
frame is located, but it will not provide you with the actual column, or X value.
For that, you need a similar solution. Instead of dividing the frame number by
columns, we will use modulus. This calculation focuses on the remainder as
the answer we want.

frameX = (frameNumber % columns) * width;


BufferedImage img = image.getSubimage((col*95)-95,(row*52)-52, width,
height);

Complete method:
Alejandro Gleason Méndez A01703013 Videojuegos

public BufferedImage grabImage(int col, int row, int width, int height){
BufferedImage img = image.getSubimage((col*95)-95,(row*52)-
52, width, height);//Starting point
return img;
}

public void draw (Graphics g, BufferedImage imgx){


g.drawImage(imgx, 200, 500, null);
}

 Update method that increments the animation frame and then tests it
against the bounds of the animation sequence. For instance:

frameCount++;
if (frameCount > frameDelay) {
frameCount=0;
currentFrame += animationDirection;
if (currentFrame > totalFrames-1) {
currentFrame = 0;
}
else if (currentFrame < 0) {
currentFrame = totalFrames-1;
}
}

Images, Visual Effects, and Animation

Images are a central part of every game, and this chapter examines how we
can (efficiently) load and display them, apply visual effects such as blurring,
fading, and rotation, and animate them.

You need an ImageLoader (AWT Imaging model):

import javax.swing.*;
import java.awt.*;
public class ShowImage extends JApplet

{
private Image im;

public void init()


{ im = getImage( getDocumentBase(), "ball.gif"); }

public void paint(Graphics g)


Alejandro Gleason Méndez A01703013 Videojuegos

{ g.drawImage(im, 0, 0, this); } }

The getDocumentBase() method returns the URL of the directory holding the
original Web document, and this is prepended to the image's filename to get
a URL suitable for getImage(). The problem with image retrieval is the speed.

The getImage() method is poorly named since it doesn't get (or download)
the image at all. Instead it prepares an empty Image object (im) for holding
the image, returning immediately after that.

Another option would be the MediaTracker Class, which method consists of


the following, it preloads images before drawing them (we do not tie
downloading to painting).

public void init()


{
im = getImage( getDocumentBase(), "ball.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(im, 0);
try {
tracker.waitForID(0);
}
catch (InterruptedException e)
{ System.out.println("Download Error"); }
}

Finally, there is the ImageIcon option. The ImageIcon object can be


converted to an Image (as here), or be painted with ImageIcon's paintIcon()
method.

public void init()


{ im = new ImageIcon( getDocumentBase()+"ball.gif").getImage(); }

On the other hand, a JAR (Java Archive) file is a way of packing code and
resources together into a single, compressed file, resources can be anything.
If an applet (or application) is going to utilize a lot of images, repeated
network connections to download them will severely reduce execution
speed. It's much better to create a single JAR file containing the applet (or
application) and all the images, and have the browser (or user) download it.
Then, when an image comes to be loaded, it's a fast, local load from the JAR
file, loading changes a little:

im = new ImageIcon( getClass().getResource("ball.gif") ).getImage();

Images processed on AWT can be seen as the following:


Alejandro Gleason Méndez A01703013 Videojuegos

Though an alternative would be to use the PixelGrabber (in my opinion, It is


more complicated, it collects all the pixel data from an image into an array).

As we know, the paintComponent method from Javava 2D class only paints


stuff from that library (such as figures), it has the following method:
public void paintComponent(Graphics g)
// draw a blue square
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; // cast the graphics context
g2d.setPaint(Color.blue);
Rectangle2D.Double square =
new Rectangle2D.Double(10,10,350,350);
g2d.fill(square);

}
Alejandro Gleason Méndez A01703013 Videojuegos

Now, getting to the most important part, the BufferedImage class is a


subclass ofImage, and it can be emploued instead of Image in methods such
as drawImage(). It has 2 main advantages: the data required for image
manipulation is easily accessible through methods and BufferedImages are
automatically converted to manage images by the JVM, the code would be

public class ShowImage extends JApplet

{
private BufferedImage im;

public void init()


{ try {

im = ImageIO.read( getClass().getResource("ball.gif") ); }

catch(IOException e) {
System.out.println("Load Image error:");
}
} // end of init()
public void paint(Graphics g)
{ g.drawImage(im, 0, 0, this); }
}

It is said to be faster (in a 10%) from imageIcon, the data manainted by a


BufferedImage is represented as:

You might also like