Space Invaders for C# and .NET

In this article, we are going to see to develop games in C#. Here I am developing space invaders game in C# with spiraling bombs and a ship lives indicator.

Introduction

Yes, the classic arcade game has returned and is appearing in C# Corner complete with sound and authentic aliens and source code. It's not quite as good as the real game, but with a few adjustments it can get there. I actually got many of the sounds and images from a web site that is bent on celebrating this traditional ground breaking game: http://spaceinvaders.retrogames.com. This made writing the game a little bit easier since they supplied many of the files in which you can capture the images and sounds. 

Space Invaders Game in C# 
Figure 1 - The Space Invader Game

Note: This article is actually an update of the original space invaders article I posted in 2001. It adds a few minor features such as displaying remaining lives and spiraling bombs.

Playing the Game

This version is played using the left and right arrow keys to move the man and the space bar to fire. If you hit an alien, you get 10, 20, or 30 points, depending on which one you hit. If a bomb hits you, you die and you only have 3 lives. The shields will protect you from a bomb, so you can hide behind them while you wait for the bombs to pass. Occasionally, a saucer flies across the screen. If you hit it, you get a suprise score (up to 150 points). 

Design of the Game

The design for this game is shown below: 

UML Space Invaders Game in C#

 

 

Figure 2 - Space Invader UML Design Reverse engineered using WithClass 2000
 
The Design basically has a base class GameObject which knows how to draw the game pieces bitmap.  Subclassed under this object is all the component classes of the game: Man, Invader, Bullet, Bomb and Shield. A row of Invader classes is controlled through the InvaderRow class. The form contains an array of  InvaderRows, an array of Shields, and a Man.

The Timer Event Handler

The entire game is played off of the timer component. After each tick of the timer, the game checks to see if their are bomb collisions, bullet collisions, and whether or not to move the invaders. It also handles the key presses from the user here so as not to change the timing of the game. If a key is pressed, it is remembered by the key press event, but nothing more happens in the key press event handler.  Below is the code executed with each entry into the timer event handler:

 

  1. private void timer1_Tick(object sender, System.EventArgs e)  
  2. {  
  3.     // move the man according to keypresses  
  4.     HandleKeys();  
  5.     // Count each timer interval to use it as a reference for timing the game  
  6.     TimerCounter++;  
  7.     // if the game is over, just invalidate and return  
  8.     if (GameGoing == false)  
  9.     {  
  10.         // Move the Invaders in place to make them look like they are clapping  
  11.         if (TimerCounter % 6 == 0)  
  12.             MoveInvadersInPlace();  
  13.         Invalidate();  
  14.         return;  
  15.     }  
  16.     // if the bullet shot from the man is off the screen, turn it off  
  17.     if (TheBullet.Position.Y < 0)  
  18.     {  
  19.         ActiveBullet = false;  
  20.     }  
  21.     // Fly the saucer every 200 intervals  
  22.     if (TimerCounter % kSaucerInterval == 0)  
  23.     {  
  24.         InitializeSaucer();  
  25.         PlaySoundInThread("8.wav", 1);  
  26.         SaucerStart = true;  
  27.     }  
  28.     // Move the saucer across the screen  
  29.     if (SaucerStart == true)  
  30.     {  
  31.         CurrentSaucer.Move();  
  32.         if (CurrentSaucer.GetBounds().Left > ClientRectangle.Right)  
  33.         {  
  34.             SaucerStart = false;  
  35.         }  
  36.     }  
  37.     // move invaders every  timer interval according to the speed factor  
  38.     if (TimerCounter % TheSpeed == 0)  
  39.     {  
  40.         MoveInvaders();  
  41.         nTotalInvaders = TotalNumberOfInvaders();  
  42.         // Change the speed of the Invader according to how many are left  
  43.         if (nTotalInvaders <= 20)  
  44.         {  
  45.             TheSpeed = 5;  
  46.         }  
  47.         if (nTotalInvaders <= 10)  
  48.         {  
  49.             TheSpeed = 4;  
  50.         }  
  51.         if (nTotalInvaders <= 5)  
  52.         {  
  53.             TheSpeed = 3;  
  54.         }  
  55.         if (nTotalInvaders <= 3)  
  56.         {  
  57.             TheSpeed = 2;  
  58.         }  
  59.         if (nTotalInvaders <= 1)  
  60.         {  
  61.             TheSpeed = 1;  
  62.         }  
  63.         if (nTotalInvaders == 0)  
  64.         {  
  65.             // All the invaders were killed  
  66.             // Start the game again at the next level (move the invader starting position down)  
  67.             InitializeAllGameObjects(false); // don't initialize score  
  68.             TheLevel++;  
  69.         }  
  70.     }  
  71.     // Test for bullet and bomb collisions and act appropriately  
  72.     TestBulletCollision();  
  73.     TestBombCollision();  
  74.     // Paint all the game objects  
  75.     Invalidate();  
  76. }  
Listing 1 - The Timer Event Handler in the Form

Painting the Game Components

