Book Read Free

Introduction To Game Design & Programming in GameMaker Studio 2

Page 8

by Ben Tyers

if y
  if y>flying_level y-=3;

  }

  If the player is not pressing a key to move up or down, this code will move the player back to it’s starting value, at 3 pixels per step.

  if keyboard_check(ord("W"))

  {

  y-=6;

  }

  if keyboard_check(ord("S"))

  {

  y+=6;

  }

  This checks for a keypress and then changes the player’s y position accordlingly at 6 pixels per step that the key is held down

  .

  //forward back

  if keyboard_check(ord("D"))

  {

  x+=6;

  }

  if keyboard_check(ord("A"))

  {

  x-=6;

  }

  x=clamp(x,xx-40,xx+180);

  nokey=keyboard_check(ord("A"))+keyboard_check(ord("D"));

  if nokey==0

  {

  if x
  if x>xx x-=3;

  }

  This code works in same way as moving up and down, instead it moves the player along the horizontal x position.

  if global.level>8 power_active=true;

  This checks if the player has upgraded to level 8, if so then the power weapon becomes active.

  Alarm 0 Event

  /// @description Flash Control

  flash=false;

  This sets the flag for flash back to false, this tells the game to no longer show player damage.

  Alarm 1 Event:

  /// @description Weapon 1 Control

  can_shoot_1=true;

  Allows the player to shoot weapon 1 once again. This alarm is set when the player fires bullet 1, this flag is then set to false. This prevents the player shooting again until set back to true, preventing the player from shooting too often.

  Alarm 2 Event

  /// @description Weapon 2 Control

  can_shoot_2=true;

  Allows player to shoot weapon 2 (if active)

  Alarm 10 Event

  /// @description Power Weapon

  alarm[10]=room_speed*6;

  if power_active

  {

  pow=instance_create_layer(x,y,"Missiles",obj_power_bullet);

  pow.direction=image_angle;

  pow.image_angle=image_angle;

  pow.speed=8;

  }

  Restarts the alarm for 6 seconds, and if active creates an instance of the power weapon.

  Alarm 11 Event

  /// @description Sheild Control

  shield-=0.05;

  if shield>0 alarm[11]=room_speed;

  else

  shield=0;

  Reduces the value of shield and resets alarm 11 if more than 0, if less the 0 the value of shield is set to 0 and the alarm is not reset. If the value of shield is more than 0 it means it is active and the player has protection against enemy weapons and asteroids.

  Draw Event

  /// @description Drawing Stuff

  if flash==true && shield==0

  {

  draw_sprite_ext(sprite_index,image_index,x,y,1,1,0,c_red,1);

  }

  else

  {

  draw_self();

  }

  draw_sprite_ext(spr_shield,0,x,y,1,1,0,c_white,shield);

  draw_set_alpha(0.3);

  draw_healthbar(5,room_height-100,room_width-5,room_height-5,(100/health)*health,c_black,c_red,c_green,0,true,true);

  draw_set_alpha(1);

  if flash==true && shield==0

  {

  draw_sprite_ext(sprite_index,image_index,x,y,1,1,0,c_red,1);

  }

  else

  {

  draw_self();

  }

  This code will draw the player with a red hue if it has damage, or just draw the set sprite otherwise.

  draw_sprite_ext(spr_shield,0,x,y,1,1,0,c_white,shield);

  This draws the shield over the player’s sprite, with an alpha value of shield. This means that if the shield has a value 0 it’s alpha will 0 and this not visible.

  draw_set_alpha(0.3);

  draw_healthbar(5,room_height-100,room_width-5,room_height-5,(100/health)*health,c_black,c_red,c_green,0,true,true);

  draw_set_alpha(1);

  This sets the alpha value to 30% and then draws a health bar with the player’s health at the bottom of the window.

  Note: If you set the alpha value in code, like above, it will apply to all game objects until it is changed again, this we set it back to 1 (100%) when done drawing the health bar.

  Global Left Pressed Event

  /// @description Weapon 1

  if can_shoot_1==false exit;

  var xx = x + lengthdir_x(64, image_angle);

  var yy = y + lengthdir_y(64, image_angle);

  missile=instance_create_layer(xx,yy,"Missiles",obj_player_arrow);

  missile.image_angle=image_angle;

  missile.direction=image_angle;

  missile.speed=6;

  can_shoot_1=false;

  alarm[1]=room_speed*1.5;

  This checks if the player can shoot (can_shoot_1 is true), and exits if false. If true it will create an instance of obj_player_arrow, with the same angle and direction as the player is. It then sets the flag can_shoot_1 to false (preventing the player from shooting until true again), an alarm 1 is set for 1.5 seconds.

  var xx = x + lengthdir_x(64, image_angle);

  var yy = y + lengthdir_y(64, image_angle);

  These two line simplify some complex maths, basically it sets two variables that stay at a point on the player’s sprite, taking into account the player’s image angle.

  Global Right Pressed Event

  /// @description Weapon 2

  if global.level<13 exit;

  if can_shoot_2==false exit;

  alarm[1]=room_speed;

  show_debug_message("Missile 2");

  var xx = x + lengthdir_x(64, image_angle);

  var yy = y + lengthdir_y(64, image_angle);

  missile=instance_create_layer(xx,yy,"Missiles",obj_player_missile);

  missile.image_angle=image_angle;

  missile.direction=image_angle;

  missile.speed=10;

  can_shoot_2=false;

  alarm[2]=room_speed*1.5;

  Some logic here as previous event.

  Key Down H Event

  /// @description For Testing

  health=100;

  Put this in for testing, just resets health to 100.

  Collision With obj_ball Event

  /// @description Damage

  if shield>0

  {

  //do nothing

  }

  else

  {

  audio_play_sound(snd_lose_health,1,false);

  health-=5;

  flash=true;

  alarm[0]=room_speed/2;

  }

  with (other) instance_destroy();

  Checks if shield is active, if it is skips the next block and destroys the ball. If the player does not have a shield it plays a sound, reduces health (which is automatically a global value that can be accessed by all game instances – so no global. Is needed), sets flash to true so the player draws with a red hue and sets the alarm 0 to 0.5 seconds.

  Collision With obj_en_parent_projectile Event

  /// @description Collision Control

  if shield>0

  {

  //do nothing

  }

  else

  {

  audio_play_sound(snd_lose_health,1,false);

  health-=other.strength;

  flash=true;

  alarm[0]=room_speed/2;

  }

  with (other) instance_destroy();

  show_debug_message("Collision With Enemy Detected");

  show_debug_message("Health is now "+string(health));

  Works in a similar way to the previous collision event. This collision event occurs when any instance that has this set as a parent collides with the player – this allows
