1. Technology

The design of the Asteroids Game


An asteroid

Mostly everyone has played Asteroids at some time or other in their life. If not you can remind yourself what the fuss is about with this version in html5 or this version in Flash.

This document describes the version for the game programming tutorials in C, C++, C# and Objective-C.

Game Overview

The game is about avoiding crashing into asteroids and instead destroying asteroids using your laser cannon. So the first question is what is an asteroid (in the game)? The html5 version uses solid graphics (impressively rotating!) while the Flash game uses wireframe like the original from 1980. This game will support both these types.

The graphics version has an Asteroid at largest size fit inside a square 192 x 192 pixels and when hit splits into a smaller 96 x 96 then 48 x 48 then 24 x 24. When a large Asteroid is hit it splits into three smaller ones. So a 192 x 192 becomes three 96 x 96 which becomes nine 48 x 48 which becomes 27 24 x 24. So that means you have to destroy 40 targets for each asteroid on the starting screen.

I've got some images convincing looking asteroids. To make it interesting the initials graphics includes two of these asteroids. While the game starts up, it rotates them in eight x 45 degree rotations so there are 16 different asteroids. I do the same for the smaller ones as well.

For the wireframe version, I pre-generate them based on points in a circle. 32 points on the circle's radius are used.


This is easy. On Level 1 there is one Asteroid. On level 2 there are two etc up to level 8. Each asteroid is placed randomly and given a drift of x and y. If it touches another one or the player's ship, it splits into three and all are given velocities that are 120 degrees apart.

Player Ship and Controls

The player's ship is placed in the centre of the screen once the computer has determined there is a clear space there. The controls are simple. Left and right cursor rotate the ship counter clockwise or clockwise. Up arrow accelerates it and down arrow decelerates it. The space bar fires the main gun, the h key does a hyperspace jump to a random clear location but this can only be activated once every three seconds. The f key toggles the frames per second counter; it's displayed in the Window caption bar and d toggles between wireframe and solid graphics. The tab key toggles debug information.

The players ship is a little equilateral triangle with the bullets coming from the apex. There are only ever 16 bullets in play at any time and they have a travel distance of 500 pixels before vanishing. Each has its own velocity so if you are rotating the ship and firing at the same time, you'll get a spray.

Onscreen Movements

Every moving object on the screen has an x and y co-ordinate. They also have a velocity x and y in pixels per frame. Unlike Snake which could run at very high frame rates, the game loop is conditioned to run at 60 frames per second. What this gives is greater control over object movement. The game loop works out where everything will move to, checking for collisions, then it renders everything and flips the displayed screen. It then watches the timer and idles until the 60th of a second from the start of loop is complete and a new frame starts. If everything overruns the 60th of a second, it switches to a 30th of a second mode.

60 frames per second is more than enough for smooth animation but the fallback of 30 fps is also acceptable.

Collision detection uses bounding boxes for the player and asteroids and circule collision detection for bullets. Bullets from the player's ship can hit the player's ship

Alien spacecraft

Once evry minute, an alien spacecraft can appear and start shooting at the player's craft. Up to four bullets are fired at the player's ship and onscreen at the same time and will split Asteroids or destroy the ship until they travel 750 pixels and vanish.

Number of Lives

The player has three lives and continues playing until all are lost. If the score is higher than the 10th score then it gets entered in the high score table along with the player's name. This is saved out to disk everytime it's amended. It's loaded up (if present) when the game runs.

Demo Mode

The game starts at the press space to play game screen. AFter 30 seconds with no key pressed it slips into demo mode. Then 15 seconds it shows the high score table then for another 15 seconds a game is played then it jumps back to the front screen again and repeats.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Programming Games
  5. The design of the Asteroids Game

©2014 About.com. All rights reserved.