Game Prototyping: Game Maker tutorial 4 – Page 1

Game Maker Tutorial 5: 1945 Part 2

I. Key concept: “variables” we are using: Local and Global Remember a Variable is a is a value that can be changed or adjusted,

1. Local (or “Instance”) Variables apply to a specific instance of an Object, 2. Global Variables apply to variables not related to a specific instance of an Object, but to the overall game itself,

B. All variables must have a unique name.

1. In Game Maker, that name can only consist of letters and underscore symbols ( _ ).

2. Variable names are case sensitive, so the variable Can_Move is not the same as can_move.

2. Creating the player’s airplane

1. You’ve got the scrolling background. - add the plane that the player will control,

Spinning 2.Make a Sprite for it: Propellers Add, -Add Sprite, look for the graphic myplane and rename it To give the propellers spr_myplane. the illusion of rotating we use a  new to this Sprite, and that is relocate its Origin This is one Sprite, composed of Sprite consisting of coordinates. Set both the X and Y coordinates for the three images that play in three images that are Origin to ‘32’ (as shown below) sequence. See sidebar. exactly the same except for the  notice that the ‘crosshairs’ in the picture have moved to the center of the image. propellers.  Sprite is the center of the plane.  any bullets it fires come from the center of the plane, not from the left side (the default x coordinate of ‘0’).

1. Sprite Properties page that looks like this: Game Prototyping: Game Maker tutorial 4 – Page 2

3.. Use Add, Add Object, choose the newly created spr_mylpane for its Sprite and name it obj_myplane. You Need Depth · Give it a Depth of ‘–100;’ that will make sure that the plane flies above bullets and Dealing with the other incidents of Objects in the game that we will create later. The lower the Depth, the Depth property later it’s drawn each time the screen is refreshed – meaning its draw “on top of” other effectively is very stuff. important for many games. Therefore, you had ·Now to set the plane’s motion. When the player does nothing, the plane will not move better understand (the background still moves, but not the plane). If the player presses one of the four arrow keys, the plane should move in the appropriate direction. The trick is to avoid having the plane move outside the Room. To do this, we will set the controls for its motion rather than simply giving the plane a speed. This is done as follows:

·Select the move tab, then click the Add Event button. ·Add a Keyboard Event and select the key from the pop-up menu. Right now, this means if press the key, some Action that we have yet to define will happen.

But we don’t want it to fly off the screen to the left, so we need an Action to check if the variable x (its horizontal coordinate) is larger than 40. (This is done in a similar way that we checked the y coordinates of the islands above.)

·Select the control tab and drag the blue octagon VAR (‘If a variable has a value’) over into the Action box. Set the controls to variable: ‘x;’ value: ‘40;’ and operation: ‘larger than.’ The value of ‘40’ means 40 pixels from the left edge, which gives a little bit of margin there that the plane won’t fly into.

So, now we’ve set an Action to see if our plane is more than 40 pixels from the left edge. If this is true, we can actually move the plane a little bit to the left (because there is Room to move it slightly to the left). If this Action came back ‘false,’ i.e., the plane was less than or equal to 40 pixels from the left, no movement would occur. Of course, we must actually create an Action that moves the plane if it is able. To do that, select the move tab and drag the ‘Jump to a relative position’ icon over to the Action box. In the pop-up window, set the value of x to ‘–4’ (meaning that the plane will move 4 pixels to the left every step), and leave the y coordinate at ‘0’ (we don’t want the plane to change its vertical motion).

Important: check the ‘Relative’ box, since you want the plane to move in relation to it is current positioned! See the illustration on the left.

right arrow key You can find this · phase of construction in file 1945_1.gm6. Do your own work, please. Only use this version for comparison in case something isn’t Game Prototyping: Game Maker tutorial 4 – Page 3

· variable x this time should be ‘room_width–40’ operator ‘smaller than’. (That is, 40 pixels to the left of whatever the width of the ·Room happens to be is the ‘safety margin’ into which the plane will not fly.) If so, have its jump Action ‘Relative’ to an x of ‘4’ and a y of ‘0’ (i.e., it moves 4 pixels to the right).

