Gamecreators Beginners Guide To Game Maker Programming pdf

Beginners Guide to Game Maker Programming

Be g in n e r ’s Gu id e t o Ga m e
M a k e r 4 .0 Pr o g r a m m in g

This is a t ut orial in how t o st art pr ogram m ing using Gam e Maker 4.0. I t is
m eant for beginners wit h lit t le or no knowledge about com put er program m ing
languages.
Docum ent w rit t en by Carl Gust afsson ( carl.gust afsson@hom e.se)
Gam e Mak er 4.0 by Mark Overm ars
This docum ent is also available in online HTML for m at at :
ht t p: / / www.gam ecr eat ors.nl, t hanks t o Mart ij n.
Docum ent dat e: 2002- 03- 05

1

Beginners Guide to Game Maker Programming

1 Contents
1


CONTENTS .................................................. 2

2

INTRODUCTION ........................................ 3
2.1
2.2
2.3
2.4
2.5

3

SOME SPRITES.......................................... 4
CREATE OBJECTS .................................... 5
ROOM FOR IMPROVEMENT ....................... 5
SAVE, SAVE, SAVE! ................................. 6
ACTION ................................................... 6
REFINING THE ACTIONS ........................... 7


FIRST VARIABLE ...................................... 8
FIRST FUNCTION ...................................... 9
MORE VARIABLES ................................. 10
FIRST SCRIPT ......................................... 11
GETTING RID OF THE EVENT 12
BULLET LOADING TIME ......................... 13

ENEMY AIRCRAFT.................................. 17
RANDOMIZING....................................... 19
OUCH! THAT HURT!............................... 20
SHOOTOUT ............................................ 22
PYROTECHNICS ..................................... 23

ENHANCING THE GAME ...................... 26
6.1
6.2
6.3
6.4

CENTERED SPRITES ................................ 26

SMOOTHER ENEMY APPEARANCE .......... 27
GOING GLOBAL ..................................... 28
WHERE’S MY ENERGY? ......................... 30

7
LIFE, THE UNIVERSE AND
EVERYTHING................................................... 33
7.1
7.2
7.3
7.4
7.5

THE CODING BEGINS .............................. 8
4.1
4.2
4.3
4.4
4.5
4.6


5

6

CREATING A GAME ................................. 4
3.1
3.2
3.3
3.4
3.5
3.6

4

ACKNOWLEDGEMENTS ............................ 3
GAME MAKER INFORMATION .................. 3
PURPOSE OF THIS DOCUMENT .................. 3
OTHER REFERENCES ................................ 3
WELL?..................................................... 3


5.1
5.2
5.3
5.4
5.5

8

A GALAXY FAR, FAR AWAY ................... 33
COOL WORD: PARALLAX ....................... 34
ENEMY FIRE .......................................... 35
MEANING OF LIFE .................................. 37
SCORING................................................ 39

FINAL WORDS ......................................... 41
8.1
8.2
8.3
8.4


END OF THIS GUIDE................................ 41
COMMUNICATION .................................. 41
USELESS STATISTICS ............................. 41
BYE ....................................................... 41

ENEMIES SIGHTED! ............................... 17

2

Beginners Guide to Game Maker Programming

2 Introduction
2.1

Acknowledgements
I w ould like t o use t his space t o say a heart ily Thank You t o Mark Overm ars
for creat ing such a w onderful gam e building t ool. Thank You Mar k!
Thank you Mart ij n for convert ing t his guide t o HTML form at and publishing it
at ht t p: / / www.gam ecreat ors.nl .

Thank You also t o all m em bers of t he Gam e Maker com m unit y who ar e great
at helping m e and each ot her wit h gam e building advice, help and hint s.
Finally, I am grat eful for all com m ent s and suggest ions t hat I hav e received
regarding t his docum ent fr om a num ber of Gam e Maker user s.

2.2

Game Maker information
Gam e Mak er is writ t en by Mark Ov erm ars. I t is a com plet e gam e building t ool
t hat can be used t o creat e 2- dim ensional com put er gam es t hat can run on
Microsoft Windows sy st em s. The pr ogram can be downloaded fr om t he Gam e
Maker w eb sit e, ht t p: / / www.cs.uu.nl/ people/ m ark ov/ gm ak er/ index.ht m l.
The applicat ion includes an int egrat ed graphics cr eat ion t ool t hat can be used
t o creat e sprit es, a drag- and- drop int erface, m aking it a built - in
program m ing language t hat is sim ilar t o well- known program m ing languages
like C/ C+ + , Pascal and BASI C.
Wit h Gam e Mak er it is possible t o creat e com put er gam es wit hout using a
single line of code, t hanks t o it s int uit ive drag- and- drop icons represent ing
gam e ev ent s and act ions. How ev er, t o be able t o cr eat e m ore advanced
gam es and really release t he full pot ent ial of Gam e Maker t he use of Gam e

Maker Language ( hereaft er referred t o as GML) is an absolut e requirem ent .

2.3

Purpose of this document
This guide was w rit t en as an at t em pt t o int roduce user s t hat ar e used t o
cr eat e gam es using t he drag- and- drop m et hod, t o t he concept of GML.

2.4

Other references
I am going t o m ak e a lot of r efer ences t o t he Gam e Maker PDF m anual
( Gam e_Maker.pdf) , so I suggest you have it ready. I t can be downloaded
from t he Gam e Maker w eb sit e ( see 2.2 Gam e Maker inform at ion)

2.5

Well?
Well? What are you wait ing for? Read on! ; )


3

Beginners Guide to Game Maker Programming

3 Creating a game
We need som et hing t o wor k wit h in order t o be able t o under st and t he
concept s of GML. This m eans t hat I , in t he beginning, am going t o refer t o t he
graphical drag- and- drop icons and com par e t hem t o t he GML code.
So, st art up Gam e Mak er and cr eat e a blank gam e ( File - > New, but I am
sure y ou know t hat ) .

3.1

Some sprites
I n order t o see anyt hing in our gam e w e are going t o need som e sprit es. I f
you do not know w hat a sprit e is, I suggest you scan t hrough Chapt er 3 of t he
Gam e Mak er Manual. Ther e are som e sprit es included wit h t he Gam e Maker
inst allat ion, and t o m ak e t hings easier ( t his is not an im age creat ion guide) I
am going t o use t hem in t he gam e.
For t he player sprit e, w e are going t o use t he im age called “ SR71.bm p” . I t can

