Click here to Skip to main content
13,868,937 members
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 14 Jun 2004

C# Asteroids

, 14 Jun 2004
Rate this:
Please Sign up or sign in to vote.
An article on writing a simple game in C#


This is my own implementation of Asteroids. It is simulated vector based and uses the GDI for graphics. The game itself is fairly object-oriented and should be easy for someone to interpret. There are some issues remaining in the code which I hope some people could help resolve.


I originally wrote an Asteroids game in VB on Windows 3.1 a very long time ago. This is essentially a port of that version. That version had just basic gameplay, but no sounds.

Using the code

Since this game is vector based, it is based on a very large space. All objects are represented in this larger space and then scaled down to the resolution of the display. This seems to work fairly well and allows the same game to be scaled in a window of any size. The playfield owns all the objects and basically loops through to draw each one.

The game features a fancy title screen display and pause. Implementing pause was trivial - I don't know why all games don't incorporate such a feature.

Sounds are played through DirectX. The graphics are drawn using the GDI. DirectX or some other form of display would likely improve things quite a bit.

Each object is in charge of itself. This makes it trivial for animation, since the object essentially handles this for free. There are a few unique situations where things are drawn a little bit differently. The space ship, for example, is responsible for drawing its thrust.

A surprisingly nice effect is generated for the thurst, bullets and explosions. This is done by randomly cycling through a list of colors while drawing. This creates a dazzling effect.

Collision detection is currently trivial. Since the asteroids are all round (see below), it's trivial to just check the distance of any point from the center of the asteroid. When UFO's come (see below) and asteroids get unique shapes, this method won't work - but will be good to see if two objects are close enough to warrant a more complete check.

Object rotation:

protected void Rotate(double degrees)
   double radiansAdjust = degrees * 0.0174532925;
   radians += radiansAdjust/FPS;
   double SinVal = Math.Sin(radians);
   double CosVal = Math.Cos(radians);

   Point ptTransformed = new Point(0,0);
   for (int i=0; i<points.Count; i++)
      Point pt = ((Point)points[i]);
      ptTransformed.X = (int)(pt.X * CosVal + pt.Y * SinVal);
      ptTransformed.Y = (int)(pt.X * SinVal - pt.Y * CosVal);

Drawing an object (in this case, ship):

public new void Draw(ScreenCanvas sc, int iPictX, int iPictY)
   switch (state)
      case SHIP_STATE.ALIVE:
         base.Draw(sc, iPictX, iPictY);
         if (bThrustOn)
             //We have points transformed
             //so...  we know where the bottom of the ship is
            ArrayList alPoly = new ArrayList();
            alPoly.Capacity = 3;
            int iThrustSize = rndGen.Next(200) + 100;  random thrust effect
            alPoly.Add(new Point((((Point)pointsTransformed[iPointThrust1]).X +
              ((Point)pointsTransformed[iPointThrust2]).X) / 2 +
              (((Point)pointsTransformed[iPointThrust1]).Y +
              ((Point)pointsTransformed[iPointThrust2]).Y) / 2 +
             //Draw thrust directly to ScreenCanvas
             //it's not really part of the ship object
            DrawPolyToSC(alPoly, sc, iPictX, iPictY, GetRandomFireColor());

Points of Interest


  • Thrust - up arrow
  • Rotate right - right arrow
  • Rotate left - left arrow
  • Hyperspace - down arrow (there is a risk of explosion on re-entry)
  • Shoot - space bar
  • Pause - P key.

There are several things that are a bit annoying or incomplete with this game. They are:

  • Sometimes the game completely freezes. I think this is due to sound.
  • The game seems to work fine up to resolutions above 1024x768. I am sure this is due to my goofy drawing and page-flipping algorithm. It's pretty amazing that this game performs no better than my old VB version in this regard.
  • Sometimes the game slows to a crawl while thrusting. Releasing thrust and it's fine.
  • Sometimes there is an exception thrown at shutdown of the game.
  • No UFOs - yet. I need some help on smart bullet shooting logic.
  • Asteroids are all round. They do rotate. I'd like to get bumpy asteroids like the arcade. This should be fairly trivial, but it will make the collision detection a little bit harder.
  • Objects jump when going over edge of the screen. This could create the possibility where something that looks like it should have collided will disappear and there won't be a collision unless it occurs where the object flipped to.
  • Sounds are in a directory - I couldn't figure out how to embed them into the executable, which would be so much nicer.


  • June 15, 2004 - Initial release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Howard Uman
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Daytona_6753-Oct-13 14:20
memberDaytona_6753-Oct-13 14:20 
GeneralAwesome! Pin
Daytona_6751-Oct-13 12:02
memberDaytona_6751-Oct-13 12:02 
GeneralRe: Awesome! Pin
unhuman4-Nov-13 9:26
memberunhuman4-Nov-13 9:26 
GeneralRe: Awesome! Pin
Daytona_67522-Jan-14 10:37
memberDaytona_67522-Jan-14 10:37 
GeneralRe: Awesome! Pin
jwakeman4-Jun-16 10:55
memberjwakeman4-Jun-16 10:55 
GeneralRe: Awesome! Pin
unhuman16-Jun-16 3:48
memberunhuman16-Jun-16 3:48 
GeneralRe: Awesome! Pin
Ernie Salazar2-Oct-18 12:44
memberErnie Salazar2-Oct-18 12:44 
GeneralVery Cool Pin
DaveMon21-Sep-07 9:51
memberDaveMon21-Sep-07 9:51 
QuestionWhen and Where do you fill the members of ScreenCanacas Pin
Muhammad Gouda22-May-07 4:40
memberMuhammad Gouda22-May-07 4:40 
AnswerRe: When and Where do you fill the members of ScreenCanacas Pin
Howard Uman22-May-07 6:44
memberHoward Uman22-May-07 6:44 
GeneralPage flipping Pin
Dirk Vandenheuvel16-Jun-06 1:01
memberDirk Vandenheuvel16-Jun-06 1:01 
GeneralCool Pin
J.Doli15-Jun-04 18:22
memberJ.Doli15-Jun-04 18:22 
GeneralRe: Cool Pin
Howard Uman16-Jun-04 4:49
memberHoward Uman16-Jun-04 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 15 Jun 2004
Article Copyright 2004 by Howard Uman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid