Game Development Reference
In-Depth Information
1.
The size of the screen is queried using the layout's getWidth and getHeight
methods. The size is required to render elements on screen
The stars are drawn using an array of star sprites and calling canvas.drawPoint .
Each sprite is drawn using its X and Y coordinates and a Paint object for style
information.
2.
3.
Photons are drawn only if they are active. A photon becomes active when the
user presses the space bar to fire the gun. Each photon is aware of its position
on the canvas.
4.
Missiles, a UFO, and the user's ship are drawn if they are active. When the user
reaches a score threshold, the UFO will show up. Users must watch out for the
missiles fired against them by the UFO. Note that the ship's thrusters must be
drawn when the user presses the arrow keys to move the ship around.
5.
Asteroids and explosion debris are drawn. Note that each bit of debris is an
independent sprite.
6.
Status messages are drawn. Messages include the score in the upper-left
corner, the number of ships left in the lower-left, and the high score in the
upper-right.
7.
Other messages are displayed depending on the state of the game, including
Game Over, Game Paused, or Sound Muted.
Tip Remember that each sprite is aware of its X and Y coordinates on screen and angle of rotation? The key
method that performs all the magic is PolygonSprite.draw(Canvas canvas, Paint paint) . Within this method,
the polygon lines are drawn using canvas.drawLines(float[] points, Paint paint) .
Listing 4-7. The Drawing Subroutine for Asteroids
protected void onDraw(Canvas canvas)
{
// get screen size
int width = getWidth();
int height = getHeight();
// draw stars
for (i = 0; i < numStars; i++) {
canvas.drawPoint(stars[i].x, stars[i].y, mPaint);
}
// Draw photon bullets.
for (i = 0; i < Constants.MAX_SHOTS; i++) {
if (photons[i].active) {
photons[i].draw(canvas, mPaint);
Search WWH ::




Custom Search