be found in t he “ Sprit es \ Transport ” folder of t he Gam e Mak er inst allat ion
direct or y. The im age looks like t his:

Ah, yes! The SR- 71 Blackbird is m y absolut e fav orit e plane! Add a new sprit e.
I n t he nam e box , w rit e “ sprPlayer” . I always use t he pr efix “ spr” in t he nam es
of m y sprit es, since if a sprit e has t he sam e nam e as an obj ect , err ors m ay
occur. So, I consider it a good habit t o have a nam ing convent ion for sprit es
and such. Then, when t he obj ect is creat ed, you do not hav e t o w or ry about
t he nam e coinciding wit h a sprit e nam e. Anot her good t hing about t his is t hat
lat er, when you look at your code, for exam ple when debugging, you
im m ediat ely know if you are r efer ring t o a sprit e or not wit h a variable nam e.
For obj ect s, I suggest t he use of “ obj ” as prefix.
OK, so you hav e nam ed t he sprit e? Good. Now , click t he “ Load Sprit e” but t on.
I n t he file select ion dialog t hat appears, browse t hrough t he Gam e Mak er
inst all direct or y unt il you find t he “ SR71.bm p” im age file. Select it .
Make sure t hat t he checkbox m ark ed “ Transparent ” is check ed ( t hat is, t her e
should be a t ick m ar k in it ) . Ot herwise, check it . This will m ake part s of t he
sprit e t ransparent . Which part s? All pixels t hat have t he sam e color as t he
pixel in t he lower left corner of t he sprit e will be t ranspar ent when t he sprit e
is lat er drawn on t he scr een.

Most gam es I know involve som e kind of shoot ing. For shoot ing we need
bullet s. Creat e a new sprit e and call it “ sprBullet ” . For a bullet im age, let us
use a red ball. Red balls are com m on in gam es. Load t he im age “ ball2.gif”
int o t he “ sprBullet ” sprit e ( ) . The im age file is locat ed in “ Sprit es \
Breakout ” in t he Gam e Maker inst allat ion direct ory . Make sur e t he sprit e is
t ranspar ent ( see abov e) .
That is all sprit es we will need for now.

4

Beginners Guide to Game Maker Programming

3.2

Create Objects
The sprit es w e hav e cr eat ed ar e j ust dum b im ages. OK I agr ee, t hey have
som e int elligence, like t ransparency and bounding box inform at ion, but t hey
really do not do anyt hing. The t hings in a Gam e Mak er gam e t hat act ually
perform s som e act ions are t he Obj ect s. Did you read Chapt er 3 of t he Gam e
Maker Manual? I f not , please read it now, since it explains t he m eaning of
obj ect s.
Creat e a new obj ect and call it “ obj Player” . Now you see it was a good idea t o
call t he Player sprit e “ sprPlayer ” , and not j ust “ Player” . I n t he “ obj Player ”
obj ect ’s sprit e select ion box, select “ sprPlayer ” . Now our “ obj Player” obj ect
will look like t he “ spr Player” sprit e. Gr eat !
Tim e t o cr eat e t he bullet obj ect . Creat e a new obj ect . Nam e it “ obj Bullet ” and
select t he sprit e “ spr Bullet ” as t he sprit e for t he “ obj Bullet ” obj ect .

3.3

Room for improvement
Now we need a place for t he obj ect s t o act . This is done in a r oom .
Creat e a new r oom and call it “ Room 1” ( OBS! no space charact ers) . You m ay
be t em pt ed t o call it “ Room 1” ( wit h a space before t he “ 1” ) , but t hen y ou
would have a hard t im e referencing it from t he GML, so nev er use spaces in
t he nam e for y our obj ect s, sprit es, room s, et c. I f y ou need t o separat e t wo
words in t he nam e, use t he under scor e charact er inst ead “ _” .
Click on t he “ Background” but t on in “ Room 1” t o view t he background set t ings.
Make sure “ Draw background color” is enabled and click on “ background
color” ( upper right corner of background set t ings) . Select a nice gr een ( like
grass) color. Click “ OK” t o close t he background set t ings window. Now y our
room should be all green. The room size should be widt h: 640, height : 480. I f
not so, change it t o t hese values. The default Speed set t ing is 30, which is
pret t y norm al for gam es. This m eans t hat a new gam e fram e will be cr eat ed
30 t im es each second. Hence t he expr ession “ FPS” , Fram es Per Second. Not
t o be confused wit h “ FPS” , First Person Shoot er … Sorr y, j ust being st upid.
We ar e now going t o place an inst ance of our “ obj Player” obj ect in t he room .
I n t he Obj ect select ion box of t he r oom , select “ obj Player” . Now click ONCE in
m iddle of t he room . This should place an “ obj Player” inst ance wher e you
clicked. I f y ou happened t o click m ore t han once, or m ove t he m ouse t oo
m uch when clicking t here m ight have been cr eat ed m ore t han one “ obj Player”
inst ance in t he room . To rem ov e t hem , right - click on t hem . Mak e sure t here is
only one “ obj Player” inst ance in t he r oom .
Here w e pause a m om ent t o cont em plat e on t he t erm s obj ect and inst ance.
To explain t his, I am going t o use a m et aphor. Hope it w orks. Your obj ect is
like a cookie- form , y ou know t he ones you use when m aking ginger- br ead
cookies. When placing your obj ect s in your r oom , y ou ar e act ually placing
“ inst ances” of t he obj ect s, which is like st am ping out t he cookies using your
cookie- form . Each inst ance will act j ust as described in t he obj ect , but each
inst ance will have it s own local variables, like x and y posit ion and speed
( m or e on variables lat er ) . Just like each ginger- bread cookie y ou st am p out
using your form is shaped like t he form , but you can give t hem all different
looks w it h som e icing. Hey! I am get t ing hungry! Back t o t he gam e.
Click OK t o close t he room window.

5

Beginners Guide to Game Maker Programming

3.4

