IT tutorials
 
Mobile
 

Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 3) - Enemy Class

5/22/2013 4:15:12 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Enemy Class

The AlienGameObject class that represents the frenzied aliens is remarkably simple. The class inherits from the GameObject class and uses the inherited constructor with some modifications:

public AlienGameObject(SpriteSheet loadedTexture,
          string spriteName, Rectangle screenRect)
  : base(loadedTexture, spriteName, screenRect)
{
  Alive = true;
  ResetGameObject();
}

The Alive property is set to true because we want enemies to keep dropping. There is a customized ResetGameObject method that overrides the base class version as well:

public override void ResetGameObject()
    {
      //Randomize animation
      _spriteIndex = (int)(randomNumber.NextDouble() * NumberOfAnimationFrames);
      //Randomize initial position
      Position = new Vector2(randomNumber.Next(_screenRect.Width), 35);

      //Apply default alien speed
      Velocity = new Vector2(randomNumber.Next(alienVelocityArc), alienSpeed);
      Alive = true;
    }

					  

The alien spaceship has a unique shape. For better collision detection we override the BoundingRect property and call Rectangle.Inflate(0,2); to inflate the Rectangle on the Y axis resulting in much better collision detection for this game:

public override Rectangle BoundingRect
{
  get
  {
    Rectangle rect = new Rectangle((int)Position.X, (int)Position.Y,
      SpriteAnimationSpriteSheet.SourceRectangle(SpriteName + 0).Width,
      SpriteAnimationSpriteSheet.SourceRectangle(SpriteName + 0).Height);
    rect.Inflate(0,20);
    return rect ;
  }
}

That's it to this class. All of the lively action is a result of the animation code handled by the base class and the ResetGameObject method. The ResetGameObject method does a couple of things:

  • It starts the animation at a random index, so that the objects don't appear to be animating in sync, which would be boring.

  • It randomly picks an 'X' value for the initial PositionVector2 to start the drop.

  • Finally, it applies a random but very small 'X' value to the Velocity so that the alien space ships don't appear to just fall straight down.

Figure 5 shows an army of aliens invading.

Figure 5. Alien invasion

You will want to play on a real device to get a full sense of the game, but as you can see in Figure 5, the aliens strike different drop lines and fall fairly randomly for such little code. The animation code helps to keep things lively as well. In the next section, we cover the hero ship class, which takes accelerometer and touch input to move and fire missiles at the invasion.

 
Others
 
- 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
- Symbian OS : Security - Secure Agent
- iOS SDK : Application Settings - The Settings Bundle (part 4) - PSSliderSpecifier, PSChildPaneSpecifier
 
 
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
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us