Asteroids Game Stage 1 – The Spaceship

{loadposition adposition4}

Asteroids Game Overview

The Asteroids game concept is very straight forward; you have a space ship that flies around based on three buttons, a clockwise and a counterclockwise rotation button, and a forward thrust button. The goal is to shoot the asteroids which break apart in stages, into smaller and smaller asteroids, before eventually disappearing from the screen. {loadposition adposition5}You also need to shoot the alien spaceships, that show up periodically trying to kill you. One of the memorable characteristics of the game was the wrap around screen action that allowed both the asteroids and the spaceships, to fly off of one side of the screen to immediately return on the other side. The game progresses in levels with each level introducing more asteroids and therefore more difficulty.

Stage 1 – The Spaceship

In stage 1 of the Asteroids game development process, you will set up the basic parameters of your game, and create some Flash movieclips for the game graphics, like the ship and the asteroids. Then also in stage 1 we will write the actionscript code that will initialize the spaceship and enable it to fly around on the screen with classic keyboard controls.

Building The Game

Asteroids is a great starter game for learning basic game scripting because the graphics are relatively simple to create. These are the basic game parameters and settings that I used to build the game in Flash. You will find the same Flash document settings used in my series of video tutorials also.

Game Dimensions (size): 550 pixels wide, 400 pixels tall,
Frame Rate (fps): 65 frames per second,
Scripting Language: Actionscript 2.0

The graphics in stage 1 consists of just a single movieclip, the spaceship. The space ship should be facing right and have the registration point set to the center of the spaceship. The image below was shows the mc-ship movieclip in symbol editing mode. You can see the registration point in the center of the ship and the linkage identifier which has been set to ship.To set the linkage identifier right+click on the movieclip symbol in the library and go to properties.

 

Stage 1 Game Code:

stop();
init();

function init(){
    SCORE = 0;
    LIVES = 3;
    MAXAST = 3;
    SHIPDEAD = false;
    LEFT = 0;
    TOP = 0;
    BOTTOM = Stage.height;
    RIGHT = Stage.width;
   
    shipInit();
}

function shipInit(){
    attachMovie("ship", "ship_mc", 2);
    ship_mc._x = RIGHT/2;
    ship_mc._y = BOTTOM/2;
    ship_mc.VELX = 0;
    ship_mc.VELY = 0;
    ship_mc.onEnterFrame = control;
    ROTATE = 0;
    THRUST = 0;
}

function control(){
    if(SHIPDEAD == false){
        if(Key.isDown(Key.LEFT)){
            ROTATE = -1;
        }else if(Key.isDown(Key.RIGHT)){
            ROTATE = 1;
        }else{
            ROTATE = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(Key.UP)){
            THRUST = .1;
            ship_mc.gotoAndStop(2);
        }else{
            THRUST = 0;
            ship_mc.gotoAndStop(1);
        }
    }
    move();
}

function move(){
    if(ROTATE){
        ship_mc._rotation += ROTATE*3;
    }
    if(THRUST){
        RAD = ship_mc._rotation*Math.PI/180;
        ship_mc.THRUSTX = Math.cos(RAD)*THRUST;
        ship_mc.THRUSTY = Math.sin(RAD)*THRUST;
        ship_mc.VELX += ship_mc.THRUSTX;
        ship_mc.VELY += ship_mc.THRUSTY;
    }
    ship_mc._x += ship_mc.VELX;
    ship_mc._y += ship_mc.VELY;
   
    if(ship_mc._x > RIGHT){
        ship_mc._x = LEFT;
    }else if(ship_mc._x < LEFT){
        ship_mc._x = RIGHT;
    }else if(ship_mc._y > BOTTOM){
        ship_mc._y = TOP;
    }else if(ship_mc._y < TOP){
        ship_mc._y = BOTTOM;
    }
}

Video Tutorials

 In part 1, I setup the game and create some of the game graphics

In part 2, I script the keyboard game controls

In part 3, I script the movement of the spaceship using the Math class’ built-in trigonometric functions

 In part 4, I write the actionscript to keep the spaceship on the screen by wrapping the _x and _y properties

 

Author: Dan

Dan teaches computer networking and security classes at Central Oregon Community College.

Leave a Reply

Your email address will not be published. Required fields are marked *