Save, save, save!
Now we ar e alm ost ready t o st art t he gam e, j ust t o check t hat t he “ obj Player”
inst ance is displayed properly in t he gam e r oom . But before we run it , SAVE
I T! Rem em ber t o save your gam e oft en and ALWAYS save it before y ou run
it . I t MAY happen, under cert ain circum st ances, t hat t he com put er freezes
com plet ely and all you can do is t o rest art it . NOT FUN if your gam e is not
saved.
Right . Save it wit h an im aginat ive nam e ( I called m ine “ GMLTut orial” ) .
Now it is t im e t o st art it . Hit F5, or click on t he green arrow t o st art t he gam e.
Okaay! Now w e have creat ed t he foundat ion for a Windows gam e. I f you do
not see t he green background wit h an inst ance of “ obj Player ” in t he m iddle,
you hav e m issed som et hing earlier in t he t ut orial, or som et hing else is wrong.
Check back t o see if y ou m issed anyt hing.
Close t he gam e w indow t hrough pressing [ ESC] or clicking on t he w indow’s
“ Close” icon ( t he cross- m ark, y ou know) .

3.5

Action
I n order t o be able t o call our cr eat ion a gam e, we need t o be able t o int eract
wit h it in som e way , and preferably som et hing should be m oving t oo. We will
st art out by m aking it possible t o m ove t he “ obj Player” wit h t he cursor key s of
t he keyboard.
Back in Gam e Mak er, double- click on t he “ obj Player” obj ect t o open it . Now
we are going t o creat e som e act ions. When som et hing happens t o an obj ect ,
it is called an “ ev ent ” . The obj ect ’s r esponse t o t his ev ent is called an
“ act ion” . What w e want is t hat when we press any of t he cursor k eys, t he
“ obj Player” should st art m oving in t hat direct ion.
Ther e is an ev ent but t on in t he “ obj Player” obj ect t hat says “ < No k ey > ” . This
but t on hides a long list of different key ev ent s. Click on t he sm all select ion
icon on t he right hand of t he “ < No k ey> ” but t on. This will display a large list ,
wit h som e under- list s cont aining all keys of t he keyboard. Select t he “ < Left > ”
key in t his list . The but t on should now display “ < Left > ” . Now w e can define
t he act ions t hat should t ake place when t he left cursor k ey is pressed on t he
keyboard. The list of act ions is t o t he right of t he obj ect window. Find an
act ion t hat is called “ St art m oving in a direct ion” . I t is t he t op left act ion,
represent ed by 8 red ar row s point ing away from t he m iddle. Drag t his icon
ont o t he whit e space bet ween t he ev ent list and t he act ion list . We can call
t his t he “ Act ion sequence” .
A window will pop up when you drop t he act ion in t he act ion sequence. I n t his
w indow you can specify t he param et ers t hat are needed t o define t he act ion.
Click on t he left arr ow t o select it in “ Direct ions” , and set t he speed t o “ 5” .
Then click “ OK” .
What we now hav e done is t o define t hat when t he left key is pr essed on t he
keyboard, t he “ obj Player” will st art m oving left .
Now, select t he “ < Right > ” k ey in t he k ey ev ent but t on. Add t he “ St art m oving
in a direct ion” act ion t o t hat event , set t he Right direct ion, and set t he speed
t o 5.
Repeat t his for t he k eys “ < Up> ” and “ < Down> ” , set t ing t heir corr esponding
direct ion in t he “ Direct ions” sect ion of t he “ St art m oving in a direct ion” act ion.
Save t he gam e and st ar t it again.

6

Beginners Guide to Game Maker Programming

You should now be able t o m ove t he plane using t he cursor keys. Not e
howev er, t hat it is not possible t o st op t he plane. Neit her can y ou m ov e
diagonally.

3.6

Refining the actions
We will now refine t he act ions a bit , as well as add a “ Shoot ” act ion.
Open t he obj ect window for “ obj Player” . Select t he “ < No key> ” ev ent . This
ev ent will happen when all keys on t he keyboar d are r eleased. Add a “ St art
m oving in a direct ion” act ion and select t he square in t he m iddle of
“ Direct ions” . Click OK. This should m ake t he “ obj Player” st op when no k ey is
pressed.
To add a shoot ing act ion, we need t o decide w hich key should be used t o fire
t he bullet . I usually choose t he [ SPACE] k ey for t his.
I n t he obj ect window for “ obj Player” , choose t he “ < Space> ” k ey event . I n t he
act ion list , select t he “ Obj ect s” t ab t o display t he act ions t hat hav e som et hing
t o do wit h obj ect s. Select t he “ Cr eat e an inst ance of an obj ect ” ( look s like a
light bulb) act ion. I n t he window t hat pops up, choose t he obj ect “ obj Bullet ”
and t ick t he checkbox m ark ed “ Relat ive” . This m eans t hat an inst ance of t he
“ obj Bullet ” obj ect will be cr eat ed at t he sam e coordinat es as t he inst ance of
t he “ obj Player” obj ect . Click OK.
Now a Bullet will be cr eat ed. But it needs t o be m oving t oo, t o be of any use.
To do t his, open t he obj ect window for t he “ obj Bullet ” . Select t he “ Cr eat e”
ev ent . Add t he act ion: “ Set direct ion and speed of m ot ion” . This act ion can be
found in t he “ Mov e” t ab, and looks like 8 blue arrows. I n t he popup window
for t he act ion, ent er “ 90” as t he direct ion and “ 15” as t he speed. This will
m ake t he bullet st art m oving in t he direct ion “ 90” wit h speed “ 15” . Direct ions
are m easured in degrees and w ork like t his:
90

180

0

270

So, 90 would be st raight up.
One m ore t hing needs t o be done before st art ing t he gam e. What happens t o
t he bullet t hat reaches t he t op of t he scr een? Not hing. I t j ust cont inues on
“ forever” . So, when enough bullet s ar e fired, t he com put er’s m em ory w ill be
filled up wit h dat a about bullet s t hat m oves up, up, up, and w e never see
t hem . The t hing t o do is t o m ak e sure t he bullet is dest roy ed once it reaches
t he t op of t he screen.
Select t he ev ent “ Out side” . This will happen when an inst ance m ov es out side
t he scr een. Add t he act ion “ Dest roy t he inst ance” fr om t he “ obj ect s” t ab of
t he act ions. The default values ar e OK. Now t he bullet will be dest roy ed once
it reaches out side t he screen.
Save and st art t he gam e and t ry m oving, st opping and shoot ing.