you to perform one collision check on various object instances – great if you have a game with many ten’s or hundred’s of instance.

  health-=other.strength;

  This line will reduce the health by what ever value strength in the other holds, again great if you have many instances that have different strength values.

  show_debug_message("Collision With Enemy Detected");

  show_debug_message("Health is now "+string(health));

  This was covered in the debug chapter, it sends output to the console window.

  Collision With obj_enemy_parent Event

  /// @description Collision

  if shield>0

  {

  other.hp-=global.pow;

  }

  else

  {

  audio_play_sound(snd_lose_health,1,false);

  health-=1;

  flash=true;

  alarm[0]=room_speed/2;

  }

  Again checks for a collision and whether shield is active or not.

  other.hp-=global.pow;

  This code will reduce the colliding enemy’s health by the current global.pow value.

  Collision With obj_power_up Event

  /// @description Increase Level

  with other instance_destroy();

  audio_play_sound(snd_power_up,1,false);

  global.level++;

  if global.level==2

  {

  //show_message("Top Shooter");

  instance_create_layer(x,y,"Player",obj_extra_1_top);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=2;

  }

  if global.level==3

  {

  /////show_message("Bottom Shooter");

  instance_create_layer(x,y,"Player",obj_extra_1_bottom);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=3;

  }

  if global.level==4

  {

  //show_message("Top Laser");

  instance_create_layer(x,y,"Player",obj_extra_2_top);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=4;

  }

  if global.level==5

  {

  //show_message("Bottom Laser");

  instance_create_layer(x,y,"Player",obj_extra_2_bottom);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=5;

  }

  if global.level==6

  {

  //show_message("Triple Shots");

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=6;

  }

  if global.level==7

  {

  //show_message("Mine 1");

  instance_create_layer(x,y,"Player",obj_mine_1);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=7;

  }

  if global.level==8

  {

  //show_message("Mine 2");

  instance_create_layer(x,y,"Player",obj_mine_2);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=8;

  }

  if global.level==9

  {

  //show_message("Plasma");

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=9;

  }

  if global.level==10

  {

  //show_message("Mine 3 & 4");

  instance_create_layer(x,y,"Player",obj_mine_3);

  instance_create_layer(x,y,"Player",obj_mine_4);

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=10;

  }

  if global.level==11

  {

  //show_message("Faster Shots");

  global.shoot_speed++;

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=11;

  }

  if global.level==12

  {

  //show_message("Faster Shots");

  global.shoot_speed++;

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=12;

  }

  if global.level==13

  {

  //show_message("Homing Missile");

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=13;

  }

  if global.level>13

  {

  global.pow++;

  ///show info

  info=instance_create_layer(x,y,"Foreground",obj_info);

  info.image_index=14;

  }

  This will destroy the bonus item and then check the current level, it will then create an instance of obj_info and tell it to use the given subimage. This is used to tell the player what bonus they got for collecting the item.

  Collision With obj_shield_collect Event

  /// @description Activate Shield

  shield=1;

  alarm[11]=room_speed;

  with other instance_destroy();

  audio_play_sound(snd_shield,1,false);

  This will set the shield to 1 (active) and set an alarm. It will also destroy the shield collect item and play a sound.

  obj_player_weapon_parent

  This object is used so that we can reduce the number of collision event calls in the game’s enemies. A parent object has children (with the parent set in the child object), allowing you check against a parent, which check for all children. This is used so we can reduce the number of collision checks needed when checking enemy and player projectile collisions. 3 is not a huge number, but when you make a game with lots of weapons, this can reduce the coding time and complexity needed.

  Is the parent of the following player’s projectiles.

  obj_bullet_1, obj_bullet_2, and obj_player_arrow

  There is no code for this object.obj_mine_parent

  Is the parent of obj_mine_1, obj_mine_2, obj_mine_3 and obj_mine_4

  Again, a parent is used to reduce collision event checks.

  obj_mine_1

  This an object that rotates around the player, which can damage enemies or destroy enemy projectiles.

  This has the sprite spr_mine, as shown in Figure 7_28:

  Figure 7_28: Showing sprite set up for spr_mine

  You should note the sprite origin as -110 x 13. This negative value is used to make rotating it around the player, without complex code.

  Create Event

  /// @description Set Up

  my_power=50;

  Sets the strength of the mine.

  Step Event

  /// @description Rotate & Clamp To Player & Power

  image_angle++;

  x = obj_player.x;

  y = obj_player.y;

  my_power=50*global.pow;

  image_angle++;

  This makes the sprite rotate around it’s origin by 1’ each step.

  x = obj_player.x;

  y = obj_player.y;

  Keeps the mine clamped with the player, so if the player moves the mine will stay relative to it.

  The final line sets the strength of the mine, based on the current value of global.pow.

  obj_mine_2

  Does the same as obj_mine_1 and uses the same sprite.

  Create Event

  /// @description Set Up

  my_power=50;

  Step Event

  /// @description Rotate & Clamp To Player & Power

  image_angle=obj_mine_1.image_angle+180

  x = obj_
