Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » GDI+ & Graphics » Space Invaders for C# and .NET

Space Invaders for C# and .NET

This is an update of the space invaders game posted on C# Corner 3 years ago for Visual Studio 2005. This version adds spiraling bombs and a ship lives indicator.

Author Rank:
Total page views :  258592
Total downloads :  3067
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SpaceInvaders2005.ZIP
 
Become a Sponsor

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. 

 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:

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:

private void timer1_Tick(object sender, System.EventArgs e)

// move the man according to keypresses
HandleKeys();
// Count each timer interval to use it as a reference for timing the game
TimerCounter++;
// if the game is over, just invalidate and return
if
(GameGoing == false)
{
// Move the Invaders in place to make them look like they are clapping
if (TimerCounter % 6 == 0)
MoveInvadersInPlace();
Invalidate();
return;
}
// if the bullet shot from the man is off the screen, turn it off
if (TheBullet.Position.Y < 0)
{
ActiveBullet = false;
}
// Fly the saucer every 200 intervals
if
(TimerCounter % kSaucerInterval == 0)
{
InitializeSaucer();
PlaySoundInThread("8.wav", 1);
SaucerStart = true;
}
// Move the saucer across the screen
if
(SaucerStart == true)
{
CurrentSaucer.Move();

if (CurrentSaucer.GetBounds().Left > ClientRectangle.Right)
{
SaucerStart = false;
}
}
// move invaders every  timer interval according to the speed factor
if (TimerCounter % TheSpeed == 0)

MoveInvaders();
nTotalInvaders = TotalNumberOfInvaders();
// Change the speed of the Invader according to how many are left
if (nTotalInvaders <= 20)
{
TheSpeed = 5;
}
if (nTotalInvaders <= 10)
{
TheSpeed = 4;
}
if (nTotalInvaders <= 5)
{
TheSpeed = 3;
}
if (nTotalInvaders <= 3)
{
TheSpeed = 2;
}
if (nTotalInvaders <= 1 )
{
TheSpeed = 1;
}
if (nTotalInvaders == 0)
{

// All the invaders were killed
// Start the game again at the next level (move the invader starting position down)
InitializeAllGameObjects(false); // don't initialize score
TheLevel++;
}
}
// Test for bullet and bomb collisions and act appropriately
TestBulletCollision();
TestBombCollision();
// Paint all the game objects
Invalidate();
}

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:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// get the graphics surface
Graphics g = e.Graphics;
// Draw the entire graphics surface black
g.FillRectangle(Brushes.Black, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
// Draw the man
TheMan.Draw(g);
// Draw the score
TheScore.Draw(g);
// Draw the the number of lives remaining
_livesIndicator.Draw(g);
// Draw the Bullet shot
if
(ActiveBullet)
{
TheBullet.Draw(g);
}
// Draw the Saucer
if
(SaucerStart)
{
CurrentSaucer.Draw(g);
}
// Draw the Space Invaders
for
(int i = 0; i < kNumberOfRows; i++)
{
TheInvaders = InvaderRows[i];
TheInvaders.Draw(g);
}
// Draw the Shields
for
(int i = 0; i < kNumberOfShields; i++)
{
Shields[i].Draw(g);
}
}

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.

GraphicsPath bombIn = new GraphicsPath();
GraphicsPath bombOut =
new
GraphicsPath();
GraphicsPath bombInTransformed =
new
GraphicsPath();
GraphicsPath bombOutTransformed =
new
GraphicsPath();
// BombIn Path
// \
// /
// \
void
CreateBombInPath()
{
float
width = 5;
float
height = 15;
float
seg = height / 3;
bombIn.AddLine(
new PointF(0, 0), new
PointF(width, seg));
bombIn.AddLine(
new PointF(width, seg), new
PointF(0, seg * 2));
bombIn.AddLine(
new PointF(0, seg * 2), new
PointF(width, seg * 3));
}
// BombOut Path
// /
// \
// /
void
CreateBombOutPath()
{
float
width = 5;
float
height = 15;
float
seg = height / 3;
bombOut.AddLine(
new PointF(width, 0), new
PointF(0, seg));
bombOut.AddLine(
new PointF(0, seg), new
PointF(width, seg * 2));
bombOut.AddLine(
new PointF(width, seg * 2), new
PointF(0, seg * 3));
}
bool _invert = false
;
///
<summary>
///
This routine draws the animated bomb
///
using two toggling graphics paths
///
giving the effect of a spinning bomb
///
</summary>
/// <param name="g">Graphics Canvas
</param>
public override void
Draw(Graphics g)
{
UpdateBounds();
// Create an identity Matrix
Matrix m = new
Matrix();
// Translate the Matrix to point to the correct position
// in the game where the bomb is positioned
m.Translate(MovingBounds.Left, MovingBounds.Top);
// if the invert flag is set, draw the BombIn Graphics Path
if
(_invert)
{
bombInTransformed = (GraphicsPath)bombIn.Clone();
// first clone the original 0,0 origin path
bombInTransformed.Transform(m);
// Translate the cloned graphics path
g.DrawPath(BombPen, bombInTransformed);
// Draw the bomb
bombInTransformed.Dispose();
// dispose the used path
}
else

{
bombOutTransformed = (GraphicsPath)bombOut.Clone();
// draw the bomb out path for non-inverted
bombOutTransformed.Transform(m);
g.DrawPath(BombPen, bombOutTransformed);
bombOutTransformed.Dispose();
}
_invert = !_invert;
// toggle the path each time to give a spiraling bomb effect
Position.Y += TheBombInterval;
// move the bomb down towards the round target
}

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!


Login to add your contents and source code to this article
 About the author
 
Mike Gold
Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems. He can be reached at mike@c-sharpcorner.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SpaceInvaders2005.ZIP
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.