The drawing of all the components is triggered by the Invalidate called in the timer event handler. Below is the paint event handler that paints all of the game elements:
  1. private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)  
  2. {  
  3.     // get the graphics surface  
  4.     Graphics g = e.Graphics;  
  5.     // Draw the entire graphics surface black  
  6.     g.FillRectangle(Brushes.Black, 0, 0, ClientRectangle.Width, ClientRectangle.Height);  
  7.     // Draw the man  
  8.     TheMan.Draw(g);  
  9.     // Draw the score  
  10.     TheScore.Draw(g);  
  11.     // Draw the the number of lives remaining  
  12.     _livesIndicator.Draw(g);  
  13.     // Draw the Bullet shot  
  14.     if (ActiveBullet)  
  15.     {  
  16.         TheBullet.Draw(g);  
  17.     }  
  18.     // Draw the Saucer  
  19.     if (SaucerStart)  
  20.     {  
  21.         CurrentSaucer.Draw(g);  
  22.     }  
  23.     // Draw the Space Invaders  
  24.     for (int i = 0; i < kNumberOfRows; i++)  
  25.     {  
  26.         TheInvaders = InvaderRows[i];  
  27.         TheInvaders.Draw(g);  
  28.     }  
  29.     // Draw the Shields  
  30.     for (int i = 0; i < kNumberOfShields; i++)  
  31.     {  
  32.         Shields[i].Draw(g);  
  33.     }  
  34. }  
Listing 2 - The Paint Event Handler in the Form

Spinning Bombs with Graphic Paths

Short of using a bitmap, graphic paths give you a nice way to draw complex shapes with lines and curves.  Graphic paths can also be operated on with matrix transformations so you can easily rotate, flip, scale, and translate a graphics path on the screen.  Listing 3 shows how a bomb is drawn on a screen using the Draw method in the Bomb class. Before the bomb is drawn, however, two graphics paths are created. One path is created for the zig zag bomb and a one graphics path is created for the bombs reflection on the Y axis.  When we draw the bomb in the Draw method we determine which path to draw by an inversion flag.  The inversion flag is toggled each time in the draw routine, so each path is drawn alternately. As the path is flipped back and forth along the y-axis, it looks like the bomb is spinning down towards the ship.
  1. GraphicsPath bombIn = new GraphicsPath();  
  2. GraphicsPath bombOut = new GraphicsPath();  
  3. GraphicsPath bombInTransformed = new GraphicsPath();  
  4. GraphicsPath bombOutTransformed = new GraphicsPath();  
  5. // BombIn Path  
  6. // \  
  7. // /  
  8. // \  
  9. void CreateBombInPath()  
  10. {  
  11.     float width = 5;  
  12.     float height = 15;  
  13.     float seg = height / 3;  
  14.     bombIn.AddLine(new PointF(0, 0), new PointF(width, seg));  
  15.     bombIn.AddLine(new PointF(width, seg), new PointF(0, seg * 2));  
  16.     bombIn.AddLine(new PointF(0, seg * 2), new PointF(width, seg * 3));  
  17. }  
  18. // BombOut Path  
  19. // /  
  20. // \  
  21. // /  
  22. void CreateBombOutPath()  
  23. {  
  24.     float width = 5;  
  25.     float height = 15;  
  26.     float seg = height / 3;  
  27.     bombOut.AddLine(new PointF(width, 0), new PointF(0, seg));  
  28.     bombOut.AddLine(new PointF(0, seg), new PointF(width, seg * 2));  
  29.     bombOut.AddLine(new PointF(width, seg * 2), new PointF(0, seg * 3));  
  30. }  
  31. bool _invert = false;  
  32. /// <summary>  
  33. /// This routine draws the animated bomb  
  34. /// using two toggling graphics paths  
  35. /// giving the effect of a spinning bomb  
  36. /// </summary>  
  37. /// <param name="g">Graphics Canvas</param>  
  38. public override void Draw(Graphics g)  
  39. {  
  40.     UpdateBounds();  
  41.     // Create an identity Matrix  
  42.     Matrix m = new Matrix();  
  43.     // Translate the Matrix to point to the correct position   
  44.     // in the game where the bomb is positioned  
  45.     m.Translate(MovingBounds.Left, MovingBounds.Top);  
  46.     // if the invert flag is set, draw the BombIn Graphics Path  
  47.     if (_invert)  
  48.     {  
  49.         bombInTransformed = (GraphicsPath)bombIn.Clone();// first clone the original 0,0 origin path  
  50.         bombInTransformed.Transform(m); // Translate the cloned graphics path  
  51.         g.DrawPath(BombPen, bombInTransformed); // Draw the bomb  
  52.         bombInTransformed.Dispose(); // dispose the used path  
  53.     }  
  54.     else  
  55.     {  
  56.         bombOutTransformed = (GraphicsPath)bombOut.Clone();// draw the bomb out path for non-inverted  
  57.         bombOutTransformed.Transform(m);  
  58.         g.DrawPath(BombPen, bombOutTransformed);  
  59.         bombOutTransformed.Dispose();  
  60.     }  
  61.     _invert = !_invert;// toggle the path each time to give a spiraling bomb effect  
  62.     Position.Y += TheBombInterval; // move the bomb down towards the round target  
  63. }  
Listing 3 - Creating the Bomb Graphic Paths and Drawing the Bomb

Conclusion

Although an old arcade game, Space Invaders is still fun to play. GDI+ provides enough graphic power to recreate the game in its entirety. Using bitmaps, graphic paths, simple shapes, and a timer, those invaders are soon moving across the screen and dropping bombs on your unsuspecting ship. Using the old windows multimedia library you can also play wave files and bring back the original sounds and explosions. Space Invaders was designed with OOP in mind, so I hope this article gives you a good basis for thinking about how to design games with objects.  In the meantime, don't let those invaders get the best of you!

Master Guide to Starcraft 2  World of Warcraft Leveling Guide Passing Microsoft Certification  Basic Programming