player.x;

  y = obj_player.y;

  my_power=50*global.pow;

  image_angle=obj_mine_1.image_angle+180

  This keeps the angle 180 off of mine 1, so that they are stay on either side of the player instance.

  obj_mine_3

  Does the same as obj_mine_1 and uses the same sprite.

  Create Event

  /// @description Set Up

  my_power=50;

  Step Event

  /// @description Rotate & Clamp To Player & Powerimage_angle=obj_mine_1.image_angle+180

  image_angle=obj_mine_1.image_angle+90

  x = obj_player.x;

  y = obj_player.y;

  my_power=50*global.pow;

  As previous object, though at 90’ off obj_mine_1’s angle.

  obj_mine_4

  Does the same as obj_mine_1 and uses the same sprite.

  Create Event

  /// @description Set Up

  my_power=50;

  Step Event

  /// @description Rotate & Clamp To Player & Power

  image_angle=obj_mine_1.image_angle-90

  x = obj_player.x;

  y = obj_player.y;

  my_power=50*global.pow;

  As previous object, though at -90’ off obj_mine_1’s angle.

  This set up ensures that once all 4 mines are active, they smoothly rotate around the player at equal distances.

  obj_player_arrow

  This is the weapon that the player starts with, it is fired by the player pressing the left mouse button (when the weapon is active and can be fired).

  The sprite for this instance is sprite spr_rocket as shown in Figure 7_29:

  Figure 7_29: Showing player’s primary weapon

  Create Event

  /// @description Set Up

  my_power=1*global.pow;

  audio_play_sound(snd_rocket,1,false);

  Sets the power and plays a sound.

  Outside Room Event

  /// @description Destroy when not needeed

  instance_destroy();

  Destroys once outside the room, good practice and helps prevent memory issues.

  obj_player_missile

  This is one the weapons the player can upgrade to. This weapon will find a target enemy and attempt to hit it.

  The sprite for this is spr_player_missile_2, as shown in Figure 7_30 below:

  Figure 7_30: Sprite set up for spr_player_missile

 

‹ Prev