Click here to Skip to main content
Click here to Skip to main content

C# Asteroids

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

Introduction

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.

Background

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);

    pointsTransformed.Clear();
    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);
       pointsTransformed.Add(ptTransformed);
    }
 }

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;
             alPoly.Add(pointsTransformed[iPointThrust1]);
             alPoly.Add(pointsTransformed[iPointThrust2]);
             int iThrustSize = rndGen.Next(200) + 100;  random thrust effect
             alPoly.Add(new Point((((Point)pointsTransformed[iPointThrust1]).X + 
               ((Point)pointsTransformed[iPointThrust2]).X) / 2 + 
               (int)(iThrustSize*Math.Sin(radians)),
               (((Point)pointsTransformed[iPointThrust1]).Y + 
               ((Point)pointsTransformed[iPointThrust2]).Y) / 2 + 
               (int)(-iThrustSize*Math.Cos(radians))));
              //Draw thrust directly to ScreenCanvas
              //it's not really part of the ship object
             DrawPolyToSC(alPoly, sc, iPictX, iPictY, GetRandomFireColor());
          }
          break;
    }

Points of Interest

Controls:

  • 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.

History

  • June 15, 2004 - Initial release.

License

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

Share

About the Author

Howard Uman

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberTyron Harford3-Oct-13 14:20 
GeneralAwesome! [modified] PinmemberTyron Harford1-Oct-13 12:02 
GeneralRe: Awesome! Pinmemberunhuman4-Nov-13 9:26 
GeneralRe: Awesome! PinmemberTyron Harford22-Jan-14 10:37 
GeneralVery Cool PinmemberDaveMon21-Sep-07 9:51 
QuestionWhen and Where do you fill the members of ScreenCanacas PinmemberGouda Sharp22-May-07 4:40 
AnswerRe: When and Where do you fill the members of ScreenCanacas PinmemberHoward Uman22-May-07 6:44 
GeneralPage flipping PinmemberDirk Vandenheuvel16-Jun-06 1:01 
GeneralCool Pinmemberdolimite3515-Jun-04 18:22 
GeneralRe: Cool PinmemberHoward Uman16-Jun-04 4:49 

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

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

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