4 The vertical motion is similar, but now we’re working the y axis. For the up arrow key, the Variable Action is ‘if y is larger than 40’ (i.e., ‘y,’ ‘40,’ ‘larger than’) and the Jump Action is ‘x = ‘0;’ y = ‘–2;’ and ‘Relative.’ positive y-direction is downward (i.e., the ‘0’ y value is at the top of the screen); to move upward you must subtract from its relative position (and thus move it close to ‘0’). Jump Action means that pushing the up arrow key will advance the plane up the y axis by 2 pixels per step (when added to the speed of the scrolling background, it will have the appearance of moving forward 4 pixels per step, as you will soon see).

5. For the down arrow key, the Variable Action is ‘if y is smaller than ‘room_height– 120’ (i.e., ‘y,’ ‘room_height-120,’ ‘smaller than’) and the Jump Action is x = ‘0;’ y = ‘2;’ and ‘Relative.’ room_height is another variable name built into Game Maker that means ‘however many pixels high the Room is along its y axis.’ But why did you set this value to –120 pixels? Well, this large margin along the bottom of the screen is where you will be placing a nice display panel a little later on; we’re just planning ahead. 

So, this down arrow key Jump Action means the plane will down the y axis by 2 pixels per step (with the scrolling background, it will have the appearance of standing still because the background is also scrolling in that same direction and at the same speed).

That’s it! Place one instance of this plane Object in the Room. Fasten your safety belt and try flying your airplane over those scrolling islands. Be sure to fly it to each of the x and y coordinate extremes so that you get a feel for the actual gameplay area that you’ve just created.

Save your game file! The “Other” Way Rather than a Step Event with a -16 x variable, you could also use the Outside Room Event that can be found in the ‘Other’ Events to create the Action that destroys the bullet once it leaves the Room as we did for the fireballs in Evil Clutches.

C. Creating the enemy and weapons

1. What’s a scrolling shooter without enemies and shooting? Those items are next on our checklist, starting with the cannon.

First, bullet Sprite. - push the ‘Center’ button in the ‘Origin’ box of the Sprite Properties window. Look at the picture Exaggerating? Okay! Can you guess what’s next? That’s right, a bullet Object using that Sprite! You To make the bullet can even use the default depth of ‘0’ because that will draw it above the island more spectacular, and below the plane. it is very large. Exaggerating things is often Game Prototyping: Game Maker tutorial 4 – Page 4

The behavior of this bullet Object Add a Create Event, with the Action to Set the vertical speed to ‘–8’ to make it move upwards.

To avoid having more and more bullets flying around off-screen (this will slow down the computer’s processor speed if you have it keeping track of every bullet you ever shot in a game forever), we must destroy each bullet when it leaves the Room. This is easy to do; create a Step Event and makes its Action Variable a test whether the variable y (the bullet’s vertical position) is smaller than –16 (i.e., 16 pixels above the top of the screen; enough Room for the bullet image to move completely out of sight off the top edge). If so we destroy the instance Object;

·go to the main1 tab and drag the light blue Destroy the instance icon over to the Action box; set the pop-up window to ‘Self.’

We want the user to fire the bullet when the key is pressed. As in most shooters, the plane should keep firing bullets as long as the key remains pressed. Yet, we don’t want too many bullets flying around at the same time as this would make the game too easy. We only want to allow the player to fire two bullets every second, that is, one bullet every 15 Steps.

·edit our airplane Object (obj_myplane), since that is what’s firing the bullets. Start by adding a Create Event. From the control tab, drag the ‘Set the value of a variable’ icon into the Action box. Create a new variable called can_shoot and set this variable to 1 (‘1’ means “true” or “yes” to a computer; ‘2’ means “false” or “no”), indicating that we can indeed shoot a bullet when this plane is created (i.e., at the beginning of the game).

·add a Keyboard Event that uses the bar. The Actions here are tricky to explain, so pay close attention!

To begin with, we need an Action to test if the variable can_shoot is set to ‘1,’ because if it is we can create a bullet and send it on its way. ·Drag the blue octagonal ‘If a variable has a value’ icon into the Actions box and set the variable to ‘can_shoot,’ the value to ‘1,’ and the operation to ‘equal to.’

