Game Development Reference
In-Depth Information
Creating a PolygonSprite Class for Asteroids
PolygonSprite is the final foundation class of the game (see Listing 4-3). It is used to describe all game
objects including the ship, asteroids, and flying saucer. Furthermore, it tracks information such as
the following for all of these game objects:
X and Y coordinates
Angle of rotation
Position in the screen
In this class, two polygons are used: one to keep the basic shape, and the other to apply the final
translation and rotation and to paint on screen.
Listing 4-3. The PolygonSprite Class Used by Asteroids
package ch04.game;
import ch04.common.Polygon;
import android.graphics.Canvas;
import android.graphics.Paint;
/************************************************************
* The PolygonSprite class defines a game object, including it's shape,
* position, movement and rotation. It also can determine if two objects
* collide.
************************************************************/
public class PolygonSprite
{
// Base sprite shape, centered at the origin (0,0).
public Polygon shape;
// Final location and shape of sprite after
// applying rotation and translation to get screen
// position. Used for drawing on the screen and in
// detecting collisions.
public Polygon sprite;
boolean active; // Active flag.
double angle; // Current angle of rotation.
double deltaAngle; // Amount to change the rotation angle.
double deltaX, deltaY; // Amount to change the screen position.
// coords
int x;
int y;
// Constructors:
Search WWH ::




Custom Search