An Asteroids Clone

The classic retro game Asteroids!

Like most of my peers, many hours were spent (or wasted, depending on your point of view) on the game Asteroids. The following is a brief overview of what was my first major programming project at university creating a clone of this classic, even down to the awful music (if two distorted tones could be considered music!). The font style is typical Atari, with only the graphics being updated. The gameplay remains the same, with the objective being to achieve the highest score by destroying the Asteroids whilst avoiding crashing into them.

The game is programmed in C++ and uses the SplashKit library. This handles the heavy lifting of refreshing screens, drawing sprites, controlling movement, etc. Of note, being an early project I had yet to take note of licensing, therefore the game itself uses several resources (graphics and sounds) which likely don’t have applicable creative commons licensing. This is something I addressed in my final year ensuring all projects used appropriate resources.

Classic Asteroids Movement

Probably the most interesting area of the game was creating the same effect the original game had, whereby the ship had a velocity and the player could control this via the ships thruster. The same idea was also used to move the asteroids and the bullets.

SplashKits physics capabilities were leveraged in order to provide this functionality. When the player uses the A or D keys, the players ship is rotated accordingly. When the player pushes the W key, the thruster is engaged and the velocity of the ship is changed in accordance with the direction of the ship/sprite. This means that the objects continue to move in a trajectory without further player input.

This functionality can be found within a function called handle_input within the game class file. As you can see below an instance of a game object is passed into the function and the sprite representing the ship is the reference in regards to orientation. When the thruster is engaged the velocity of the sprite is modified accordingly.

// handle key strokes from the user
void handle_input(game_data &game)
{
    // Handle movement - rotating left/right and changes in velocity
    float rotation = sprite_rotation(game.player.main_sprite);

    // Allow rotation with A/D keys
    if (key_down(A_KEY))
        sprite_set_rotation(game.player.main_sprite, rotation - PLAYER_ROTATE_SPEED);

    if (key_down(D_KEY))
        sprite_set_rotation(game.player.main_sprite, rotation + PLAYER_ROTATE_SPEED);

    // Modify velocity
    sprite_hide_layer(game.player.main_sprite, 1); // sprite layer representing thrust
    if (key_down(W_KEY)){
        // add to velocity depending on direction of thrust and existing velocity
        game.player.velocity.x += cos(rotation*M_PI/180) * PLAYER_SPEED;
        game.player.velocity.y += sin(rotation*M_PI/180) * PLAYER_SPEED;
        sprite_show_layer(game.player.main_sprite, 1); // show thrust layer
        play_sound_effect(sound_effect_named("thrust")); // play thrust sound effect
    }
}

Full source code for this project can be found within my github account.

There are several areas that could be improved, probably the greatest being when an sprite is straddling the screen. As it stands the game simply checks when the sprite is out of bounds and then places it at the opposite side of the screen. Also speeding up the default velocity of the asteroids upon completion of each level could be implemented. However, I was happy overall with how the game turned out!