Introduction To Game Design & Programming in GameMaker Studio 2

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

by Ben Tyers


  Figure A_11_1: Setting up a collision event

  There are a number of different ways to use GML to check for a collision.

  For example, check whether it does not collide with another object, returning true or false:

  place_empty(x, y);

  As above, but checks for solid objects only:

  place_free(x, y);

  You can check for a specific object, which uses the sprite as base to check for overlap:

  place_meeting(x, y, object);

  You can check a single pixel location to see if an object is in that position, for example to check for a specific instance:

  position_meeting(mouse_x,mouse_y,id);

  You can destroy all objects at a location:

  position_destroy(x, y);

  Or the following, which finds the instance ID:

  instance_position(x, y, obj );

  For the purpose of this level 1 book we’ll mainly be using the in-built Collision Event for all object collisions (except for buttons that detect a mouse click), but feel free to experiment.

  There will be lots of occasions that you may want to check that the mouse cursor is over an object before performing any additional code.

  if (position_meeting(mouse_x, mouse_y, id))

  {

  image_index=0;

  } else

  {

  image_index=1;

  }

  Collision Line

  For this example you'll need three sprites, spr_player in green and spr_enemy in red and spr_wall in blue. A size of 32x32 with origin set as center for each will be fine.

  In the Step Event of obj_player and assign the sprite, put the following; this will allow you to move the player object using the arrow keys:

  x+=4*(keyboard_check(vk_right)-keyboard_check(vk_left));

  y+=4*(keyboard_check(vk_down)-keyboard_check(vk_up));

  Create an object obj_enemy and assign the sprite . In the Draw Event of obj_target put the following code, which will draw a line between obj_target and obj_player if there is a direct line of sight (i.e., no walls in the way):

  draw_self();

  draw_set_colour(c_white);

  if (collision_line(x,y,obj_player.x,obj_player.y,obj_wall, false,true)) == noone

  {

  draw_line(x,y,obj_player.x,obj_player.y);

  }

  Place one instance of obj_player, and a few each of obj_target and obj_wall into a new room and test. Figure A_11_2 shows this in action:

  Figure A_11_2: Showing example room with objects added

  A YYZ for the above is available in the resources.

  You could use something like this as the basis for an AI system.

  Basic Projects

  • A) Make the player change colour (or sub image) when it can see one or more of obj_enemy. 2 Points

  • B) Create a new object, obj_target, and assign a pink sprite to it. If player collides with it, play a sound and make it destroy itself.

  • C) Create a clickable object with four sub images. When the mouse button is released when over the object change the sub image. On the forth click destroy the object.

  Advanced Projects

  • D) Make the player change direction at random if the mouse gets within 100 pixels in any direction, but only check this once every 5 seconds. Also make the object wrap around the screen. See distance_to_point(x, y); in the manual.

  • E) Surround the outside of a room with walls. Create a ball that bounces around the room. Have some objects in the room that require four hits of the ball to be destroyed, changing the sub image each time it’s hit. Note: You can use the D&D Bounce in the jump section of the Move tab.

  Appendix 12 Rooms

  Rooms are where you place your instances and where the action happens. A simple room may have an enemy instance and player instance – and maybe a few platforms to jump on – where the player and enemy try to shoot each other.

  Instances are placed in the room, which then interact with other instances, keypresses, and mouse

  events.

  The room editor is a very powerful visual layout tool, and has many useful features like views, creation codes, and tile settings.

  It allows you to place objects in the room where you want them. You can also do the following:

  Set room creation code (GML that runs on room start, which happens after the Create Event of instances already in the room)

  Set backgrounds

  Set views

  Turn physics on or off

  Set the room size and name

  Set and add tiles

  Most games have one than one room. You can use different rooms, for example:

  Splash Screen – Defining variables and showing your company’s logo

  Menu – Where a player selects a level to play

  Shop – Where weapons and upgrades can be purchased

  Game Levels – Where the main game action takes place

  Boss Levels – Special level between levels – usually harder than standard levels

  Game Over – When a player completes the game or loses all lives

  Most rooms will have some form of background, from the basic static background to moving parallax backgrounds.

  Create a new GameMaker Studio 2 project, and load in a new background. Backgrounds are loaded in the same way as sprites, for example as shown in Figure A_12_1:

  Figure A_12_1: Loading a sprite to use as a background

  Note: For naming purposes, use something bg_1 so you know it will be used as a background

  You can set this background in a room. Create a new room, room_example, and select the background layer, and set the room dimensions as 800 by 400, set the background image as the background you just loaded in, shown in Figure A_12_2:

  Figure A_12_2: Setting a background

  Backgrounds are dealt with in more detail in appendix 14.

  Next we’ll look at the views tab. Start a new project.

  You set a view so it only shows part of the room at any one time. This is useful if you have a large room and only want to show the part where the player is, for example. Create a new project.

  Create new object, obj_player and create and assign a red sprite (32x32 is fine) for it. In a Step Event put:

  x+=4*(keyboard_check(vk_right)-keyboard_check(vk_left));

  y+=4*(keyboard_check(vk_down)-keyboard_check(vk_up));

  Create another object, obj_wall, and create and assign a blue sprite for it. No code is needed for this. Open room0 and set the room width and height to 2000 each. Place one instance of obj_player and multiple of obj_wall. It doesn’t matter too much where you place them. An example is shown in Figure A_12_3.

  Figure A_12_3: Showing instances placed in room

  Set up a view, as shown in Figure A_12_4. This will create a view that keeps the player visible on the screen.

  Figure A_12_4: Setting up view to follow an object

  An example for this is in the resources folder for appendix 12

  A view can be used to show part of the room. For example you may have a huge level that’s 2000 by 2000 pixels – a view can be used just show a part, maybe 800 by 400 at any one time.

  There is a number of built-in functions for backgrounds and views. If you’re feeling adventurous, look these up in the manual, by pressed F1.

  There’s a ton of GML for using with rooms. The main ones are these:

  This one will take you to a named room:

  room_goto(room_name);

  This GML will take you to the next room (as shown in the resource tree):

  room_goto_next();

  This GML will take you to the previous room (as shown in the resource tree):

  room_goto_previous();

  You can restart the room using:

  room_restart();

  New rooms can be created by right clicking on Rooms in the resources tree.

  You can name a room by right-clicking on it and selecting rename.

  Basic Projects

>   • A) Make a splash screen with a background that shows for 5 seconds, plays a sound, and then goes to a new room.

  • B) Create a level select screen that has 3 buttons that each go to a different room. Make the buttons change colour when a mouse is over them. Draw the level as text in the middle of each button. Remember to set up text drawing correctly.

  Advanced Project

  • C) Create 2 rooms, A B. Visualize them as:

  A

  B

  Make the player wrap up and down in each room.

  Make it so a player object can move from one room to the next. So if the player moves off the right of room A, the player will appear at the same Y location in room B, but on the left of the room; and, if the player moves off the right of room B, the player will appear at the same Y location in room A, but on the left of the room. Do this for moving left also.

  Appendix 13 Backgrounds

  Backgrounds are one of the basic resources that are used for rooms in which the game takes place. Backgrounds can be made of one large image or tiles of a smaller image. Additionally, you can have static and moving backgrounds in GameMaker Studio 2. These backgrounds can be set using the room editor, or in GML. You can also set and change backgrounds using GML.

  Backgrounds are images that show in the room but do not have any direct interaction with objects. You can combine two or more backgrounds to create a parallax scrolling effect. Some uses of backgrounds include:

  Splash screens

  Backgrounds for levels

  Moving backgrounds for infinite runner type games

  Let’s make an example by creating a new project. We are going to create a moving background. This is useful as it creates a more professional look to the game, and it is useful for scrolling type games. Load in a background from the resources, and name it as bg_wire. Set it as shown in Figure A_13_1.

  Figure A_13_1: Setting up a background

  If you now test you will see that the background now moves to the right.

  If you wanted to move the background to the left, you can set the Horizontal Speed to a negative value, for example -2.

  You can tile a background as shown in Figure A_13_2:

  Figure A_13_2: Setting background to tile

  You can change the horizontal and vertical (x and y) positions using code. For example Create Event:

  /// @description setup

  yy=y;

  Step Event:

  /// @description Move bg

  yy=yy+2;

  layer_y("Background",yy);

  The above code would move by 2 pixels per step.

  Note: These don't have to be in the Step Event.

  You can have multiple backgrounds, which can be used to great effect.

  You can create a new layer, by clicking where shown in Figure A_13_3:

  Figure A_13_3: Creating a new background layer

  You can set a background visible or not with:

  layer_background_visible(bg_name,true);

  Basic Projects

  • A) Make a program that changes background shown if keys 1-3 are presed.

  • B) Make a tiled background that scolls up and left.

  Advanced Project

  • C) Create a tiled background that move in the opposite direction to arrow key presses. (effects like this are used in a lot of styles of games)

  Background assets are available for this in the downloadable resources.

  Appendix 14 Sounds

  Sounds and music are very important in games. The correct style of music can set the scene for the game: a horror-themed game would require different music than an RPG. Sound effects and voices can provide feedback too. For example when you buy an item in a shop, you want audio confirmation the purchase has gone through. When you fire a weapon or swipe a sword you want to hear a reassuring sound. This section serves as an introduction to playing sounds and music in GameMaker.

  A few sound resources are included in the resources download for you to play with. Sounds can be used for the following:

  Explosions effects

  Button clicks

  Giving player feedback using voices

  Playing background music

  Jumping and landing effects

  Collision sounds

  You can create a new sound by right clicking where shown in Figure A_14_1:

  Figure A_14_1: Creating a new sound

  Load in the sound effect, bells from the resources folder for this appendix, and name the sound snd_bell; this step is shown in Figure A_14_2.

  Figure A_14_2: Naming and loading a sound

  Create an object and place this in the Step Event:

  /// @description Play sound on X

  if keyboard_check(ord("X"))

  {

  audio_play_sound(snd_bell,1,false);

  }

  The snd_bell is the sound file to play, 1 is the channel priority, and false stops the sound from looping.

  Place one instance of this object in room0.

  Test this game, when you press the X key, the sound effect will play.

  Add a new sound, snd_music_1 and load song_1 from the resources. Put the following code in to a Create Event of object obj_example.

  /// @description Play music on loop

  audio_play_sound(snd_music_1,1,true);

  Test again, and a background track will play. Check that you can hear the bell sound over the background music.

  You can lower the volume of the background music, and make the sound effect easier to hear:

  bg_music=audio_play_sound(snd_music_1,1,true);

  audio_sound_gain(bg_music, 0.2, 0);

  You can check if a sound is playing, and stop it if it is:

  if (audio_is_playing(snd_music_1))

  {

  audio_stop_sound(snd_music_1);

  }

  An example for this, including pause and resume, is in the resources.

  Some additional functions include:

  audio_pause_sound

  audio_resume_sound

  audio_stop_all

  An example using the pause and resume functions could look like the following, which would pause the sound on keypress P and resume on keypress R:

  Create Event:

  audio_play_sound(snd_music_1,1,true);

  Step Event:

  if keyboard_check_pressed(ord(“P”))

  {

  if audio_is_playing(snd_music_1)

  {

  audio_pause_sound(snd_music_1);

  }

  }

  if keyboard_check_pressed(ord(“R”))

  {

  if audio_is_playing(snd_music_1)

  {

  audio_resume_sound(snd_music_1);

  }

  }

  You can also stop all sounds. Stopped sounds cannot be resumed.

  Here is an example of this being used, which would stop any and all audio when X is pressed:

  Create Event:

  audio_play_sound(snd_music,1,true);

  Step Event:

  if (keyboard_check_pressed(ord(“X”)))

  {

  audio_stop_all();

  }

  Basic Projects

  • A) Make a program that can play, pause, and resume a song.

  • B) Play one of four sound effects at random when a moving ball collides with a wall.

  Advanced Projects

  • C) Play some music. Adjust the volume based on y’s mouse position.

  (see audio_sound_gain)

  Appendix 15 Splash Screens & Menu

  A splash screen is usually shown when a game starts up. It’s an ideal time to initialize variables and load external resources. A splash is usually a single room and a control object. It will load / set the data and then take you to another room, for example a menu or the main game level. A good way to show a splash screen is to create a new room, assign a background or sprite, and then move it to the top of the room’s resources tree, for example, as shown in Figure A_15_1.
