Be g in n e r ’s Gu id e t o Ga m e
Be g in n e r ’s Gu id e t o Ga m e
Be g in n e r ’s Gu id e t o Ga m e
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
M a k e r 4 .0 Pr o g r a m m in g
M a k e r 4 .0 Pr o g r a m m in g
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 w it h lit t le or no know ledge about com put er pr ogram 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 Mar k Overm ar s
This docum ent is also available in online HTML for m at at :
ht t p: / / w w w .gam ecr eat ors.nl, t hanks t o Mart ij n.
1 Contents
1 CONTENTS ... 2
2 INTRODUCTION ... 3
2.1 ACKNOWLEDGEMENTS... 3
2.2 GAME MAKER INFORMATION... 3
2.3 PURPOSE OF THIS DOCUMENT... 3
2.4 OTHER REFERENCES... 3
2.5 WELL?... 3
3 CREATING A GAME ... 4
3.1 SOME SPRITES... 4
3.2 CREATE OBJECTS... 5
3.3 ROOM FOR IMPROVEMENT... 5
3.4 SAVE, SAVE, SAVE! ... 6
3.5 ACTION... 6
3.6 REFINING THE ACTIONS... 7
4 THE CODING BEGINS ... 8
4.1 FIRST VARIABLE... 8
4.2 FIRST FUNCTION... 9
4.3 MORE VARIABLES... 10
4.4 FIRST SCRIPT... 11
4.5 GETTING RID OF THE <NO KEY> EVENT 12 4.6 BULLET LOADING TIME... 13
5 ENEMIES SIGHTED! ... 17
5.1 ENEMY AIRCRAFT... 17
5.2 RANDOMIZING... 19
5.3 OUCH!THAT HURT!... 20
5.4 SHOOTOUT... 22
5.5 PYROTECHNICS... 23
6 ENHANCING THE GAME ... 26
6.1 CENTERED SPRITES... 26
6.2 SMOOTHER ENEMY APPEARANCE... 27
6.3 GOING GLOBAL... 28
6.4 WHERE’S MY ENERGY? ... 30
7 LIFE, THE UNIVERSE AND EVERYTHING... 33
7.1 A GALAXY FAR, FAR AWAY... 33
7.2 COOL WORD: PARALLAX... 34
7.3 ENEMY FIRE... 35
7.4 MEANING OF LIFE... 37
7.5 SCORING... 39
8 FINAL WORDS ... 41
8.1 END OF THIS GUIDE... 41
8.2 COMMUNICATION... 41
8.3 USELESS STATISTICS... 41
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 Over m 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: / / w w w .gam ecreat ors.nl .
Thank You also t o all m em bers of t he Gam e Maker com m unit y w ho ar e great at helping m e and each ot her w it 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 w rit t en by Mar k Ov erm ars. I t is a com plet e gam e building t ool t hat can be used t o cr eat 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: / / w w w .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 w ell- know n pr ogram 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 w it 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 r eally release t he full pot ent ial of Gam e Mak er t he use of Gam e Mak er Language ( hereaft er r efer r ed t o as GML) is an absolut e r equirem ent .
2.3 Purpose of this document
This guide w as 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?
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 ar e 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 hr ough 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 alw ays 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, w hen 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, w hen you look at your code, for exam ple w hen debugging, you
im m ediat ely know if you are r efer ring t o a sprit e or not w it h a variable nam e. For obj ect s, I suggest t he use of “ obj ” as pr efix.
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, br ow se 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 sur e t hat t he checkbox m ark ed “ Tr anspar ent ” is check ed ( t hat is, t her e should be a t ick m ar k in it ) . Ot herw ise, check it . This w ill 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 draw n 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 r ed ball. Red balls ar e 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) .
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 ranspar ency 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 w as 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 w ill 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” ( w it h a space befor e 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 w o w ords 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 backgr ound set t ings. Make sur e “ Draw background color” is enabled and click on “ backgr ound 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 r oom size should be w idt h: 640, height : 480. I f not so, change it t o t hese values. The default Speed set t ing is 30, w hich is pret t y norm al for gam es. This m eans t hat a new gam e fram e w ill 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 w it h “ FPS” , First Per son 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 w her e you clicked. I f y ou happened t o click m or e t han once, or m ove t he m ouse t oo m uch w hen clicking t here m ight have been cr eat ed m or e t han one “ obj Player” inst ance in t he room . To r em 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 or k s. Your obj ect is like a cookie- form , y ou know t he ones you use w hen 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, w hich 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.
3.4 Save, save, save!
Now w e 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 w e run it , SAV E
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 w it 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 ar r ow t o st art t he gam e.
Okaay! Now w e hav e cr eat ed t he foundat ion for a Window s gam e. I f y ou do not see t he gr een background w it 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 w rong. Check back t o see if y ou m issed anyt hing.
Close t he gam e w indow t hrough pr essing [ 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 w it h it in som e w ay , and preferably som et hing should be m oving t oo. We w ill st art out by m aking it possible t o m ove t he “ obj Player” w it h t he cur sor 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 w e 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 w ill 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 w hen 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 w indow . 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 r ed ar row s point ing aw ay from t he m iddle. Drag t his icon ont o t he w hit e space bet w een t he ev ent list and t he act ion list . We can call t his t he “ Act ion sequence” .
A w indow w ill pop up w hen 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 ar e 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 “ < Dow n> ” , 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.
You should now be able t o m ove t he plane using t he cursor keys. Not e how ev 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 w ill now refine t he act ions a bit , as w ell as add a “ Shoot ” act ion.
Open t he obj ect w indow for “ obj Player” . Select t he “ < No key> ” ev ent . This ev ent w ill happen w hen 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 w hen no k ey is pressed.
To add a shoot ing act ion, w e 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 w indow 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 w indow 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 w ill 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 w indow 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 arr ow s. I n t he popup w indow for t he act ion, ent er “ 90” as t he direct ion and “ 15” as t he speed. This w ill m ake t he bullet st art m oving in t he direct ion “ 90” w it h speed “ 15” . Direct ions are m easur ed in degr ees and w ork like t his:
So, 90 w ould be st raight up.
One m or e t hing needs t o be done befor e 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, w hen enough bullet s ar e fired, t he com put er’s m em ory w ill be filled up w it h dat a about bullet s t hat m oves up, up, up, and w e nev er see t hem . The t hing t o do is t o m ak e sur e t he bullet is dest r oy ed once it reaches t he t op of t he scr een.
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 r y m oving, st opping and shoot ing.
0 90
180
4 The coding begins
Alright . I f y ou hav e follow ed t he direct ions abov e, you should have a sm all gam e w it 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 hav e done som e dragging- and- dropping, w e can r elat 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 w e did befor e, w it h t he icons and such, but w it h t ext inst ead. Act ually it is possible t o creat e a gam e builder w her e a gam e cr eat or is able t o do ev er yt hing w it h icons and dr ag-and- drop t hat is possible w it 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 scr een, and it w ould 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 w here a pict ure does NOT say m ore t han a t housand words.
4.1 First
variable
The first t hing w e did w it h t he drag- and- drop act ions w as m aking t he plane m ov e w hen t he cursor k ey s ar e pressed. We ar e 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 w o 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 w indow – a CODE w indow < Tension- building m usic score here> . This is w here w e 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 w o 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
St rings are list s of char act ers t hat are enclosed in single- quot es or double-quot 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 y ou 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 w ant ed a st ring t hat CONTAI NED a double- quot e? This code:
aLittleString = “And she said “gasp” and fainted”;
w ill be v ery confusing for t he com put er. I t w ould be t r eat ed as TWO st r ings, w it h t he w ord “ gasp” bet w een t hem . The sam e goes for single- quot es. I nst ead, t his code could be used ( not e t he differ ence) :
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 w e are w rit ing variables like t his, w e 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 w e w ould 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 w hich 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, w hy is t her e a sem icolon ( ; ) at t he end of each st ring? This t ells t he program int erpr et 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 w it 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 w indow . Ot her w ise, t his w indow 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 w ay as t he < Left > event , by set t ing t he “ direct ion” and “ speed” variables, but j ust t o learn ot her w ays t o do t he sam e t hing, w e do som et hing different . We ar e going t o use a funct ion.
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 par ent heses is called an argum ent t o t he funct ion “ y” .
The result of, for exam ple, y( 5) w ould 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 her e, if it had been a com put er program , w ould be t o call t he funct ion “ y” w it 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 procedur e, m et hod, subr out ine et c, but pr act 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 herw ise, double- click it t o open it up again.
I n t he em pt y code w indow , w rit e:
motion_set(0, 5);
Now w e 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 w her 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 w o different w ays. Which w ay is t he best depends on t he sit uat ion. I n t his case, I t hink t he second w ay , w it 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, w e w ill define and use our ow n 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 w ay t o accom plish w hat w e 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 w indow , w rit e:
hspeed = 0; vspeed = -5;
coordinat es, w hile “ 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 scr een 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 hav e a negat ive v ert 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 < Dow n> 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 w or ks. 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 ePlayerDow n” t her e. The nam e could be anyt hing, but should describe t he script ’s funct ionalit y in a good w ay . Rem em ber, do not use spaces in t he nam e.
Ent er t he follow ing code in t he script :
hspeed = 0; vspeed = 5;
Then y ou can, if you w ant , close t he script , but you m ight as w ell leave it open.
Go back t o t he “ obj Player” obj ect w indow . Select t he < Dow n> 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 r est of t he w indow as it is and click OK.
Now , w hen t he player presses t he < Dow n> key , t he “ obj Player ” inst ance w ill call t he “ Mov ePlayerDow n” script , w hich in t urn w ill st art t he “ obj Player” m oving dow nw ards.
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 w ould be ver y 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 sear ch 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 w ould act on t he “ obj Bullet ” inst ance inst ead of on an “ obj Player” inst ance.
Alm ost forgot about t he < No k ey> ev ent . That is w her e t he plane is st opped.
Close t he code w indow , 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 follow ing in t he new code w indow :
speed = 0;
The direct ion does not m at t er w hen 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 r equire m uch coding, did it ?
But t here ar e lot s of t hings t o im prov e w it h coding, so w e ar e going t o cont inue on t his gam e a bit m ore.
Save your gam e and t r y it . You should not not ice any differ ence. The code w or ks in t he sam e w ay as t he icons did.
4.5 Getting rid of the <No key> event
Act ually, t he < No k ey> ev ent is no good for st opping a player- cont r olled 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 k ey ( SPACE) y ou are not get t ing any < No key> ev ent . And y ou can im agine how m any < No k ey> event s w e w ould receive in a t w o- player gam e. No, not m any . How can w e solve t his?
This is t he w ay 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 her e, but any w ays) .
Select t he < Left > k ey ev ent , and double- click on t he “ Ex ecut 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 w hile t he key is pressed. Anot her t hing w e w ill gain wit h t his is t hat w e w ill be able t o m ov e diagonally as w ell. Great !
So, delet e all code in t he code w indow , 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, w e 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 w ill 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 .
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 w indow . Delet e t he “ m ot ion set ” funct ion call and w rit 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 w it h t he < Up> and < Dow n> 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 < Dow n> 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 “ MovePlayerDow n” 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 ar e t w o t hings I w ant you t o not ice about t he gam e now .
1. The plane does not cont inue m oving w hen t he fire key is held dow n 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 er rupt 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, w e w ill 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, befor e 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 ” w ill 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” , w hich m eans t hat t he gun can not fir e 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 w hen < Space> is pr essed again, t he gun w ill 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 w e can shoot again. This will repeat it self during t he w hole 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 w hen 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 w ill be halt ed w it 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 w indow 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 w indow t hat appears, w rit e t his:
That w ill 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 w ord “ 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 w ell w rit e
gunReady = 1;
and achieve t he sam e r esult . But using t he w or ds “ t rue” and “ false” kind of m akes m or e sense.
I w ill 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 r eader of t he code, and not for t he com put er , w hich w ill sim ply ignore it . Use com m ent s a lot t o describe w hat you m ean w it h your code. This w ill m ake it a lot easier lat er w hen y ou w ant t o change som et hing. Or debug it . To add a com m ent , w rit e t he t w o charact er s “ / / ” before t he com m ent . Like t his:
// Make the gun ready to fire. gunReady = true;
The com put er w ill com plet ely ignore t he com m ent and ex ecut e t he ot her code. But w hen som eone sees t his code segm ent , t hey w ill underst and m ore about w hat 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 pr ogram st at em ent , like t his:
gunReady = true; // Make the gun ready to fire.
but I prefer t he st y le w it h t he com m ent on it s ow n row . You do w hat ev er you like.
Now , r em 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 w ill check if t he variable “ gunReady” is “ t rue” . How do w e “ check” t he v alue of a v ariable? That is w hat 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 (<expression>) <statement>
or
if (<expression>) <statement> else <statement>
The st at em ent can also be a block. The expr ession w ill be ev aluat ed. I f t he ( r ounded) v alue is < = 0 (f a lse) t he st at em ent after else is ex ecut ed, ot herw ise (t r u e) t he ot her st at em ent is
ex ecut ed. I t is a good habit t o alw ay s 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 (<expression>)
{
<statement>
}
else
{
< st at em ent >
Hm m m . That m ight be a bit hard t o under st and if you ar e not a program m er . consider ed good program m ing st yle t o alw ays 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 w hy t his is useful, consider t he following exam ple.
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 ak e sur e 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 w e w ill 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 w it h t he code in t he < Space> key event . I f you w ant , 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, w hich 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 befor e t hem . This is j ust t o m ak e t he code easier t o read. The language int erpr et er does not car e about “ indent at ions” at all, but it is good for hum an readabilit y. I t is easier t o see w hich 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 w indow and open t he ev ent for alarm [ 0] . Add an “ Execut e a piece of code” act ion and w rit e t he following code in t he w indow :
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!
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, w e m ust have som eone t o fight . I n t his chapt er w e w ill 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 scr een and drive t o t he bot t om of t he scr een.
We need t o do t he follow ing:
- 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 r oy ed.
- Lat er w e w ill add a collision ev ent bet w een t he enem y craft and t he “ obj Player” .
First , w e 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 ev er facing upw ards, and w e need our enem y craft t o face dow nw ards, unless of course w e w ant it t o at t ack in rev er se, but I do not t hink t hat w ould look good.
I n order t o r ot 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 w indow . Click OK again t o close t he sprit e w indow .
Now w e 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 pr efer not t o. I w ould rat her hav e a dedicat ed enem y cont rol obj ect .
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 anyw ay , 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 or y. 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 r oller” t o it . Mak e sure t he checkbox “ Visible” in t he obj ect w indow of “ obj Enem yCont roller” is
u n ch e ck e d. That w ill m ake t he cont r oller obj ect invisible t o t he player , but w e w ill be able t o see it w hen designing t he gam e.
Open Room 1. Add an inst ance of t he “ obj Enem yCont r oller” obj ect som ew here in t he r oom . I t does not m at t er w here y ou place it . Just som ew her 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 r oom in order t o giv e t he play er t im e t o r eact befor e t he enem ies com e. I f you placed t he “ obj Player” som ew her 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 w e 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 ” . For a st art er w e only w ant t he enem y cr aft t o t rav el dow n acr oss t he scr een. So, in t he new script w e w rit e:
vspeed = 10;
That w ill m ake w hat ever obj ect calls t he script m ov e dow nw ard acr oss t he scr een wit h a speed of 10.
We also need t he enem ies t o disappear once t hey m ov e below t he screen. Creat e a new script and call it “ Enem yDisappear” . Ent er t his code:
instance_destroy();
That code is a call t o t he funct ion “ inst ance_dest roy” , w hich dest r oy s t he inst ance t hat calls it . See chapt er 25.2 ( page 79) of t he Gam e Mak er m anual for m or e inform at ion about t his funct ion.
That is all code needed for t he enem y so far . We will m ake it a lit t le m ore advanced lat er on.
Now w e w ill add t w o scr ipt s for t he enem y cont r oller obj ect . Cr eat e t w o new script s and call t hem “ Enem yCont rollerI nit ” and “ Cr eat eEnem y1” . I n t he script “ Enem yCont rollerI nit ” , w rit e t he following:
alarm[0] = 30;
That w ill set t he alarm [ 0] funct ion of t he “ obj Enem yCont roller” t o t rigger aft er 30 fram es. That is about 1 second in “ r eal t im e” provided t hat t he r oom speed is 30 FPS. When t he alarm t rigger s it w ill call t he ot her script , called
“ Cr eat eEnem y1” . I n t hat script , w rit e:
instance_create(50, 0, objEnemy1); alarm[0] = 30;
The first line w ill creat e a new inst ance of t he obj ect “ obj Enem y1” at t he x-coordinat e 50 and t he y - x-coordinat e 0. Check chapt er 25.2 ( page 79) of t he Gam e Mak er m anual for m ore inform at ion about t his funct ion.
The second line set s t he alarm [ 0] once again t o 30 fram es ( 1 second) t o m ake sur e anot her enem y appears aft er 1 second.
w indow t hat pops up, select t he script “ Enem y 1I nit ” . Click OK t o close t he act ion w indow .
Select t he ev ent “ Out side” . Add t he act ion “ Ex ecut e a script ” and select t he script “ Enem yDisappear ” . Click OK t o close t he act ion w indow .
Open t he obj ect w indow for “ obj Enem yCont roller” . Select t he event “ CREATE” . Add t he act ion “ Execut e a script ” and select t he script “ Enem y Cont r oller I nit ” . Close t he act ion w indow . And, finally, t o t he event “ Alarm 0” , add t he act ion “ Ex ecut e a script ” and select t he script “ Creat eEnem y1” .
Save t he gam e and run it .
You should see enem y aircraft appearing and driving across t he screen, disappearing at t he bot t om . Not e t hat t he inst ance of t he obj ect
“ obj Enem yCont roller” is not visible w hen running t he gam e. I f it is, you forgot t o uncheck t he checkbox “ Visible” in t he obj ect w indow of
“ obj Enem yCont roller” .
5.2 Randomizing
The appearance of t he enem ies is quit e boring t hough. We w ould w ant t hem t o appear at random posit ions along t he t op of t he scr een, and at random int ervals and random speeds. That w ould m ake t he gam e a lot m ore int erest ing, don’t you t hink? That is w hat t he “ r andom ” funct ion is for .
Check out t he definit ion for t he funct ion “ random ” in t he Gam e Mak er m anual, chapt er 24.2 ( page 74) . The random funct ion could be used t o m ak e t hings happen random ly or per haps t o sim ulat e a dice. I t r et urns a random v alue t hat is alw ays less t han t he specified value. Like t his:
random(3);
The abov e line w ill ret ur n values fr om 0 t o 2.9999999999999999999. I f I am corr ect ly inform ed, Gam e Maker w or ks w it h 20 decim als.
To get rid of t he decim als, w e could use anot her Gam e Maker funct ion, called “ floor” . This funct ion sim ply t akes aw ay all decim als, so t hat , for exam ple:
floor( 1.23423) = 1
floor( 2.999999999) = 2
I f w e w ant ed t o sim ulat e a 6- sided dice, w e w ould use:
myDice = random(6);
The abov e line w ould give values from 0 t o 5.9999… Then w e use t he floor funct ion:
myDice = floor(myDice);
As t he argum ent t o t he floor( ) funct ion, w e use t he variable “ m yDice” it self. This is perfect ly OK. The result will now be an int eger bet w een 0 and 5. Alm ost t here. We could now add 1 t o t he r esult .
myDice = myDice + 1;
And finally t he result would be values from 1 t o 6, all wit h t he sam e probabilit y.
To sav e som e place in t he coding w indow , w e could do all t here calculat ions in one line:
The abov e line m ight be a bit hard t o r ead, but it car ries out all t he prev ious calculat ions in one st at em ent .
Now we will put t he random funct ion t o w or k in t hree places of our code. Open up t he script “ Cr eat eEnem y1” .
First w e w ant t he enem ies t o appear at random posit ions along t he t op of t he scr een. That m eans w e w ant t o use random v alues ranging from 0 t o 639, w hich is t he w idt h of our gam e scr een ( 640 pixels w ide) . The st at em ent
random(640);
w ill produce values fr om 0 t o 639.999999. Adding t he “ floor ” funct ion,
floor(random(640));
will produce values fr om 0 t o 639 wit h no decim als. That looks good. But what if w e lat er w ant t o change t he screen size? No problem . I n Gam e Maker t here is a variable t hat can be used t o det erm ine t he size of t he scr een. I t is called “ screen_w idt h” . So, inst ead of using “ 640” w e w ill use “ screen_w idt h” . ( see m anual chapt er 27.5, page 94) . So, now w e can change t he first line of t he script t o read:
instance_create(floor(random(screen_width)), 0, objEnemy1);
Be VERY car eful w it h t he par ent heses, since Gam e Maker m ight crash if t hey are not all t here.
I f y ou w ant , y ou can save t he gam e and t est it .
We also want t he enem ies t o appear at differ ent t im e int ervals. Say bet w een 0.3 and 2 seconds. 0.3 seconds m ean 10 fram es, and 2 seconds m ean 60 fram es. So w e w ant a r andom num ber bet w een 10 and 60. Change t he second line in t he script t o:
alarm[0] = floor(random(51)) + 10;
That will result in alarm t im es fr om 10 t o 60 fram es.
Save and run t he gam e again.
Finally, w e w ant t he enem ies t o fly at random speeds. Open t he script “ Enem y1I nit ” and change t he code line t o t his:
vspeed = random(8) + 2;
This w ill give speeds fr om 2 t o 9.999999. We do not use “ floor” her e, since speeds w it h decim als are OK.
Test t he gam e. Now y ou w ill see t hat t he enem ies appear at differ ent places, at different t im es and at differ ent speeds. Just what we w ant ed!
5.3 Ouch! That hurt!
So far, t he enem ies are not danger ous. Ther e is no point in avoiding t hem . Well, t hat we will have t o r em edy!
I f y ou have creat ed any gam e befor e, you have probably used t he Collision Det ect ion feat ur e of Gam e Maker. I t is really great . We will use it now t o det ect collisions bet w een t he player and t he enem ies.
Here y ou add an “ Ex ecut e a script ” act ion and select t he script
So, t he ent ire script “ PlayerEnem y1Collision” should now look like:
The difference bet w een t his script and t he previous is t hat her e m ost of t he wor k is done on t he enem y inst ance, and not on t he inst ance of t he obj ect t hat cont ains t he code ( t he “ obj Bullet ” obj ect ) .
To hav e m ore t han one st at em ent inside a “ w it h” st at em ent , you could use t he curly braces like t his. As y ou can see, it is also possible t o “ nest ” t he curly braces. That m eans, in t his exam ple, t hat y ou can have an “ if” st at em ent
inside a “ w it h” st at em ent . When y ou do like t his, y ou usually use t w o TAB
charact er s in t he beginning of t he deepest nest ed lines t o show t hat t hey belong inside t he “ if” st at em ent . Once again, t he TAB char act ers, or , as it is also called, “ indent at ion” is only im port ant t o t he hum an eye. The com put er does not car e.
So, t his script low ers t he enem y ’s energy w it h 50 unit s and checks if t he energy lev el is 0 or less. I f so, t he enem y is dest royed.
Finally t he script also dest roys t he bullet . Ot her w ise it w ould have cont inued t o m ove across t he scr een. I hav e also put som e com m ent s in t his script t o show how I usually use com m ent s. They ar e supposed t o incr ease t he readabilit y of t he code and m ake it easier t o under st and.
Open t he obj ect w indow for t he “ obj Bullet ” obj ect . Find and select t he collision ev ent w it h “ obj Enem y1” . Add an “ Ex ecut e a script ” act ion and select t he script w e j ust cr eat ed ( “ Bullet Enem y Collision” ) .
Now w e only need t o give t he enem ies a st art ing energy lev el. We already have an init ializing script for t he enem ies, so let ’s use it . Open up t he script “ Enem y1I nit ” and add t he line:
myEnergy = 100;
That should set t he st ar t ing energy for t he enem ies t o 100.
Save t he gam e and t r y it out . Gr eat ! Now it is possible t o shoot t he enem y craft , but only t he slow ones. The ones m oving fast are hard t o hit .
5.5 Pyrotechnics
Explosions! They ar e w hat are m issing from our gam e. Fort unat ely Gam e Maker com es bundled w it h a nice explosion anim at ion. We w ill creat e an explosion obj ect from it .
First , w e need t o cr eat e t he sprit e. This t im e it w ill be an anim at ing sprit e, m eaning it has m or e t han one im age. Cr eat e a new sprit e and call it “ spr Explosion” . Load int o it t he im age file t hat is called “ explode2.gif” . I t should be locat ed in t he Gam e Maker direct ory, in t he “ Sprit es \ Various” folder. When you hav e loaded it , y ou w ill not ice t hat t he sprit e w indow says t he num ber of subim ages is 17. Click on t he “ Edit Sprit e” but t on.
You w ill now see all 17 subim ages of t he sprit e. They ar e nam ed “ im age 0” t o “ im age 16” . To see t hem anim at ed, check t he lit t le checkbox in t he upper left corner of t he Sprit e Edit w indow , t he one t hat is called “ Show Pr eview ” .
Select im age 0 and click t he r ed arr ow point ing t o t he right in t he t oolbar. This m oves t he im age one st ep t o t he right . Keep clicking on t he right arr ow unt il t he im age is m oved all t he w ay t o t he end of t he anim at ion. The nam e of t he select ed im age should now be im age 16. Now , select t he new “ im age 0” and m ov e it t o t he end of t he im age sequence. You can use t he k ey
com binat ion [ CTRL] + [ R] inst ead of clicking on t he right arr ow all t he t im e. Keep m oving im ages like t his unt il im age 0 is t he im age w it h t he sm allest bright spot and im age 16 is an em pt y im age w it h j ust t he gr een backgr ound. I t should look like t his w hen y ou ar e ready:
I f y ou check t he “ Show Preview ” box again, you should not ice no differ ence. But t hat is j ust because t he anim at ion is looped endlessly w hen looking at t he preview . When using it in t he gam e, w e w ill only see t he sequence once, and t hen it is m or e im port ant w her e it st art s.
Close t he sprit e edit w indow and t he sprit e w indow . Cr eat e a new obj ect , called “ obj Explosion” . Select t he sprit e “ spr Explosion” for t he new obj ect . I n t he “ ANI MATI ON END” ev ent of t he “ obj Explosion” obj ect , add an “ Ex ecut e a piece of code” act ion. The “ ANI MATI ON END” event can be found in t he select ion box of t he ev ent key w it h a quest ion m ark on it :
I n t he code w indow t hat pops up, dest r oy t he inst ance:
instance_destroy();
That will dest roy t he ex plosion inst ance once it has played t hrough it s anim at ion fram es.
Now , open t he obj ect “ obj Enem y1” . I n t he “ DESTROY” event of “ obj Enem y1” , add an “ Execut e a piece of code” event . We use t his act ion inst ead of t he script act ion because t he code w e will execut e her e is so sm all t hat it is com plet ely unnecessary t o hav e a fr eest anding script for it . When t he enem y is dest r oy ed w e want an explosion t o show up, so w e want t o cr eat e an explosion inst ance w hen t he enem y is dest r oy ed. I n t he new script w indow t hat pops up, w rit e:
instance_create(x, y, objExplosion);
Save t he gam e and st ar t it . Now t here is a beaut iful explosion show ing up w henev er an enem y is shot dow n.
6 Enhancing the game
Ther e ar e som e part s of t he gam e t hat need enhancem ent t o look good. For exam ple t he bullet does not com e out at t he m iddle of t he “ obj Player” , and t he enem ies sort of “ pops up” on t he scr een inst ead of flying int o it . Tim e t o fix t hat .
6.1 Centered
sprites
We w ill begin t o look at w hy t he bullet s do not com e out from t he m iddle of t he player plane. Open t he sprit e “ spr Player” and click on t he “ Advanced” t ab.
Here y ou w ill find t he “ Origin” set t ings. I t consist s of an x value and a y value. These values det erm ine t he so- called origin of t he sprit e. Their default values are ( 0, 0) . That m eans t hat if w e place t he sprit e at locat ion ( 100, 100) , in t he r oom , it w ill look like t his:
Then, w hen a bullet is creat ed at t he sam e locat ion as t he plane, it w ill look like t his:
We w ould rat her w ant t he ball t o st art in t he m iddle front of t he plane. What w e do is t hat w e “ cent er ” t he origins of bot h sprit es. St art w it h t he “ sprPlayer” sprit e.
Click on t he “ St andard” t ab of t he “ sprPlayer” sprit e again. Her e you can see t he w idt h and height of t he sprit e. Not e t hat t he w idt h of t he sprit e is 52, and t he height is 78. Now , go back t o t he “ Advanced” t ab again. Ent er “ 26” as t he X origin, and “ 39” as t he Y origin ( 26 = 52/ 2 and 39 = 78/ 2) . Do t he sam e t hing wit h t he “ sprBullet ” sprit e, w here you should ent er “ 8” as t he origin for X, and “ 8” as t he origin for Y ( t his is t he m iddle of t he sprit e) .
(100, 100)
Now , if t he player sprit e and t he bullet sprit e ar e placed at t he sam e locat ion, for exam ple ( 100, 100) , it will look like t his:
The cent er of t he bullet sprit e w ill coincide w it h t he cent er of t he player sprit e. Good. But we would like it t o st art m or e like j ust in front of t he plane. This will be fixed t hr ough fiddling a bit w it h t he “ inst ance_creat e” funct ion, but first , sav e and run t he gam e t o see t he differ ence.
Not ice t hat t he bullet s now appear at t he cent er of t he player plane?
Now , open up t he w indow s for t he “ spr Explosion” sprit e and cent er it . The origin coordinat es should be ( 35, 50) , since t he sprit e is 71 x 100 pixels large. Then, open t he “ spr Enem y1” sprit e and ent er ( 32, 32) as t he origin of t hat sprit e. Good. Now all sprit es ar e what I called “ cent er ed” .
Open t he “ obj Player” obj ect and select t he < Space> k ey ev ent . Double- click on t he “ Ex ecut e a piece of code” act ion in t he act ion sequence t o open up it s code w indow . This is w her e an inst ance of t he “ obj Bullet ” obj et is cr eat ed. We w ant t he bullet t o be cr eat ed a bit higher up on t he scr een. Higher up m eans lower y- coordinat es. So, change t he “ inst ance_cr eat e” line so t hat it reads:
instance_create(x, y – 15, objBullet);
Close t he code w indow , save t he gam e and t ry it out . Hm m . A bit bet t er , but t he bullet should appear ev en higher up. OK. Let us change t he code again. This t im e, change it t o:
instance_create(x, y – 25, objBullet);
Then t r y out t he gam e again. This looks good t o m e. Som et im es you j ust have t o t r y out differ ent values unt il t hings look good.
6.2 Smoother enemy appearance
The enem ies st ill pops up out of now her e. I t w ould be nicer if t hey kind of flew int o t he screen. That could be fixed t hough m aking sure t hey are creat ed
out side t he scr een and t hen fly int o it . This w ill bring up a sm all problem w it h
t he “ Out side” ev ent , but w e w ill look at t hat as it com es up.
Open up t he script called “ Cr eat eEnem y1” . This is w her e t he enem y inst ances are creat ed. The “ inst ance_cr eat e” funct ion is used t o cr eat e an enem y
inst ance at a specified x and y coordinat e. We w ant t o change t his so t hat t he enem y is cr eat ed higher up, w hich m eans w e w ill have t o low er t he y
coordinat e of t he cr eat ion point . Change t he first line in t he script t o r ead:
instance_create(floor(random(screen_width)), -64, objEnemy1)
Save and run t he gam e. You w ill not ice t hat no enem ies appear at all. Why is t his? This is because of t he “ Out side” ev ent of t he enem y obj ect . Open t he obj ect “ obj Enem y1” and select t he “ Out side” ev ent . You will see t hat t his ev ent ex ecut es t he script called “ Enem yDisappear ” . This script is called ev er y t im e t he enem y inst ance is locat ed out side t he scr een. But w e do not w ant
t he enem y t o disappear w hen t he inst ance is above t he screen, only w hen it is
below t he scr een. OK, so let us do a t est in t he “ Enem yDisappear ” script .
Open t he script and change it so t hat it looks like t his:
if (y > screen_height + sprite_yoffset) then {
instance_destroy(); }
That w ill check if t he enem y is locat ed below t he scr een, t hat is, if t he y coordinat e of t he enem y is larger t han t he screen height plus t he enem y
sprit e origin. The y coor dinat e of t he sprit e origin is aut om at ically st or ed in
t he variable “ sprit e_y offset ” , w hich w e use her e. This is needed, because ot her w ise t he enem y w ould be dest royed as soon as t he origin w as out side t he scr een, w hich w ould m ean t hat half t he enem y sprit e w ould st ill be inside t he scr een and visible. Not good. This script w ill m ake sur e t hat t he ent ire enem y sprit e is out side t he scr een befor e dest r oying t he inst ance.
I f y ou t ry out t he gam e now , y ou should not ice t hat t he enem ies appear sm oot hly flying int o t he screen, and flying out from t he scr een. But t here is a part of an explosion show ing up as t he enem ies ar e dest r oy ed. This m ust be corr ect ed. The explosion inst ance is creat ed in t he “ DESTROY” ev ent of t he “ obj Enem y1” obj ect . Open it up and double- click on t he act ion “ Execut e a piece of code” t o edit it .
All t his code does is t o creat e an inst ance of t he “ obj Explosion” obj ect . We could m ake it t est t he y coordinat e of t he enem y before creat ing t he ex plosion so t hat t he explosion is only creat ed if t he enem y is st ill on t he scr een.
Do t o t hat , w e t est t hat t he y coordinat e is less t han t he screen height plus t he sprit e y origin. Change t he script so t hat it reads:
if (y <= screen_height + sprite_yoffset) then {
instance_create(x, y, objExplosion); }
Close t he code w indow and t ry t he gam e. Now t he enem ies should disappear silent ly, w it hout and explosion. But t he explosion should st ill be cr eat ed if t he enem ies ar e shot dow n.
6.3 Going
global
One t hing t hat I t hink is very im port ant t o know of is global variables. They are variables t hat do not belong t o any part icular inst ance, but are accessible from all inst ances, all t he t im e. You could t hink of it as if t her e w as an
inst ance called “ global” t hat cont ained all global variables.
Global variables ar e good for cont aining values like scor e, healt h, m ax and m in values and such. The first t hing t hat w e ar e going t o use a global variable for is t he speed of t he player. I t hink t he speed is a bit t oo slow . To change t he speed now requires t he change of a num ber at four different places in t he code. I t w ould be bet t er if t he speed could be changed by j ust changing t he code in a single place. This could be done in ot her w ay s, but w e w ill use a global variable.
Creat e a script t hat is called “ Gam eSt art ” . Add t he follow ing line t o t he script :
global.playerMaxSpeed = 8;
Now w e need t o execut e t his script from som ew her e. The “ Gam e St art ” event of t he “ obj Player” obj ect seem s like a good place. Open t hat ev ent and add an “ Ex ecut e a script ” act ion, and select t he script we j ust cr eat ed.
Now , in t he < Left > k ey ev ent of “ obj Player” , change t he code t hat is ex ecut ed t here t o:
x -= global.playerMaxSpeed;
The code in t he < Right > key ev ent should read:
x += global.playerMaxSpeed;
And t he code in t he < Up> key ev ent should be:
y -= global.playerMaxSpeed;
Finally, open t he script “ Mov ePlayerDow n” and change t he code t o:
y += global.playerMaxSpeed;
Now , save and run t he gam e.
I f w e lat er decide t o change t he play er speed, w e only need t o change t he code in one place, in t he script “ Gam eSt art ” .
I t is a good pr ogram m ing pract ice t o use num bers as lit t le as possible in your gam es. The num ber s should inst ead be defined in an init ializat ion script , like “ Gam eSt art ” , t o global variables or som et hing like t hat , and t hen t hose variables should be used inst ead. This great ly sim plifies any changes t hat need t o be done t o t he gam e lat er.
We w ill add som e ot her t hings t o t he “ Gam eSt ar t ” script . First , w e add t he m axim um energy level of t he player. Like t his:
global.playerMaxEnergy = 100;
Then w e change t he “ m yEnergy” line in t he script “ Player I nit ” t o r ead:
myEnergy = global.playerMaxEnergy;
I t is a good t hing t o hav e all t hose value definit ions in a single place.
We cont inue adding values t o t he “ Gam eSt art ” script unt il it looks like t his:
global.playerMaxSpeed = 8; global.playerMaxEnergy = 100; global.enemy1MaxEnergy = 100; global.enemy1Damage = 30;
global.bulletToEnemy1Damage = 50;
Ther e m ay be m or e values t hat can be changed like t his, but I w ill st op her e. To use t hese global variables, w e need t o change som e script s a bit . Open t he script “ Enem y1I nit ” and change t he set t ing of t he “ m y Energy” variable so t hat it reads:
myEnergy = global.enemy1MaxEnergy;
Then open t he script “ PlayerEnem y1Collision” and change t he decr ease of t he “ m yEnergy” variable t o:
myEnergy -= global.enemy1Damage;
Finally, open t he “ Bullet Enem y Collision” and change t he energy decr ease st at em ent t o:
myEnergy -= global.bulletToEnemy1Damage;