7

Beginners Guide to Game Maker Programming

4 The coding begins
Alright . I f you have follow ed t he direct ions above, you should have a sm all
gam e wit h a plane t hat m ov es and shoot s. The reason I creat ed t his is j ust
t hat now t hat you have done som e dragging- and- dropping, w e can relat e t he
code st at em ent s t o t hese act ions t o im prov e t he underst anding. ( At least ,
t hat is a t heory I hav e ☺ ) .
Coding. That m ay sound spooky t o som e people, while ot her s will relat e it t o
very “ cool st uff” . Really, coding is like doing t he t hing we did before, wit h t he
icons and such, but wit h t ext inst ead. Act ually it is possible t o creat e a gam e
builder wher e a gam e cr eat or is able t o do ev er yt hing wit h icons and dr agand- drop t hat is possible wit h coding in Gam e Maker, but t hat w ould m ean
hundreds of different icons, and t he list of icons in an act ion sequence w ould
be longer t han t he screen, and it would be im possible t o gain a good
ov erview. ( Wow, Word com plained about a long sent ence… ☺ )
So, t his is act ually a sit uat ion where a pict ure does NOT say m ore t han a
t housand words.

4.1

First variable
The first t hing we did wit h t he drag- and- drop act ions was m aking t he plane
m ov e when t he cursor k eys are pressed. We are now going t o ex change t he
act ion icons for code.
Open t he “ obj Player” obj ect and select t he “ < Left > ” k ey ev ent . Rem ove t he
“ St art m oving in a direct ion” act ion from t he act ion sequence list by select ing
it and pressing [ DEL] . Now, view t he “ Code” t ab in t he act ions list . The t wo
act ions I use t he m ost her e are “ Ex ecut e a script ” and “ Execut e a piece of
code” . Drag t he act ion “ Ex ecut e a piece of code” t o t he act ion sequence.
What now pops up is som et hing t hat looks like an em pt y window – a CODE
window < Tension- building m usic score here> . This is where we ent er t he
code t hat should be ex ecut ed as a response t o t he “ < Left > ” k ey event .
Ent er t he follow ing code:
direction = 180;
speed = 5;

What t his m eans is t hat t he variable direct ion of t he curr ent inst ance of t he
“ obj Player” obj ect is set t o 180, and t he speed is set t o 5. This is called
variable assignm ent . To r ead m or e about variables and assignm ent s, see
sect ion 23.2 and 23.3 of t he Gam e Mak er Manual.
Essent ially a variable is a place in t he com put er m em ory t hat can hold a
value. There ar e different kinds of values t hat a variable can hold. Gam e
Maker differs bet w een t wo different t ypes of variable inform at ion: num ber s
and st rings. A num ber is j ust a plain num ber, like
1
5
2.21
63.132
24

8

Beginners Guide to Game Maker Programming

St rings are list s of char act ers t hat are enclosed in single- quot es or doublequot es, like:
“ Hello”
“ This is a st ring”
‘This is anot her st ring’
The use of bot h single- quot es and double- quot es for defining a st ring is one of
t he “ nice” aspect s of Gam e Maker. I t m ak es it possible t o easily include a
single- quot e or a double- quot e in a st ring. Think about it . I f you only could
define a st ring w it h double- quot es, how w ould you t ell t he com put er t hat you
want ed a st ring t hat CONTAI NED a double- quot e? This code:
aLittleString = “And she said “gasp” and fainted”;

will be very confusing for t he com put er. I t w ould be t reat ed as TWO st rings,
wit h t he word “ gasp” bet ween t hem . The sam e goes for single- quot es.
I nst ead, t his code could be used ( not e t he difference) :
aLittleString = ‘And she said “gasp” and fainted’;

Back t o t he gam e.
The variables “ direct ion” and “ speed” are built - in variables, and ev ery
inst ance has t hem . When we are w rit ing variables like t his, we are r efer ring t o
t he so- called local variables t hat belong t o an inst ance of an obj ect . This
m eans t hat if we would check t he value of “ direct ion” in, for exam ple an
inst ance of t he “ obj Bullet ” obj ect , w e w ould not see t he value 5, but inst ead
anot her value, t hat is local t o t he “ obj Bullet ” inst ance.
By set t ing t he variable “ direct ion” t o 180, w e t ell Gam e Mak er t hat t he
direct ion in which t his inst ance should m ov e is 180 ( left ) . Set t ing t he “ speed”
variable t o 5 inst ruct s Gam e Maker t o m ov e t he inst ance 5 pixels each fram e,
in t he direct ion of t he “ direct ion” variable. Fair enough?
So, why is t her e a sem icolon ( ; ) at t he end of each st ring? This t ells t he
program int erpret er t hat t his is t he end of t he st at em ent . Each st at em ent
should end wit h a sem icolon. Work s about t he sam e as “ .” ( dot ) for people.
Dot s m ark t he end of a sent ence. A st at em ent in a com put er pr ogram is about
t he sam e as a sent ence t o people. Act ually, t he GML does not need t he
sem icolon. I t underst ands t he code anyway, if each st at em ent is placed on a
separat e line, but it is considered “ good program m ing” t o use sem icolons.
OK, now w e are done wit h t he < Left > ev ent . Click t he gr een check m ar k t o
st or e t he code changes and close t he window. Ot herwise, t his window blocks
all ot her windows. I t m ust be closed when y ou are r eady edit ing.

4.2

First function
Let us go t o t he < Right > ev ent .
Rem ove t he “ St art m oving in a direct ion” act ion fr om t he < Right > key ev ent .
Add a “ Execut e a piece of code” act ion inst ead.
Now, w e could do t his in t he sam e way as t he < Left > event , by set t ing t he
“ direct ion” and “ speed” variables, but j ust t o learn ot her ways t o do t he sam e
t hing, we do som et hing different . We are going t o use a funct ion.
A funct ion is like a collect ion of program st at em ent s t hat are bundled t oget her
and can be ex ecut ed by calling t he funct ion nam e.

9

Beginners Guide to Game Maker Programming