Next, three things happen if we can shot a bullet, so to make sure that they all happen only when a bullet is fired we need to put them in a

·BLOCK OF CODE. Drag a copy of theupward-pointing triangle labeled ‘Start of a block’ over to the Action box. That makes sure that these Actions are always performed together.

first of the three Actions that happen together is that we need to create an instance of the bullet. On the main1 tab, drag the light bulb icon labeled ‘Create an instance of an Object’ over to the Action box. In the pop-up window, the Object you want to create is ‘obj_bullet.’ Now, where you want to create this is at x = ‘0,’ and y = ‘–16’ with the ‘Relative’ box checked to show that it starts at that spot in relation to obj_myplane. See the sidebar, “Bullets Under Planes,” for an explanation of why you use these settings. Game Prototyping: Game Maker tutorial 4 – Page 5

second of three Actions is that we need to set the variable can_shoot to ‘0.’ On the control tab, drag the square ‘Set the value of a variable’ icon over to the Actions box. The variable name is ‘can_shoot’ and the value should be set to the default of ‘0.’ By doing this, the plane can no longer fire a bullet.

Bullets Under Planes Review obj_myplane is 32 pixels square, but we earlier defined its origin point at the image’s center 16 pixels in. By making the relative bullet creation position x = ‘0,’ the bullet will fire from the center of the airplane graphic. By making the relative bullet creation position y = -16, the bullet will be drawn 16 pixels above the airplane’s centerline – that is, right on the top edge of the airplane graphic just ready to pop out. And since the depth of the bullet is greater than the plane, the plane will be drawn over the bullet as the bullet moves forward from ‘under’ the plane image.

third of three Actions we need is to set an alarm so that the plane can shot bullets again.

From the main2 tab, drag the ‘Set an alarm clock’ icon over to the Action box. In the pop-up menu, set the number of steps to ‘15’ (15 Steps = 1/2 a second of real time) for ‘Alarm0.’ The count ticks down 1 step every 1/30 of a second; when the alarm reaches 0, the value will be reset to ‘1’ and the player can fire the gun once again.

tab labeled control, drag the downward-pointing triangle labeled ‘End of a block’ into the Action box. The Key Press Event should now look like this:

Note that you can change the speed with which this plane can shoot by changing the value of this alarm clock. See the sidebar, “Wait! Don’t Shoot!” on the next page.

Wait! Don’t Shoot! In some games you can shoot faster if you are willing to constantly press the space key. This can be achieved by using a similar set of Actions with a Key Press Event, but this time without a check for the can_shoot variable. This is a more advance variable code we are going to work on called Wingman YOU HAVE TO WAIT sorry Game Prototyping: Game Maker tutorial 4 – Page 6

·Action for obj_myplane’s Alamr0, but no Event for it! Add Event; Alarm:Alarm0. For its Action, go to the control tab, drag the ‘Set the value of a variable’ over to the Actions box, and set the value ‘can_shoot’ to ‘1.’ This means that, when the alarm goes off, the plane’s gun variable is set back to ‘1’ (or ‘true’) meaning that it can shoot again.

2. Now let’s make the first enemy unit – a small plane that simply flies downward. It does not shoot, but when it collides into the player’s plane it inflicts damage upon it.

create a Sprite (spr_enemy1) and an Object (obj_enemy1) for the enemy plane. The default settings are fine.

Add a Create Event to the Object and for its Action, Set the vertical speed to ‘4’ to make the plane fly downwards.

3.

Now, when the plane reaches the bottom of the Room, we want to make it reappear at a random place along the top of the Room (in exactly the same way as we did for the islands). You should know how to do this by now, but if you forgot,

·add a Step Event. - control tab, right-click the blue octagonal ‘If a variable has a value’ icon over to the Actions box. Set the variable to ‘y’ (because we’re concerned with the vertical row), and the value to ‘room_height’ – in fact, set the value to ‘room_height+32.’ In that way, the plane will go 32 pixels below the bottom of the Room height value before resetting itself (and give the player a little more time between waves of enemy fighters). The next Action is on the move tab where a ‘Jump to a given position’ icon is dragged over; set x to ‘random(room_width)’ and y to ‘–16’ (that is, 16 pixels above the top edge of the Room).

