The next game in my continued endeavor to learn C++ was Asteroids. I intentionally picked something about an order magnitude more complicated than my previous project, Pong, but wanted to still stay relatively simple.


Asteroids is, simply put, a top-down 2D ship shooter with a wraparound play-field where you try last as long as possible. It is a pretty well-known game from back in the day and I didn't think it would be all that hard to make.

My coding style didn't change all to much between Pong and Asteroids. My main challenge ended up being a larger game object hierarchy, which I solved in a somewhat straightforward manner. The GameObject class has several instance variables:

  • int UID - a unique identification number
  • int type - allows for object type classification (ie, TYPE_SHIP, TYPE_BULLET, TYPE_ASTEROID, etc.)
  • bool alive - whether or not the GameObject needs to be removed

In addition, I moved towards a component based architecture, where the GameObject class possesses a pointer to one of each of:

  • State2D - the object's position and orientation in 2-space
  • Visual - for rendering an object
  • CollisionObject - for handling collisions

Because each of these were abstracted out I was able to (debatably) decrease the size of my object tree hierarchy. Since Asteroids is such a simple game it probably didn't save me much here, but I wanted to test it out since I will only be making more complicated games in the future. This ended up working well enough for me here, and I think I will stick to it in the future.

Aside from complexity arising from scale, here are some of the other challenges I overcame:

  • Display of line-frame objects with position & orientation
  • Persistent high score maintained through a save file
  • Particle effects on object death
  • Screen wraparound

I would like to add that Mike Geig's Object Oriented Game Development series helped out immensely in my understanding of C++ and how to deal with larger projects, and especially with how to work with iterators.

Asteroids source code is available here.

Asteroids as an executable + resources is available here. To run it, rename the ".rename" extension to ".exe".  Controls are either arrow keys or ASDW and the spacebar. The high score screen is a bit tricky to navigate around in. Use left / right arrow to change your cursor position and up/down arrow to change the lettering. Navigate all the way to the right and hit enter to commit & exit.

Leave a Reply