Game Development Reference
In-Depth Information
for (i = 0; i < numStars; i++) {
// random XY Point
stars[i] = new Point((int) (Math.random() * width)
, (int) (Math.random() * height) );
}
}
The stars are created using random
Point
s with X and Y coordinates defined using the
getWidth
and
getHeight
methods. Note that
getWidth
and
getHeight
are built-in methods provided by the
LinerLayout
class.
Next, we create all polygon sprites in the game:
The ship
•
The ship's thrusters
•
Photons (the ship's bullets)
•
The flying saucer
•
Asteroids and asteroid explosions
Note that all elements in the game are polygons, even the tiny photons and asteroid explosions.
Finally, miscellaneous data is initialized and the game is put in Game Over mode.
•
Listing 4-6.
Game Initialization
protected void initialize() {
// Load sounds
try {
loadSounds();
loaded = true;
} catch (Exception e) {
Tools.MessageBox(mContex, "Sound Error: " + e.toString());
}
// create star background
createStarts();
// Create shape for the ship sprite.
ship = new PolygonSprite();
ship.shape.addPoint(0, -10);
ship.shape.addPoint(7, 10);
ship.shape.addPoint(-7, 10);
// Create thruster shapes
createThrusters();
// Create shape for each photon sprites.
for (i = 0; i < Constants.MAX_SHOTS; i++) {