When y our m at h t eacher speak s of funct ions, he m eans som et hing like t his:
y( x) = 4 + 3x
This is a definit ion of a funct ion. The nam e of t he funct ion is “ y” . The “ x ” in
t he parent heses is called an argum ent t o t he funct ion “ y” .
The result of, for exam ple, y( 5) would be 19. ( 4 + 3 * 5) . The “ * ” ( ast erisk)
charact er is com m only used as m ult iplicat ion operat or in com put er languages.
What w e have done here, if it had been a com put er program , w ould be t o call
t he funct ion “ y” wit h t he argum ent “ 5” . A funct ion is called, t akes som e
argum ent s, does som e com put ing, and ret urns a value. Not all funct ions
ret urn values, and not all funct ions t ake any argum ent s at all, but t his is t he
general idea of a funct ion.
Not e: in ot her languages t he concept “ funct ion” could be called ot her t hings,
like procedure, m et hod, subrout ine et c, but pract ically speaking t hey w ork t he
sam e.
So, if y ou look in t he Gam e Maker Manual, on page 76, t her e is a definit ion of
a funct ion called “ m ot ion_set ” . The definit ion looks like t his:
motion_set(dir, speed)

The t ext aft er t he definit ion in t he m anual explains t hat t his funct ion w ill set
t he speed of an obj ect t o t he “ speed” argum ent , and t he direct ion t o t he “ dir”
argum ent . OK, so now , let us use t his funct ion in t he < Right > key ev ent .
Do y ou st ill have t he code window ( em pt y) for t he act ion in t he < Right > key
ev ent ? Good. Ot herwise, double- click it t o open it up again.
I n t he em pt y code window, writ e:
motion_set(0, 5);

Now we have called t he funct ion “ m ot ion_set ” w it h t he argum ent s “ 0” and “ 5”
in t he places wher e “ dir ” and “ speed” should be defined. And v oilà! When t he
< Right > cursor k ey is pressed in t he gam e, y our inst ance should now st art
m oving in t he direct ion 0 ( right ) wit h speed 5.
We hav e now done t he sam e t hing, but in t wo different ways. Which way is
t he best depends on t he sit uat ion. I n t his case, I t hink t he second way , wit h
t he funct ion call is t he best , but suppose t hat y ou j ust w ant ed t o change t he
speed of t he inst ance, and not t he direct ion. I f so, it w ould be easier t o j ust
assign a new speed value t o t he “ speed” variable.
Lat er in t his guide, we will define and use our own script s, w hich w ork s in a
sim ilar m anner as t he built - in funct ions.

4.3

More variables
Ther e is a t hird way t o accom plish what we have done in t he < Right > and
< Left > k ey ev ent s.
Select t he “ < Up> ” key ev ent for t he “ obj Player ” obj ect . Add an “ Execut e a
piece of code” act ion t o t he act ion sequence. I n t he code window, writ e:
hspeed = 0;
vspeed = -5;

Now, what was t hat ? “ hspeed” and “ vspeed” ar e t w o ot her exam ples of
variables t hat are built - in in an obj ect . These variables define t he horizont al
and vert ical speed of t he obj ect . I f you ar e a m at hem at ician, you could say
t hat “ hspeed” and “ v speed” defines t he speed v ect or in rect angular

10

Beginners Guide to Game Maker Programming

coordinat es, while “ direct ion” and “ speed” defines t he speed v ect or in circular
coordinat es.
Anyway, set t ing t he ver t ical speed “ vspeed” t o –5 m eans t hat t he inst ance
should st art m oving upwards. That is because t he y axis on a com put er
scr een is point ing dow nw ards, so t he furt her dow n t he screen you go, t he
higher t he value of t he y coordinat e. So, in order t o m ake an inst ance m ov e
upw ards, w e m ust have a negat ive vert ical speed.
The horizont al speed is set t o 0, m eaning t hat t he inst ance should not m ov e
left or right at all.
The only m ovem ent left is t he < Down> k ey. We are going t o creat e it in a
fourt h way. Close t he code window for t he < Up> key.

4.4

First script
This t im e you ar e going t o learn how a fr eest anding script works. Creat e a
new script ( Menu: Add - > Script ) .
This will bring fort h a code window, m uch like t he ones we have used befor e.
The difference is t hat t his w indow has a nam e box on it s t op. Ent er t he nam e
“ Mov ePlayerDown” t here. The nam e could be anyt hing, but should describe
t he script ’s funct ionalit y in a good way . Rem em ber, do not use spaces in t he
nam e.
Ent er t he following code in t he script :
hspeed = 0;
vspeed = 5;

Then y ou can, if you want , close t he script , but you m ight as well leave it
open.
Go back t o t he “ obj Player” obj ect window. Select t he < Down> key ev ent .
Rem ove t he exist ing act ion from t he act ion sequence. Add an “ Ex ecut e a
script ” act ion.
A new w indow w ill pop up, w her e you can select w hich script should be run,
and ent er som e argum ent s t o it . To t he right of t he “ Script : ” t ext box t her e is
a select ion icon. Click it . A list of script s should appear. Only one script exist s
in t he list so far, t he “ Mov ePlayerDown” script . Select it .
Our script does not use any argum ent s, so leav e t he rest of t he window as it
is and click OK.
Now, when t he player presses t he < Down> key , t he “ obj Player ” inst ance will
call t he “ Mov ePlayerDown” script , which in t urn will st art t he “ obj Player”
m oving downwards.
What is t he point of m aking a freest anding script ? Well, in t his case, t her e is
not m uch of a point , but if t he script would be very long, it is easier t o
m aint ain if it is freest anding. You do not need t o open t he obj ect and search
for t he corr ect event t o find and edit t he script . I t is also possible t o hav e
m any fr eest anding script s open at t he sam e t im e. That is not possible wit h
script s t hat belong t o an obj ect ev ent .
The m ost im port ant reason t o m ak e a fr eest anding script is t hat any obj ect
m ay use it . I f an inst ance of t he obj ect “ obj Bullet ” needed t o m ove
downwards wit h speed 5, it could also call on t his new script , and t he script
would act on t he “ obj Bullet ” inst ance inst ead of on an “ obj Player” inst ance.
I t is also possible t o call a freest anding script fr om anot her script .

11

Beginners Guide to Game Maker Programming

Alm ost forgot about t he < No k ey> ev ent . That is where t he plane is st opped.
Close t he code window, and select t he < No k ey > ev ent . Rem ov e t he “ St art
m oving in a direct ion” act ion and add an “ Ex ecut e a piece of code” act ion
inst ead. Writ e t he following in t he new code window:
speed = 0;

