External Exam Download Resources Web Applications Games Recycle Bin

asteroids | gml

 TypeDesc
lengthdir_x(how_far, in_direction_angle)functionfinds the distance straight 'across' to a point that is diagonally away
lengthdir_y(how_far, in_direction_angle)functionfinds the distance straight 'up' to a point that is diagonally away
move_wrap(horizontal?, vertical?, margin)function'wraps' an instance around the room borders
motion_add(direction, speed)functionjust like motion_set but this function is cumulative (it will add or subtract from previous direction / speed)
point_in_rectangle(pointX, pointY, ... rectangle coords which are x1,y1,x2,y2 ...)functionchecks if a point is in a rectangle
SpriteImages
sprExplosion               
Download sprExplosion images here: asteroids-sprExplosion.zip
sprSpaceShip
sprRock
sprLazer

Set all sprite origins to Middle-centre. You may have to adjust the collision mask on the rock to be smaller.

ObjectSpriteProperties
objSpaceShipsprSpaceShipVisible
objRocksprRockVisible
objLazersprLazerVisible

objRock Create

randomize();
image_speed = 0;
motion_set(random(360), irandom_range(1,3));

objRock Step

move_wrap(true, true, 0);

//if rock has hit a lazer, start playing the animation:
if place_meeting(x,y,objLazer){
  image_speed = 1;	
}

//once the rock animation is complete:
if (image_index == sprite_get_number(sprRock) - 1) {
  instance_destroy();	
}

objSpaceShip Step

max_speed = 10;
accel_units = 0.4;

switch (keyboard_key) {
  //rotate left or right, but dont change direction yet
  case vk_left: image_angle += 7; break;
  case vk_right: image_angle -= 7; break;
  case vk_up:
    //accumulate speed the way we are facing:
    motion_add(image_angle, +accel_units) 
    //speed limiter:
    if (speed > max_speed) speed = max_speed;
  break;
  case vk_space:
    //lengthdir_x is width from origin to nose of ship:
    xx = x+lengthdir_x(sprite_width/2, image_angle);
  //lengthdir_y is height from origin to nose of ship: 
  yy = y+lengthdir_y(sprite_height/2, image_angle);
  //spawn lazer at nose of ship:
    lazer = instance_create_depth(xx,yy,0,objLazer);
    lazer.image_angle = image_angle;
  lazer.direction = image_angle;
  lazer.speed = 10;
  break;
}
//wrap around room:
move_wrap(true, true, 0);

//if collide with asteroid we explode:
if place_meeting(x,y,objRock) {
  sprite_index = sprExplosion;
}

//if we are exploding, AND the explosion is complete:
if (image_index == sprite_get_number(sprExplosion) - 1) and
    (sprite_index == sprExplosion) {
  instance_destroy();	
}

objLazer Step

//if lazer collides with rock:
if place_meeting(x,y,objRock) {
  instance_destroy();	
}

//if lazer outside room:
if (!point_in_rectangle(x, y, 0, 0, room_width, room_height)) {
  instance_destroy();
}

Add:
  1. score
  2. time
  3. lives
  4. health
Once complete, balance play mechanics.