·Collision Events for obj_enemy1: a collision with the bullet that will destroy the enemy plane (and rack up some points), and a collision with the main plane that (for now) destroys it and ends the game.

IMPORTANT STUFF PAY ATTENTION Game Prototyping: Game Maker tutorial 4 – Page 7 but let’s create a small explosion sound for this collision: Add, Add Sound, then find the sound file to create ‘snd_explosion1.’ Next, we need an explosion Sprite, so add ‘spr_explosion1’ – be sure to Center its Origin Point as well (16, 16) to match the enemy plane Object. The Sprite Properties window should, in part, look like this:

Naturally, the last step here is to create an Explosion Object (obj_explosion1) and assign it spr_explosion1. Go ahead and do this. It only needs one Event. Press the Add Event button and select ‘Other;’ from the pop-up menu, select Animation End. The only action to be taken once the animation ends is to ‘destroy’ it (i.e., remove it from the screen). To do this, tab to the main1 page and drag the ‘Destroy the instance’ icon over to the Actions box; you want it to destroy itself. Game Prototyping: Game Maker tutorial 4 – Page 8

Okay, now we need to edit the Object for the enemy plane (obj_enemy1). Double click on it to open up the Object Properties window.

First Collision Event with obj_bullet by pressing the Add Event button and selecting Collision with obj_bullet.

a) Play the explosion sound effect. Tab main1, drag the sound effect icon to the Actions box, select ‘snd_explosion1’ and leave the loop setting at ‘false.’ b) Destroy the bullet. Drag the ‘Destroy the instance’ icon over to the Actions box, but this time, set the radio button to the ‘Other’ instance. That means, it won’t destroy obj_enemy1, but the other thing that collided with it (the bullet, in this case). c) Create the explosion Object by dragging the ‘Create an instance of an Object’ icon over to the Actions box, choosing obj_explsion1 for the Object and setting the coordinates to x: ‘0,’ y: ‘0’ (i.e., centered) ‘Relative’ to this Object (obj_enemy1). d) The next action is not to destroy the enemy plane, but to teleport it back off the top of the Room and send it back down. You’ve done this before: go to the move tab and select the ‘Jump to a given position’ icon and drag it over to the Actions box. Then set the x axis to ‘random(room_width) and the y axis to ‘–16’ (that is, 16 pixels above the top edge of the Room). See the sidebar on immortality. e) Finally, we have to give the player credit for hitting this enemy plane with a bullet. On the score tab, drag the ‘Set the score’ icon over to the Actions box and set it Relative to ‘5.’

When you’re done with the bullet Collision Event for the enemy plane, the Object Properties window should look like this: Game Prototyping: Game Maker tutorial 4 – Page 9

Colliding with a bullet is done Collision Event in case this enemy plane crashes into the player’s plane.

(bigger) explosion Sprite, so create spr_explosion2. Don’t forget to center its Origin Point., so create snd_explosion2. Go ahead and make those using the default settings.

Object for this explosion, so create obj_explosoin2. Perform Add Event; Other: Animation End.

The High Score First, go to tab main1 and after this Animation End Event, have this Object ‘Destroy Screen the instance’ of itself. On the high-score screen, the player can Next, to ensure that the final explosion sound has a chance to play completely, type his or her name on the game’s list ·tab main2 and have use the ‘Sleep for a while’ action. ‘1000’ milliseconds are plenty, when a high score is and leave the redraw default set to ‘true.’ You’ve just created a second’s pause before the achieved. next Action. This is a built-in in feature in Game Maker. After that pause, you want to show the High Score screen. Select the score tab and drag Note that you can modify the appearance ·Show the highscore table’ icon into the Actions box. In the pop-up window, change the of the High Score background to ‘back_water.’ The other defaults are fine, but I would change the font to Screen quite a bit. You 12 point and bold. can use a nice background image, Finally, on the main2 tab, drag the ‘Restart the game’ icon over to the Actions box, and that should do it for now. This Animation End Event should look like this:

That’s almost everything, but we’ve neglected to take care of the instance of obj_myplane when it collides with an enemy plane. We can’t just leave it there on the screen; we have to change it into the explosion graphic and sound. Game Prototyping: Game Maker tutorial 4 – Page 10