The direct ion does not m at t er when t he speed is 0, right ?
Dat ’s it !
We hav e now ex changed t he graphical drag- and- drop icons for t he code t ext .
I t did not require m uch coding, did it ?
But t here are lot s of t hings t o im prov e wit h coding, so we are going t o
cont inue on t his gam e a bit m ore.
Save your gam e and t ry it . You should not not ice any difference. The code
works in t he sam e w ay as t he icons did.

4.5

Getting rid of the event
Act ually, t he < No k ey> ev ent is no good for st opping a player- cont rolled
inst ance. You m ight have not iced t hat if you ar e shoot ing w hile m oving, and
release t he m ove k ey, but hold t he shoot key, y ou st ill m ove. This is because
since you ar e holding dow n t he shoot key ( SPACE) you are not get t ing any
< No key> ev ent . And y ou can im agine how m any < No k ey> event s we would
receive in a t wo- player gam e. No, not m any . How can we solve t his?
This is t he way I w ould solve it .
Open t he “ obj Player” obj ect and select t he < No key> event . Rem ov e all
act ions fr om t his ev ent ( should j ust be one here, but anyw ays) .
Select t he < Left > k ey event , and double- click on t he “ Execut e a piece of
code” act ion in t he act ion sequence. This should bring up your code window.
I nst ead of set t ing a speed and a direct ion for t he “ obj Player” here, w e could
j ust change t he coordinat es for it ourselves. I n t hat case it would ONLY m ov e
while t he key is pressed. Anot her t hing we will gain wit h t his is t hat we will be
able t o m ov e diagonally as w ell. Great !
So, delet e all code in t he code window, and w rit e t his inst ead:
x = x – 5;

This m eans t hat w e set t he variable x t o it self m inus 5, t hat is, we decrease
it s value by 5.
The x variable of an inst ance cont ains it s x- coor dinat e in t he room . So, by
decreasing t he x variable, t he inst ance will m ove 5 pixels t o t he left . This is
repeat ed for each gam e fram e as long as t he player holds down t he < Left >
key . That is exact ly what we w ant .
We want t he sam e t hing wit h t he ot her direct ion key s, so let us change t he
code for t he ot her k ey s t oo.

12

Beginners Guide to Game Maker Programming

Open up t he event for t he < Right > key in t he “ obj Player” obj ect . Double- click
on t he act ion in t he act ion sequence t o open t he code window. Delet e t he
“ m ot ion set ” funct ion call and writ e t he following inst ead:
x += 5;

This is t he sam e as w rit ing
x = x + 5;

only t his is short er.
Now I t hink you know w hat t o do wit h t he < Up> and < Down> k ey s. That is
right . The code for t he < Up> k ey ev ent should cont ain:
y -= 5;

and t he code for t he < Down> k ey event should be:
y += 5;

For t he < Down> key w e m ade a fr eest anding script . We could as well keep it
and edit t hat inst ead of changing t he act ion int o an “ Execut e a piece of code”
act ion. So, j ust open t he script “ MovePlayerDown” and change t he code as
st at ed abov e.
Now it is t im e t o sav e t he gam e and run it again. There are t w o t hings I want
you t o not ice about t he gam e now.
1. The plane does not cont inue m oving when t he fire key is held down but
t he m ov e keys ar e released. Good.
2. I t is possible t o m ov e t he plane diagonally. That is good t oo.

4.6

Bullet loading time
When shoot ing bullet s from t he plane you m ay have not iced t hat t hey com e in
a nev er- ending, unint errupt ed flow. We m a y w ant t his, but I do not t hink it
looks v ery good. So, let us add som e loading t im e t o t he gun. To do t his, we
will use t he alarm feat ure of t he “ obj Player ” obj ect , and a local variable.
The t heor y behind t he loading t im e goes like t his:
When t he < Space> k ey is pressed, before firing a bullet , a local variable is
checked t o see if t he gun is ready t o fir e. We can call t his variable
“ gunReady” . The variable “ gunReady ” will t ake on one of t w o values. Eit her it
is “ t rue” , t hat m eans t hat t he gun can fire, or it is “ false” , which m eans t hat
t he gun can not fire right now . I f “ gunReady” is t rue w hen < Space> is
pressed, a bullet is fired, and t he gunReady is set t o “ false” . This m eans t hat
when < Space> is pressed again, t he gun will not fire. How ev er, at t he sam e
t im e as we set “ gunReady” t o false, we set an alarm t im er. When t he t im er
runs out , it is going t o set t he variable “ gunReady” back t o “ t rue” and we can
shoot again. This will repeat it self during t he whole gam e.
OK. The first t hing t o do is t o m ak e sure t hat t he variable “ gunReady” has a
value t he first t im e it is checked when pressing t he Fire but t on. This is called
t o init ialize a variable. I f it is not set t o a value befor e it is check ed, t he gam e
will be halt ed wit h an er ror.
The “ CREATE” event of an obj ect is a good place t o init ialize variables. Open
t he obj ect window for t he “ obj Player” obj ect and select t he CREATE ev ent .
Add an “ Ex ecut e a piece of code” act ion. I n t he code window t hat appears,
writ e t his:
gunReady = true;

13

Beginners Guide to Game Maker Programming

That will set t he variable “ gunReady ” t o “ t rue” . The w ord “ t rue” is one of
Gam e Mak er’s built - in const ant s. Pract ically speaking, using t he word “ t rue” is
t he sam e as using t he num ber “ 1” . The word “ false” is anot her of Gam e
Maker’s built - in const ant s. I t represent s t he num ber “ 0” . That m eans t hat we
could as well writ e
gunReady = 1;

and achieve t he sam e result . But using t he words “ t rue” and “ false” kind of
m akes m ore sense.
I will t ake t his t im e t o int roduce a new concept in coding; t he concept of
com m ent s. Com m ent s are very im port ant in code. The com m ent s ar e m eant
for t he hum an reader of t he code, and not for t he com put er , which will sim ply
ignore it . Use com m ent s a lot t o describe what you m ean wit h your code. This
will m ake it a lot easier lat er when y ou want t o change som et hing. Or debug
it . To add a com m ent , writ e t he t w o charact ers “ / / ” before t he com m ent . Like
t his:
// Make the gun ready to fire.
gunReady = true;

