Game Maker Tutorial 4: 1945

Game Prototyping: Game Maker tutorial 4 – Page 1
Game Prototyping: Game Maker Tutorial 4: 1945
Lets make more enemies
More enemies
Now you get to add three new types of enemy planes to your game and introduce
them over time during play. The first new enemy aircraft shoots bullets straight
down its path. The second fires bullets toward the player’s aircraft. The third
does not fire bullets, but flies up from the bottom edge of the Room which makes it very difficult to avoid
and shoot.
1. To create the first new type of enemy plane, we must make a new Sprite for it. However,
since that graphic will work almost exactly like spr_enemy1, you can copy that Sprite
(right-click on it and select ‘Duplicate’ from the pop-up). Then Load Sprite ‘enemy2’ and
name it spr_enemy2. Simple, huh?
Now Duplicate obj_enemy1 and let’s use that as the starting point for our new enemy
aircraft Object. Change its name to obj_enemey2 and select its appropriate Sprite.
Because this plane is going to shoot back, it should be worth more points when the player
shoots it. Double-click on its Collision Event with obj_bullet, the double-click on the Set
score relative to 5 action and give the player a higher score – let’s set it relative to ‘10’
points, shall we? Click OK because now we have a feature to add.
2. This enemy plane is supposed to shoot straight down its flight path. To do that, first you
must add a bullet Sprite and a bullet Object for it to shoot. Duplicate the Sprite spr_bullet
and change its name and image into spr_enemybullet1 (just as you did to make the
second enemy airplane by duplicating spr_enemy1 a moment ago).
Duplicate obj_bullet and change its name and Sprite to ‘obj_enemybullet1.’ This bullet
needs to fire down the screen, not up it. So, in its Creation Event, double-click on ‘Set the
vertical speed’ to edit its value from ‘–8’ (the value duplicated from obj_bullet) to ‘8.’
There, now the bullet will move at the right speed in the right direction.
In the Step Event, we still need to ‘Destroy the instance,’ but not when it crosses the top
of the screen (as is currently set from duplicating obj_bullet), but when the bullet leaves
the bottom of the screen. Double click on the Variable Action ‘If y is smaller than –16.’
Leave the variable ‘x’ alone (because we’re still talking about vertical movement of the
bullet to a horizontal edge, we just need to change that edge from the top to the bottom of
the Room). Change the value for ‘y’ to ‘room_height+16’ and the operation to ‘larger
than’ This means 16 pixels north of the top edge of the screen… this is a 32-square pixel
Sprite, remember, with its Origin – the point where the computer controls it from – set to
its center, which is 16 pixels in. The display will look like this:
Finally, we must create a Collision Event with the player’s aircraft that will destroy the
instance of this bullet and ding the player’s plane. That means you must add a Collision
Event with ‘obj_myplane.’ For its Actions, first we need to play the small explosion
sound effect: tab main1, ‘Play a sound,’ ‘snd_explosion1.’ Then we need an Action to
destroy the instance of obj_enemybullet1: ‘Destroy the instance,’ ‘Self.’ That was easy.
Copyright © 2005 by Alan Emrich. All rights reserved.
Game Prototyping: Game Maker tutorial 4 – Page 2
And finally the damage to the main aircraft: tab score, ‘Set the health,’ value = ‘–5’ and
check the ‘Relative’ box (i.e., this bullet will reduce the player’s current health by five).
This display should look like this:
Click OK; you’re done here.
Game Maker
Die Rolls
As you learned in the Evil
Clutches tutorial, you can
set how many sides the die
has, but there is always
only one chance per step
to make a roll.
So, in this game, you set
the virtual die to be a d30
(that is, a 30-sided die).
Thus, each step, there is a
1-in-30 chance of the next
Action listed happening.
With 30 steps per second,
that means each instance
of this enemy airplane will
fire, on average, about one
bullet per second.
To make an event more
likely to occur than 1-in-2
(a 50/50 shot), use the
‘NOT’ checkbox at the
bottom, which means a
chance for the next Action
not happening.
3. We’ve got a new enemy plane and we’ve got a new bullet for it. Now we have to get the
plane to shoot the occasional bullet. For that, we have to open up obj_enemy2 again and
modify its Step Event. What’s there already (copied from obj_enemy1) is good: that will
reset the plane when it flies off the bottom of the screen back to the top for another attack
run – we want to keep that.
What you’re doing is adding a chance for it to shoot each Step as well. To create a chance
for something to happen, Game Maker uses the “virtual die” variable. Tab to control and
right-click the ‘With a chance to perform next action’ die icon to put it over to the
Actions box. In the pop-up window, set the number of sides for this virtual die to ‘30’ See
the sidebar on Game Maker die rolls. Not surprisingly, the next Action (that has a 1-in-30
chance of occurring each Step) is to create an instance of the bullet. Tab to main1,
‘Create an instance of an Object,’ it applies to ‘Self.’ Set the Object to
‘obj_enemybullet1,’ and create it where x = ‘0’ y = ‘16’ ‘Relative’ to this Object. That
means the bullet will appear at the center of obj_enemy2 graphic (x = 0) and 16 pixels
beneath is Origin point (y = 16) which is right where you want it to appear).
The Object Properties should look like this:
4. We’ve got the plane, the bullet, and the plane shoots it. Cool! Now we just need to populate
the game with this new model enemy aircraft, but we want to gradually add it during play.
For this, we already have the controller_enemy Object standing by, so open it up and let’s
perform a little surgery there to add this new model enemy aircraft.
The Create Event and Alarm0 Event are both fine. They’ll keep producing obj_enemy1
just as before. What we want to do is expand the current Create Event to include a second
Alarm Action (for ‘Alarm1’) and set it to go off in about to about 33 seconds. Go to tab
main2, ‘Set an alarm clock,’ the number of steps is ‘1000’ in alarm number ‘Alarm1.’ It
will look like this:
Copyright © 2005 by Alan Emrich. All rights reserved.
Game Prototyping: Game Maker tutorial 4 – Page 3
Brrrrring!
Alarm 1 Just Rang
You’re setting Alarm1 to
go off for the first time
about 33 seconds after
the game starts (in the
Create Event with the
Set Alarm 1 to 1000
action; at 30 seconds per
step, 1000 steps is
roughly 33 seconds).
When the Action for this
Event is to reset Alarm1
to go off every 500
Steps; that means a new
instance of the
obj_enemy2 plane will
appear about every 17
seconds from then on.
We’re doing this is reverse order. We now have an Action to initially set the time for
Alarm1, what we need is an Alarm Event to define what happens when the alarm goes off.
To do that, add an Alarm: Alarm1 Event. The Actions for this Event are to create an
instance of our new type of enemy aircraft (obj_enemy2) and reset the alarm clock to go
off again in another 500 steps (roughly 17 seconds). To do that, tab to main1, ‘Create
instance of an Object.’ In the pop-up window, select ‘obj_enemy2,’ it’s horizontal entry
points is randomly across the top of the Room, so set x = ‘random(room_width),’ and its
vertical entry point should be just above the edge of the screen, which means that
y = ‘–16.’ The pop-up window should look like this:
Copyright © 2005 by Alan Emrich. All rights reserved.
Game Prototyping: Game Maker tutorial 4 – Page 4
Check it Out
Your New Enemy is
Ready for Testing!
You can fire up the game
now and check out this
new aircraft type in action.
Be patient! It will be about
half a minute before the
first one appears.
Note how they only shoot
bullets willy-nilly –
sometimes in bunches,
sometimes none at all.
That’s because each
bullet is the result of a
random die roll with a 1-in30 chance of happening. It
could happen multiple
times in a row, or never –
it’s all random!
Now go back to tab main2 and ‘Set an alarm clock.’ Set Alarm 1 to ‘500’ steps. When
you’re done, this Object Properties for controller_enemy will look like this:
See the sidebar “Check it Out.” Your new model enemy aircraft is ready for action!
Copyright © 2005 by Alan Emrich. All rights reserved.
Game Prototyping: Game Maker tutorial 4 – Page 5
5. One new model of enemy aircraft down and two to go.
Aiming for
the Target
A New Variable
In obj_enemybullet2’s
Create Event, we use the
Action to ‘Move towards a
point.’ But what point (or
“position”) should we use?
Well, we want to shoot
towards the position of the
main plane. Thus, we need
to know its x and y
coordinates at this time, the
moment of this bullet’s
creation.
Game Maker makes this
easy. To get the value of a
variable in another instance,
we precede the variable
name with the name of the
Object. So, for example, we
use ‘obj_myplane.x’ to
indicate the value of the x
coordinate of the main plane.
Note: when there are
multiple instances of this
Object in play, we get the
value of the variable in the
first instance only.
Our next new type of enemy aircraft requires a new Sprite for the plane and another for a
new bullet graphic. You should be able to create these
Sprites (spr_enemy3 and spr_enemybullet2) on your
own now, so go ahead and make them. (Hint: start by
duplicating spr_enemy2 and spr_enemybullet1 and then
putting in new names and graphics.) That took you,
what? 30 seconds? Good job!
Now for the new plane Object. Duplicate obj_enemy2
as your starting point for the new obj_enemy3 that
you’re about to create. Change the name and associated
Sprite; make sure the ‘Visible’ box is checked. Since
this plane is a more valuable target, open up the
Collision Event with obj_bullet and increase its score
value ‘Relative’ to ‘20.’ Click OK because you’re done
here.
See the “Aiming for the Target” sidebar for a complete explanation.
Next, there is the new bullet Object to create. Duplicate obj_enemybullet1 as your
starting point for this new Object (obj_enemybullet2) and change the name and
associated Sprite. Again, be sure it ‘Visible’ box is checked. We’ve got a lot of doctoring
to do here, so pay close attention! This new type of bullet works as follows:
In its Create Event, we’re going to move this bullet directly toward the player’s aircraft.
In other words, when this plane shoots, it is going to be an aimed shot right at the middle
of the player’s aircraft; if the player doesn’t move their plane, this bullet is going to hit it.
How do we achieve this aimed shot?
Game Maker does this with a new Action. First, delete the ‘Set the vertical speed’
Action. Select tab move, ‘Move in the direction of a point,’ and then plug in the
following variables on the pop-up window (as shown to the right): x = ‘obj_myplane.x,’ y
= ‘obj_myplane.y,’ and set the speed to ‘8.’ The Object Properties window for
obj_enemybullet2 should now look like this:
But wait! There is a moment in the game where there is no player aircraft in play (when it
is destroyed, before a new instance of that Object is created, there is temporarily no
obj_myplane on the board). If this ‘smart’ bullet is fired during that Step, it won’t be able
to ‘lock on’ to the target and you’ll get an error message. (That’s no good.) So here’s a
cool trick: first, we’re going to test to make sure that there is an instance of the main plane
and, if there’s not, we’ll set a default Action. Watch…
Select the control tab and look in the ‘Questions’ section. Drag the ‘If the number of
instances is a value’ icon over to the Action box for this Create Event. The question
you’re asking is, “If the quantity in play of the Object: ‘obj_myplane’ is ‘Larger than’
the number ‘0’…, then do the following action:” Now, there is no “following action”
currently in the Action box; what you want is the ‘Move towards point...’ Action to follow
this ‘If the number of instances is a value’ action. You do this intuitively, by dragging the
‘Move towards…’ Action below the ‘If the number…’ Action in the box so that now
Copyright © 2005 by Alan Emrich. All rights reserved.
Game Prototyping: Game Maker tutorial 4 – Page 6
you’re looking at this:
The “If Statement”
What you’ve just created
is a statement that tests
whether something is
true or not. If it is, one
Action occurs; if not, the
‘else’ statement indicates
that another Action
occurs.
In plain English, you
wrote: “If there are more
than 0 instances of
obj_myplane currently in
existence in the game,
then this bullet moves
towards that location; or
else move the bullet
straight down the screen
(like the others).”
Okay, that’s the “if this is true” part of logic. How do we tell this bullet what to do if this
is not true (i.e., there is no instance of obj_myplane currently in play)? Simple.
Every “If statement” can have an alternate outcome. In Game Maker this is known as an
“Else statement.” Drag the ‘Else’ icon in the “Other” section of the control tab over to the
Actions box. Now you’ve just said, “Or else, do the following action:” And for that
Action, go back to the move tab and ‘Set vertical speed’ to ‘8.’ In other words, fire a
‘dumb’ bullet like the other plane. If you got it right, the display will look like this:
Now, when the other bullets (obj_bullet and obj_enemybullet1) move off the screen, we
used a ‘Destroy the instance’ Action in a Step Event to destroy them. In that way, the
computer doesn’t have to keep track of every bullet forever and calculate its trajectory
through space (i.e., off the screen) forever. For those bullets, we just checked each Step
whether it went off the top or bottom of the screen, and if it did we destroyed it.
But this new ‘smart’ bullet might fly up, down, or sideways, so it could get tricky to test
whether it’s outside the Room or not. The old Step Event isn’t going to work anymore. Go
ahead and delete the Step Event (and all its Actions).
Fortunately, Game Maker has a special Other Event called ‘Outside Room’ that covers this
condition automatically. So add an Other: Outside Room Event and, for this new Event,
go to tab main1 and select ‘Destroy the instance’ of ‘Self.’ That’s it.
6. So, there’s our second airplane and its ‘smart’ bullet. The next thing we have to do is make
sure that this plane fires this bullet and at the right interval. Open up obj_enemy3 and in
its Object Properties window, select the Step Event and change the die roll chance to
shoot Action to 1 in ‘80’ (we don’t want too many of these ‘smart’ bullets flying around)
and the ‘Create instance of an Object’ Action to ‘obj_enemybullet2.’
After that, all that’s left is to handle the introduction of obj_enemey3 into the game itself.
For that, you need to open up controller_enemy and create a new Alarm: Alarm2 Event.
For its actions, tab to menu1, ‘Create an instance of an Object,’ and put Object:
‘obj_enemy3’ at x = ‘random(room_width)’ and y = ‘–16.’
Then tab to menu2 and create a ‘Set an alarm clock’ action for ‘Alarm 2’ at ‘1000’
steps. Now all we have to do is create the first one, and a new instance of obj_enemy3 will
be added to the game every 1000 steps (roughly 17 seconds). To create the first instance,
Copyright © 2005 by Alan Emrich. All rights reserved.
Game Prototyping: Game Maker tutorial 4 – Page 7
open up the Create Event for controller_enemy, add another ‘Set an alarm clock’
Action to the Action Box, this time for ‘Alarm 2’ to go off after ‘2000’ steps. This means
that the first Type III enemy aircraft will arrive about 35 seconds into the game and
another will be added about every 17 seconds after that. Your display should look like
this:
This finishes the third type of enemy aircraft. Go ahead and run this build of the game to
try it out! If it doesn’t work properly, check it against the components in the file
Save as Lastname_1945_4
Copyright © 2005 by Alan Emrich. All rights reserved.