Open up the Object Properties window of ·obj_enemy1 and add a Collision Event with obj_myplane. Now, let’s set the actions:

·In the main1 tab, look for the ‘Change the instance’ icon and drag it over to the Actions box. In the pop-up menu, set the radio button to ‘Other’ (because we’re going to remove the instance of obj_enemy1, we’ll change the instance of obj_myplane into the explosion; that is, replacing the player’s plane graphic on the screen with the explosion graphic). Change it into ‘obj_explosion2’ and leave the Perform Events default at ‘not.’

Okay, we’ve changed the picture, now we need the ‘kaboom!’ Drag the ‘Play a sound’ icon over to the Actions box and select ‘snd_explosion2.’

Last the collision between obj_enemy1 and obj_myplane is to remove the graphic of obj_enemy1. ·drag the ‘Destroy instance’ icon over to the Actions box and leave it set to ‘Self.’ The screen should look like this:

3. We’ve got our scrolling background, our player’s airplane, and our enemy airplane all set to go. We just need to populate our Room with enemy airplanes and try it out.

Instead of just plunking down an instance of an enemy airplane in the Room, we need to create several of them over time, like any good arcade game. We’ve already set up obj_enemy1 to ‘regenerate’ every time it is destroyed, so what we want to do is slowly add more and more copies of it into the game so that the player is eventually overwhelmed by their sheer numbers (in classic arcade game style).

Now we use the Game Design of the RULES Game Prototyping: Game Maker tutorial 4 – Page 11

A Box of Rules This “Controller Object” is just an invisible box of rules. Most of the rules in games that are made using object oriented programming (like Game Maker uses) are assigned to individual game pieces (i.e., “objects”). In this case, we’re creating an invisible Object that is not a game piece; it’s just something to old rules that don’t necessarily belong to an existing game piece.

The Effect of the To do this, we’re going to create a special “Controller Object” (See “A Box of Rules”) that Enemy Controller will control the creation of enemy planes in the game. Object The effect of this Object ·Create a new Object called controller_enemy, and uncheck the Visible box. We want this is that one enemy plane “enemy plane generator” Object to be invisible to the player during the game (it’s for our is created at the “internal use only”). Because this Object is invisible, it requires beginning of the game. no Sprite. 200 steps later (about seven seconds), a ·For this Object, add a Create Event. We need to initially create a single enemy plane second enemy plane and place at a random location just above the Room appears. 15 seconds after that, a ·main1, ‘Create an instance of an Object,’ Object = ‘obj_enemy1,’ x = third plane appears, ‘random(room_width),’ y = ‘–16’). This will be the first enemy airplane brought into and so on forever. play. The reason that the second plane appears faster than the others is ·Set an Alarm Clock (for Alarm0) to ‘200’ steps. (main2, ‘Set an alarm clock,’ self, steps = 200, for Alarm0). That will create a second enemy aircraft after about 7 seconds have elapsed.

·action to start ticking down Alarm0, but no Event for that Action! Add an Alarm: Alarm0 Event and for its Actions you want to create another instance of the enemy plane just like before (main1, ‘Create an instance of an Object,’ Object = ‘obj_enemy1,’ x = ‘random(room_width),’ y = ‘–16’).

·Action you want for this Event is to reset clock on Alarm0 to 500 steps (main2, ‘Set an alarm clock,’ self, steps = 500, for Alarm0). This will keep adding another new enemy plane about every 15 seconds.

So, now we don’t need to place an instance of an enemy airplane (obj_enemy1) in the Room, ·Place an instance of our new Controller Object (controller_enemy) since it controls the creation of enemy planes. Go ahead and place one instance of the Object controller_enemy anywhere in the Room (it will appear as a red question mark inside a blue ball on the screen to show you, the Level Designer, that you’ve placed an instance of an invisible Object in the Room).

SAVE AS LAST NAME_1945_2 This game should show an enemy flying at is.

This finishes the second version of 1945. We have a playable game with an enemy flying at is. Go ahead and run it to see for yourself (Run, Run normally). There is no movement of the plane yet or bullets Game Prototyping: Game Maker tutorial 4 – Page 12