r />   Menus are great ways to allow the player to select a difficulty, turn sound on or off, or visit an unlocked level.

  A completed game rooms tree may look something like this:

  Figure A_15_1: Showing an example of room order in resource tree

  Go ahead and these rooms as shown in the figure above.

  As you will see above, in Figure A_15_1, each room has a distinct name. The room at the top of the tree will be loaded first.

  Start a new project within GameMakerStudio 2.

  Create a new object, obj_splash_screen. With the Create Event code and splash sprite loaded and assigned (in resources for this appendix):

  global.level=1;

  lives=5; //Set initial lives to 5

  score=0; //Start with a score of 0

  global.bonus_score=0; //Set with a value of

  health=100; //Start with full health

  alarm[0]=7*room_speed; //Set alarm for 7 seconds

  Alarm[0] Event code:

  room_goto_next();

  Place one instance of this object in room_splash and test.

  Start a new project.

  Go ahead and load in the project file HW5B.

  Rename room0 as room_level_select. Delete obj_control.

  Create some extra rooms so the resource tree looks like that in Figure A_15_2:

  Figure A_15_2: Showing changes to resources tree

  Create an object obj_splash with the Create Event code:

  /// @description Set up

  global.level=2;

  room_goto_next();

  Place one instance of this object in room_splash.

  Change the Step Event of obj_button_1 to:

  /// @description control

 

‹ Prev