The com put er will com plet ely ignore t he com m ent and ex ecut e t he ot her
code. But when som eone sees t his code segm ent , t hey will underst and m ore
about what is happening t han if t hey only saw t he com put er code.
A com m ent can also be added aft er a program st at em ent , like t his:
gunReady = true; // Make the gun ready to fire.

but I prefer t he st yle wit h t he com m ent on it s own row. You do what ev er you
like.
Now, rem em ber how w e shoot t he bullet ? I n t he < Space> k ey event of t he
“ obj Player” w e creat e an inst ance of t he bullet obj ect . Open t he < Space> k ey
ev ent for t he “ obj Player ” . Delet e t he “ Creat e an inst ance of an obj ect ” act ion
from t he act ion sequence. Add an “ Execut e a piece of code” act ion t o t he
ev ent . This will, as usual, open up a code window. The first t hing we will do
her e is t o check if t he gun is ready. That is, w e will check if t he variable
“ gunReady” is “ t rue” . How do w e “ check” t he v alue of a v ariable? That is what
t he “ if” st at em ent is for. The definit ion of t he “ if” st at em ent is like t his
( excerpt fr om t he m anual) :
An if st at em ent has t he form
if ()
or
if () else
The st at em ent can also be a block. The expr ession will be evaluat ed. I f t he (r ounded) value is
< = 0 ( fa lse ) t he st at em ent after else is execut ed, ot herwise ( t r u e ) t he ot her st at em ent is
execut ed. I t is a good habit t o always put cur ly br acket s ar ound t he st at em ent s in t he if
st at em ent . So best use
if ()
{

}
else
{
< st at em ent >
}

14

Beginners Guide to Game Maker Programming

Hm m m . That m ight be a bit hard t o under st and if you are not a program m er.
To clear t hings out , I will sim ply writ e out t he “ if” st at em ent as it should look
in our code window. Writ e down t his:
if (gunReady = true) then
{
}

That was not so hard, w as it ? This m eans t hat if t he variable “ gunReady ” has
t he value “ t rue” , t he code t hat we ( lat er) put inside t he “ curly braces” will be
ex ecut ed. Ot herwise, it will j ust be ignored. I have added t he word “ t hen”
aft er t he parent hesis, but t hat is not required. I t j ust adds t o t he readabilit y
of t he code. You m ay use t he “ t hen” w ord or not , as you like. The so- called
“ curly braces” will be used a lot in t he code. They com e fr om t he C/ C+ +
language. The t hing is t hat if we j ust want t o per form one single code
st at em ent inside t he “ if” st at em ent , t he braces are not needed, but it is
consider ed good program m ing st yle t o always include t hem . The braces kind
of cr eat e a “ block ” of code t hat , t o t he language int erpret er, look s like a single
st at em ent . To underst and why t his is useful, consider t he following exam ple.
I f t he variable “ speed” is higher t han 5, w e want t o m ove t he obj ect t o t he
posit ion where x = 40 and y = 80. Sim ple enough. So, we writ e:
if (speed > 5) then
x = 40;
y = 80;

But THAT will NOT work as w e w ould expect it t o do. The “ if” st at em ent only
affect s t he FI RST st at em ent , j ust below it . That m eans t hat if “ speed” is NOT
higher t han 5, t he “ x = 40” will be skipped, but t he “ y = 80” will be ex ecut ed
anyway. To solve t his, we need eit her anot her “ if” st at em ent for t he “ y = 80”
st at em ent , t hat is t he sam e as t he first “ if” st at em ent , or, m uch easier, we
could use “ curly braces” . Like t his:
if (speed > 5) then
{
x = 40;
y = 80;
}

This m eans t hat bot h t he “ x” t hing and t he “ y” t hing are included in t he “ if”
st at em ent . Now, if “ speed” is NOT higher t han 5, neit her “ x = 40” nor “ y =
80” will be ex ecut ed. I f “ speed” how ever I S higher t han 5, bot h t he following
st at em ent s will be execut ed. Now, do you underst and t he point in using “ curly
braces” ? They ar e used for m or e st at em ent s, but we will t ake t hem as we go.
Now, back t o t he gam e code. I n your code window y ou should now hav e a
com plet e “ if” st at em ent wit h “ curly braces” . But t her e is st ill not hing inside
t he braces. Here w e will cr eat e an inst ance of t he “ obj Bullet ” obj ect . Earlier
t his was done using a drag- and- drop act ion. Now we will do it in code. I t is
quit e sim ple. We w ill use t he funct ion “ inst ance_cr eat e” . I t can be found in
t he Gam e Maker m anual, in sect ion 25.2, page 79.
I nside t he curly braces, ent er t his:
instance_create(x, y, objBullet);

Ther e! Now you have cr eat ed a bullet . But what about t he “ x” and t he “ y” ?
Should we not ent er som e num erical value t here? Act ually t hat is what we
have done. The variables “ x ” and “ y ” holds t he posit ion of t he curr ent
“ obj Player” inst ance ( r em em ber, we ar e in t he “ obj Player” obj ect while
coding) , and t hese coordinat es will do as t he st art ing coordinat es of t he
“ obj Bullet ” inst ance. This is t he sam e as cr eat ing an inst ance wit h t he dragand- drop icon and checking t he “ Relat ive” checkbox.

15

Beginners Guide to Game Maker Programming

Now, t hat was not all we should do, was it ? No. We also should set t he
“ gunReady” variable t o “ false” t o m ake sure t hat t he gun can not be fired
im m ediat ely again. Ent er t his, j ust aft er t he “ inst ance_creat e” line:
gunReady = false;

That was easy!
Now we will have t o set an alarm t o m ak e sure t hat t he “ gunReady” variable
becom es “ t rue” again, aft er som e t im e. Set t ing an alarm is done like t his:
alarm[0] = 10;

Ther e ar e 8 alarm clock s ( alarm [ 0] – alarm [ 7] ) . We hav e used alarm [ 0] her e.
These brack et s ( “ [ ] ” ) ar e used t o define an array. An array is like a list of
variables t hat all have t he sam e nam e, but are num bered t o m ake t hem differ
from one anot her . We w ill look closer at ar ray s in anot her t ut orial. Now t he
alarm “ alarm [ 0] ” will t rigger in 10 fram es.
We ar e now done wit h t he code in t he < Space> key event . I f you want , you
could short en t he “ if” st at m ent r ow like t his:
if (gunReady) then

