Introduction To Game Design & Programming in GameMaker Studio 2

Home > Other > Introduction To Game Design & Programming in GameMaker Studio 2 > Page 9
Introduction To Game Design & Programming in GameMaker Studio 2 Page 9

by Ben Tyers


  Create Event

  /// @description Set Up

  live=true;

  active=true;

  alarm[0]=room_speed*5;

  //look for initial target

  if instance_exists(obj_enemy_parent)

  {

  target=instance_nearest(mouse_x,mouse_y,obj_enemy_parent);

  }

  else

  {

  target=noone;

  }

  start_hp=200;

  hp=200;

  my_power=5*global.pow;

  audio_play_sound(snd_missile,1,false);

  live=true;

  active=true;

  alarm[0]=room_speed*5;

  Sets a flag as true for live and active. Sets an alarm 0 for 5 seconds.

  if instance_exists(obj_enemy_parent)

  {

  target=instance_nearest(mouse_x,mouse_y,obj_enemy_parent);

  }

  else

  {

  target=noone;

  }

  Looks for a target (that has the parent obj_enemy_parent), sets the value of target to the enemy’s id (each instance has a different id), otherwise sets it to noone.

  start_hp=200;

  hp=200;

  This object has an hp, which counts down. When run out the missile destroys.

  my_power=5*global.pow;

  audio_play_sound(snd_missile,1,false);

  Sets the power of the missile and plays a sound.

  Step Event

  /// @description Look For Target

  if instance_exists(obj_enemy_parent)

  {

  if target=noone target=instance_nearest(x,y,obj_enemy_parent);

  }

  else

  {

  target=noone;

  }

  ///track target

  if target!=noone

  if instance_exists(target)

  {

  tx = target.x;

  ty = target.y;

  direction = scr_angle_rotate(direction, point_direction(x, y, tx, ty),3);

  image_angle = direction;

  }

  else

  {

  if instance_exists(obj_enemy_parent)

  {

  target=instance_nearest(x,y,obj_enemy_parent);

  }

  else

  {

  target=noone;

  }

  }

  if instance_exists(target) && target.x<0 target=noone;

  //health bar

  hp--;

  if hp<1

  {

  instance_destroy();

  }

  if instance_exists(obj_enemy_parent)

  {

  if target=noone target=instance_nearest(x,y,obj_enemy_parent);

  }

  Checks for a new target if obj_enemy_parent exists and the weapon does not currently have a target set.

  if target!=noone

  if instance_exists(target)

  {

  tx = target.x;

  ty = target.y;

  direction = scr_angle_rotate(direction, point_direction(x, y, tx, ty),3);

  image_angle = direction;

  }

  If the weapon has a target and the target still exists, then changes direction to move towards the target.

  else

  {

  if instance_exists(obj_enemy_parent)

  {

  target=instance_nearest(x,y,obj_enemy_parent);

  }

  else

  {

  target=noone;

  }

  }

  Looks for a new target again if current does not exist.

  hp--;

  if hp<1

  {

  instance_destroy();

  }

  Reduces hp by 1 each step ( -- is basically the same as hp-=1 or hp=hp-1), if less than 1 then destroys itself.

  Draw Event

  /// @description Drawing

  draw_self();

  if instance_exists(target)

  {

  draw_sprite(spr_locked,0,target.x,target.y);

  draw_set_colour(c_lime);

  draw_line_width(x,y,target.x,target.y,3);

  }

  scr_healthbar(0,0,40);

  draw_self();

  Draws self (if you put any code in a Draw Event that doesn’t force drawing of a sprite, it will not draw – adding this forces it to draw).

  if instance_exists(target)

  {

  draw_sprite(spr_locked,0,target.x,target.y);

  draw_set_colour(c_lime);

  draw_line_width(x,y,target.x,target.y,3);

  }

  This will check if the target exists, and if it does it will draw a lime green line from the weapon to the target.

  scr_healthbar(0,0,40);

  This will draw a small healthbar relative to the weapons current location.

  obj_bullet_1

  This object is the projectile that will fire the player’s secondary weapon (when player has collected enough power ups and the weapon is active).

  Figure 7_31 shows the sprite set up:

  Figure 7_31: Showing sprite set up for spr_bullet_1

  Create Event

  /// @description Set Up

  my_power=1*global.pow;

  audio_play_sound(snd_bullet_1,1,false);

  Sets the strength of the weapon and plays a sound.

  Outside Room Event

  /// @description Destroy when not needeed

  instance_destroy();

  Destroys the instance when outside the room.

  obj_bullet_2

  This object is the projectile that will fire the player’s triary weapon (when player has collected enough power ups and the weapon is active).

  This uses the same sprite as shown in Figure 7_30.

  Create Event

  /// @description Set Up

  my_power=2*global.pow;

  audio_play_sound(snd_bullet_2,1,false);

  Sets the strength of the weapon and plays a sound.

  Outside Room Event

  /// @description Destroy When no longer needed

  instance_destroy();

  Destroys the instance when outside the room.

  obj_power_bullet

  This is a power weapon that becomes active when the player has collected enough power ups.

  Figure 7_32 shows spr_power_effect, which is assigned to this object:

  Figure 7_32: Showing the sprite spr_power_effect

  Create Event

  /// @description Set Up

  image_xscale=0.1;

  image_yscale=0.1;

  my_power=2*global.pow;

  audio_play_sound(snd_power,1,false);

  Sets the initial scale (size), power and plays a sound.

  Step Event

  /// @description Increase Size

  image_xscale+=0.06;

  image_yscale+=0.06;

  Gradually increases size.

  Outside Room Event

  /// @description Destroy When Done

  instance_destroy();

  Destroy when done with, to prevent memory errors.

  obj_extra_1_top

  This weapon will be created when the player has collected enough power ups. It shoots automatically.

  Figure 7_33 below shows the sprite assigned for this object:

  Figure 7_33: Showing sprite spr_extra_1

  Create Event

  /// @description Set initial alarm

  alarm[0]=room_speed*2;

  Sets an alarm for firing control

  Step Event

  /// @description Keep In Relation to player location and angle

  xx = obj_player.x + lengthdir_x(64, obj_player.image_angle);

  yy = obj_player.y + 100 + lengthdir_y(128, obj_player.image_angle);

  image_angle=obj_player.image_angle;

  x=xx;

  y=yy-204;

  Keeps the angle and position relative to the player.

  Alarm 0 Event

  /// @de
