5. Missile Class
The Missile class pretty straightforward, shown in Listing 3.
Example 3. Missile GameObject Class File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpriteSheetRuntime;
using Microsoft.Xna.Framework;
namespace AlienShooter.GameObjects
{
class MissileGameObject : GameObject
{
public MissileGameObject(SpriteSheet loadedTexture,
string spriteName, Rectangle screenRect)
: base(loadedTexture, spriteName, screenRect)
{
ResetGameObject();
}
public override void ResetGameObject()
{
Position = new Vector2(-SpriteCenter.X, _screenRect.Height + SpriteCenter.Y);
Velocity = new Vector2(0,-5);
Alive = false;
}
}
}
|
The next class we cover is the GameStatusBoard class.
6. Game Status Board Class
The GameStatusBoard class keeps track of and
displays the score and lives available during the game. This class also
plays sound effects when an enemy ship is destroyed and when the hero
ship takes a hit from an enemy ship. The GameStatusBoard class also vibrates the phone when an alien ship hits the hero ship using the VibrateController class. Figure 6 shows the status board in action.
In the following two sections, we cover keeping score and tracking lives functionality in the GameStatusBoard class.
6.1. Keeping Score
Each time a hero ship missile intercepts an alien space ship, 5 points are added to the GameStatusBoard.Score property. The Score property is modified in the GamePlay screen, which we cover later. Within the GameStatusBoard class, updating the score results in a SoundEffect.Play call for the Explosion.wma file:
public int Score
{
get { return _score; }
set
{
_score = value;
_alienExplosionSoundEffect.Play();
}
}
The GameStatusBoard.Update method has a switch statement to display a message based on current score:
switch (Score)
{
case 50: _displayMessage = true;
_message = "Nice Start!";
break;
case 60: _displayMessage = false;
break;
case 100: _displayMessage = true;
_message = "Keep It Up!";
break;
case 120: _displayMessage = false;
break;
default: break;
}
When the _displayMessage field is true based on score, GameStatusBoard.Draw displays the message in the Status Board with this call:
if (_displayMessage)
spriteBatch.DrawString(_gameFont, _message, new Vector2(175, 0),
_livesTextColor);
That's it for score keeping. We next cover tracking hero ship lives and game over functionality.
6.2. Tracking Lives
Be default, when playing the Alien Shooter game, you
get 3 lives to start. Each time an alien ship intersects the hero ship, a
life is deducted. At 2 lives a message of "Warning!" is displayed. At 1 life, a message of "Danger!" is displayed. Finally, when zero lives are the state, a "Game Over!"
message is displayed at top and in the middle of the screen. Also, each
time a live is deducted, the phone is briefly vibrated. Here is the
declaration and instantiation of the VibrateController class:
private VibrateController _vibrateController = VibrateController.Default;
private TimeSpan _vibrateTimeSpan = TimeSpan.FromMilliseconds(400);
Here is the Lives property where the sound is played and phone vibrated when the Lives property is modified:
public int Lives
{
get { return _lives; }
set
{
_lives = value;
_heroShipDamageSoundEffect.Play();
_vibrateController.Start(_vibrateTimeSpan);
}
}
Here is the code from the GameStatusBoard.Update method that determines what message to display, and in what color:
switch (_lives)
{
case 3: _livesTextColor = Color.LightGreen;
break;
case 2: _livesTextColor = Color.Yellow;
_displayMessage = true;
_message = "Waring!";
break;
case 1: _livesTextColor = Color.Red;
_displayMessage = true;
_message = "Danger!";
break;
case 0: _livesTextColor = Color.Red;
_displayMessage = true;
_message = "Game Over!";
GameOver = true;
break;
}
Here is the corresponding code from the GameStatusBoard.Draw method that determines when to display a message about the hero ship help on the screen:
if (_displayMessage)
spriteBatch.DrawString(_gameFont, _message, new Vector2(175, 0),
_livesTextColor);
if (GameOver)
spriteBatch.DrawString(_gameFont, _message, new Vector2(175, 370),
_livesTextColor);
Having an informative game status board is an
important component of any game development effort. This section covered
how simple it is to provide the basics. In the next section, we cover
the overall logic in the GamePlayScreen class that pulls together all of the game objects we just covered.