Because t hat is t he sam e as checking if “ gunReady” cont ains ANY posit ive
num ber, which it does if it is “ t rue” .
So, t he code should now look like t his:
if (gunReady) then
{
instance_create(x, y, objBullet);
gunReady = false;
alarm[0] = 10;
}

The t hr ee lines bet w een t he curly braces ar e “ indent ed” , t hat is, you should
add a “ TAB” charact er before t hem . This is j ust t o m ak e t he code easier t o
read. The language int erpret er does not care about “ indent at ions” at all, but it
is good for hum an readabilit y. I t is easier t o see which part of t he code is
collect ed inside a “ curly braces block ” .
Alright !
One t hing rem ains; t he alarm [ 0] event . Close t his code window and open t he
ev ent for alarm [ 0] . Add an “ Execut e a piece of code” act ion and writ e t he
following code in t he window:
gunReady = true;

That will m ake sur e t hat when t he alarm “ goes off” , 10 fram es aft er t he bullet
has been fired, t he variable “ gunReady” is set t o “ t rue” again.
Now, save and t est your gam e!
I f you have followed t he direct ions correct ly, t he “ obj Player ” should now fire
wit h longer int ervals ( about 3 bullet s per second) . Much bet t er .

16

Beginners Guide to Game Maker Programming

5 Enemies sighted!
Ther e ar e v er y few gam es t hat do not include com put er- cont r olled enem ies t o
t he player. Aft er all, we m ust have som eone t o fight . I n t his chapt er w e will
add som e enem y craft and learn t o use t he “ random ” funct ion and check
collisions.

5.1

Enemy aircraft
I hav e decided t hat t he enem y should consist of enem y aircr aft t hat appear at
t he t op of t he screen and drive t o t he bot t om of t he screen.
We need t o do t he following:
-

Hav e t he cr aft appear at random t im e int ervals.

-

The craft should appear at random places along t he t op scr een.

-

When t he craft disappear at t he bot t om of t he scr een, t hey should be
dest roy ed.

-

Lat er w e will add a collision ev ent bet w een t he enem y craft and t he
“ obj Player” .

First , we m ust creat e an enem y craft sprit e and an enem y craft obj ect .
I hav e decided t o use t he im age “ Mig41.gif” from t he folder “ Sprit es \
Transport ” in t he Gam e Maker direct ory. I t look s like t his:

Creat e a new sprit e, call it “ sprEnem y1” and load t his im age int o it . Did you
forget how? Check chapt er 3.1 Som e sprit es. The craft is how ever facing
upw ards, and w e need our enem y craft t o face dow nw ards, unless of course
we want it t o at t ack in rev erse, but I do not t hink t hat would look good.
I n order t o rot at e t he cr aft , open t he sprit e and click t he “ Edit sprit e” but t on.
Click on “ im age 0” t o select it , and choose “ Transform - > Rot at e 180” from
t he m enu. That should r ot at e t he craft t o face downwards. Click OK t o close
t he sprit e edit window. Click OK again t o close t he sprit e window.
Now we need t o m ak e an enem y obj ect . Cr eat e a new obj ect and call it
“ obj Enem y1” . Choose “ sprEnem y1” as t he sprit e for t he obj ect .
We want t he enem ies t o com e down at cert ain int ervals and fly across t he
scr een. I n order t o cont rol t he creat ion of t he enem ies, w e need a cont rol
obj ect . We cou ld use t he “ obj Player” as t his obj ect , but I prefer not t o. I
w ould rat her hav e a dedicat ed enem y cont rol obj ect .
Tim e t o explain t he t erm “ cont rol obj ect ” . The concept of a cont rol obj ect is t o
have an inst ance of an obj ect t hat is always pr esent in t he gam e. I t should
not be able t o shoot it down or ot herwise dest r oy it . This cont rol obj ect is
used t o creat e inst ances of ot her obj ect s, such as swarm s of enem ies t hat
com e flying down. I nst ances of cont rol obj ect s are also good at keeping som e
dat a in t heir local variables t hat can not be k ept in, for exam ple t he inst ance
of t he player obj ect , since it is dest r oy ed at t im es ( w e will com e t o t hat ) . A
cont r ol obj ect is usually invisible, so t hat t he user does not know it is t her e. I t
does it s dirt y j ob “ under cov er” .

17

Beginners Guide to Game Maker Programming

So, t he inst ance of t his cont r ol obj ect will not be visible when playing t he
gam e, but w e need it t o be repr esent ed by som e sprit e anyway , in order t o
place it and see it in t he room . So, w e do like t his.
Creat e a new sprit e, called “ spr Enem y Cont r oller” and load int o it t he im age
“ t rigger.gif” fr om t he “ Sprit es \ Maze” folder in t he Gam e Mak er direct ory.
That looks good for a cont roller, doesn’t it ☺ ? Creat e a new obj ect , call it
“ obj Enem yCont roller” and assign t he sprit e “ sprEnem yCont roller” t o it . Mak e
sure t he checkbox “ Visible” in t he obj ect window of “ obj Enem yCont roller” is
u n ch e ck e d. That will m ake t he cont r oller obj ect invisible t o t he player , but
we will be able t o see it when designing t he gam e.
Open Room 1. Add an inst ance of t he “ obj Enem yCont r oller” obj ect som ewhere
in t he r oom . I t does not m at t er where y ou place it . Just som ewher e you can
see it . Also, t he player inst ance ( t he “ obj Player” ) should be placed at t he
bot t om of t he room in order t o give t he player t im e t o react before t he
enem ies com e. I f you placed t he “ obj Player” som ewher e in t he m iddle of t he
room , like I did, delet e it by right - clicking on it , select t he “ obj Player” inst ance
from t he “ Obj ect ” select ion box and place a new inst ance of t he “ obj Player”
obj ect in t he m iddle, near t he bot t om of t he scr een. Ther e!
Now we should cr eat e t he script for init ializing a new enem y. Cr eat e a new
script and call it “ Enem y 1I nit ” . Fo