scription Shoot Projectiles

  alarm[0]=(room_speed*1.5)+room_speed*5/global.shoot_speed;

  ang=image_angle;

  xx = x + lengthdir_x(32, ang);

  yy = y + lengthdir_y(32, ang);

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang;

  bullet.speed=7;

  bullet.image_angle=ang;

  if global.level<6 exit;

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang-30;

  bullet.speed=7;

  bullet.image_angle=ang-30;

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang+30;

  bullet.speed=7;

  bullet.image_angle=ang+30;

  alarm[0]=(room_speed*1.5)+room_speed*5/global.shoot_speed;

  Resets the alarm.

  ang=image_angle;

  xx = x + lengthdir_x(32, ang);

  yy = y + lengthdir_y(32, ang);

  Keeps it relative to the player.

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang;

  bullet.speed=7;

  bullet.image_angle=ang;

  Creates an instance of the bullet object, with a starting location relative the weapon source, and with given speed and angle.

  if global.level<6 exit;

  Exists if player is on less than level 6. If it is 6 or above then 2 more projectiles are created, with angles of +30’ and -30’ of the initial weapon.

  obj_extra_1_bottom

  This weapon will be created when the player has collected enough power ups. It shoots automatically.

  This object uses the same sprite as shown in Figure 7_33 previously.

  Create Event

  /// @description Set Alarm (synced with top)

  alarm[0]=obj_extra_1_top.alarm[0];

  level=1;

  Step Event

  /// @description Keep in relation to player

  xx = obj_player.x + lengthdir_x(64, obj_player.image_angle);

  yy = obj_player.y + 100 + lengthdir_y(128, obj_player.image_angle);

  image_angle=obj_player.image_angle;

  x=xx;

  y=yy;

  Alarm 0 Event

  /// @description Shoot Projectiles

  alarm[0]=obj_extra_1_top.alarm[0];

  ang=image_angle;

  xx = x + lengthdir_x(32, ang);

  yy = y + lengthdir_y(32, ang);

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang;

  bullet.speed=7;

  bullet.image_angle=ang;

  if global.level<6 exit;

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang-30;

  bullet.speed=7;

  bullet.image_angle=ang-30;

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_1);

  bullet.direction=ang+30;

  bullet.speed=7;

  bullet.image_angle=ang+30;

  Works in the same way as the first weapon above.

  obj_extra_2_top

  This weapon will be created when the player has collected enough power ups. It shoots automatically.

  The sprite for this object is shown below in Figure 7_34:

  Figure 7_34: Showing sprite spr_extra_2

  Create Event

  /// @description Set inital alarm

  alarm[0]=room_speed*2;

  Step Event

  /// @description sync with player

  xx = obj_player.x + lengthdir_x(64, obj_player.image_angle);

  yy = obj_player.y + 100 + lengthdir_y(128, obj_player.image_angle);

  image_angle=obj_player.image_angle;

  x=xx;

  y=yy-332;

  Alarm 0 Event

  /// @description Create Projectile

  alarm[0]=room_speed*4;

  ang=image_angle;

  xx = x + lengthdir_x(32, ang);

  yy = y + lengthdir_y(32, ang);

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_2);

  bullet.direction=ang;

  bullet.speed=7;

  bullet.image_angle=ang;

  No new code to comment on here.

  obj_extra_2_bottom

  This weapon will be created when the player has collected enough power ups. It shoots automatically.

  This uses the same sprite as shown in Figure 7_34.

  Create Event

  /// @description Set Initial Alarm

  alarm[0]=obj_extra_2_top.alarm[0];

  Step Event

  /// @description SSync with player

  xx = obj_player.x + lengthdir_x(64, obj_player.image_angle);

  yy = obj_player.y + 100 + lengthdir_y(128, obj_player.image_angle);

  image_angle=obj_player.image_angle;

  x=xx;

  y=yy+128;

  Alarm 0 Event

  /// @description Cretae Projectile

  alarm[0]=obj_extra_2_top.alarm[0];

  ang=image_angle;

  xx = x + lengthdir_x(32, ang);

  yy = y + lengthdir_y(32, ang);

  bullet=instance_create_layer(xx,yy,"Missiles",obj_bullet_2);

  bullet.direction=ang;

  bullet.speed=7;

  bullet.image_angle=ang;

  No new code to comment on here.

  obj_spawn_enemy

  This object will act as a controller, and spawn enemies accordingly.

  There is no sprite for this object.

  Create Event

  /// @description Set Inital alarms

  alarm[0]=room_speed*8;

  alarm[1]=room_speed*16;

  alarm[2]=room_speed*60;

  Sets some initial alarms.

  Alarm 0 Event

  /// @description Make Enemy Floor

  alarm[0]=room_speed*8;

  if instance_number(obj_boss)==0 && instance_number(obj_enemy_floor)==0 enemy=instance_create_layer(room_width+200,0,"Enemy",obj_enemy_floor);

  Restarts the alarm, then checks is no instances of obj_boss or obj_enemy_floor are currently present, if not it creates an instance of obj_enemy_floor.

  Alarm 1 Event

  /// @description Make Enemy Air

  alarm[1]=room_speed*16;

  if instance_number(obj_boss)==0 && instance_number(obj_enemy_air)==0 enemy=instance_create_layer(room_width+200,0,"Enemy",obj_enemy_air);

  Restarts the alarm, and then checks whether obj_boss or obj_enemy_air is present, if not an instance of obj_enemy_air is created.

  Alarm 2 Event

  /// @description Make Boss

  alarm[0]=room_speed*60;

  if instance_number(obj_boss)==0 enemy=instance_create_layer(room_width+200,0,"Enemy",obj_boss);

  Checks whether an instance of obj_boss is currently present, if not it creates one.

  obj_enemy_floor

  This is the enemy that will move along the bottom of the window. It does not fire any projectiles.

  Figure 7_35 shows the sprite set up for this object:

  Figure 7_35: Showing the sprite spr_floor_1 for object obj_enemy_floor

  Create Event

  /// @description Set Up

  start_hp=global.level*global.level;

  hp=start_hp;

  hspeed=-3.7;

  This sets the instances starting hp values, used for drawing detecting when dead. hspeed is the horizontal speed, a negative value makes it move towards the left.

  Step Event

  /// @description Check pos, hp

  if x<0 instance_destroy();

  //if hp<1

  if hp<1

  {

  instance_destroy();

  coin=instance_create_layer(x,y,"Player",obj_coin_bubble);

  coin.value=start_hp;

  for (var i = 0; i < 15; i += 1)

  {

  part=instance_create_layer(x,y,"Score_Effect",obj_floor_parts);

  part.image_ind
ex=i;

  }

  }

  //control y

  y=layer_get_y("bg_1")+100;

  if x<0 instance_destroy();

  This line of code checks if it has gone off the left side of the window, if it has then it no longer needed and is destroyed.

  if hp<1

  {

  instance_destroy();

  coin=instance_create_layer(x,y,"Player",obj_coin_bubble);

  coin.value=start_hp;

  Checks the value of hp, if it is less than 1, this code block is executed. It firstly creates a coin at the enemy’s location and gives is a value equal to the starting hp value.

  for (var i = 0; i < 15; i += 1)

  {

  part=instance_create_layer(x,y,"Score_Effect",obj_floor_parts);

  part.image_index=i;

  }

  }

  This creates instances of obj_floor_parts and assigns each a different image_index.

  y=layer_get_y("bg_1")+100;

  This makes the instance move up and down relative to background bg_1. This ensures that it matches the backgrounds movement.

  Draw Event

  /// @description Drawing

  draw_self();

  scr_healthbar(0,-40,60);

  Draws the current subimage and a health bar.

  Collision With obj_mine_parent Event

  /// @description Reduce if in contact with mine

  hp-=other.my_power;

  This will reduces it’s hp every step it is colliding with any mine.

  Collision With obj_player_weapon_parent Event

  /// @description Reduce hp

  hp-=other.my_power;

  with other instance_destroy();

  audio_play_sound(snd_exp_1,1,false);

  Reduces hp by the colliding instances my_power value, destroys the colliding instance, and plays a sound.

  Collision with obj_power_bullet Event

  /// @description Reduce hp

  hp-=other.my_power/10;

  Reduces hp when in collision with the power_bullet object, reduces by 1/10 of the power_bullet’s power.

  obj_enemy_air

  This enemy will move onto the window, then follow a set path on a loop. This will fire projectiles towards the player.

 

‹ Prev