IT tutorials
 
Mobile
 

Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 5) - Missile Class, Game Status Board Class

5/22/2013 4:17:29 AM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

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.

Figure 6. Alien invasion status board

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.

 
Others
 
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 4) - Hero Ship Class
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 3) - Enemy Class
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 2) - Game Object Class
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 1) - Sprite Animation
- BlackBerry Bold 9700 and 9650 Series : Connect as a Tethered Modem (part 3) - Using Desktop Manager for Mac
- BlackBerry Bold 9700 and 9650 Series : Connect as a Tethered Modem (part 2) - Using Desktop Manager for Windows
- BlackBerry Bold 9700 and 9650 Series : Connect as a Tethered Modem (part 1) - Understanding the Options for Tethering
- Android 3 : Employing Basic Widgets - Fleeting Images, Fields of Green...or Other Colors
- Android 3 : Employing Basic Widgets - Assigning Labels
- Symbian OS : Security - Buckle
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS