Gamecreators Beginners Guide To Game Maker Programming pdf

Gratis

0
0
41
7 months ago
Preview
Full text
(1)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

(2) 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

(3) 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

(4) 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

(5) 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

(6) 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

(7) 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

(8) 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

(9) 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

(10) 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

(11) 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

(12) 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

(13) 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

(14) 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

(15) 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

(16) 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

(17) 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

(18) 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 ” . For a st art er we only want t he enem y cr aft t o t ravel dow n across t he scr een. So, in t he new script w e w rit e: vspeed = 10; That will m ake what ever obj ect calls t he script m ov e downward across 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” , which dest roy 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 we will add t wo scr ipt s for t he enem y cont roller obj ect . Creat e t wo new script s and call t hem “ Enem yCont rollerI nit ” and “ Creat eEnem y1” . I n t he script “ Enem yCont rollerI nit ” , writ e t he following: alarm[0] = 30; That will 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 riggers it will call t he ot her script , called “ Cr eat eEnem y1” . I n t hat script , writ e: instance_create(50, 0, objEnemy1); alarm[0] = 30; The first line will creat e a new inst ance of t he obj ect “ obj Enem y1” at t he xcoordinat e 50 and t he y - 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. Now we should m ake t he obj ect s call t hese script s. Open t he obj ect “ obj Enem y1” . Select t he ev ent “ CREATE” . This ev ent will happen when an inst ance of t he obj ect is cr eat ed. Add t he act ion “ Execut e a script ” . I n t he 18

(19) Beginners Guide to Game Maker Programming window t hat pops up, select t he script “ Enem y 1I nit ” . Click OK t o close t he act ion window. 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 window. Open t he obj ect window 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 rollerI nit ” . Close t he act ion window. 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 window of “ obj Enem yCont roller” . 5.2 Randomizing The appearance of t he enem ies is quit e boring t hough. We would want t hem t o appear at random posit ions along t he t op of t he screen, and at random int ervals and random speeds. That would m ake t he gam e a lot m ore int erest ing, don’t you t hink? That is what t he “ random ” 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 ake t hings happen random ly or perhaps t o sim ulat e a dice. I t ret urns a random value t hat is always less t han t he specified value. Like t his: random(3); The abov e line will ret urn values from 0 t o 2.9999999999999999999. I f I am corr ect ly inform ed, Gam e Maker w or ks wit 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 away all decim als, so t hat , for exam ple: floor( 1.23423) = 1 floor( 2.999999999) = 2 I f w e want ed t o sim ulat e a 6- sided dice, w e w ould use: myDice = random(6); The abov e line would 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 result . 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 window, we could do all t here calculat ions in one line: myDice = floor(random(6)) + 1; 19

(20) Beginners Guide to Game Maker Programming The abov e line m ight be a bit hard t o read, but it carries 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 we want 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 values ranging from 0 t o 639, which is t he widt h of our gam e screen ( 640 pixels wide) . The st at em ent random(640); will produce values from 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 we lat er want 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_widt h” . So, inst ead of using “ 640” w e w ill use “ screen_widt 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 careful wit h t he parent heses, since Gam e Maker m ight crash if t hey are not all t here. I f you w ant , you 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 ween 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 random 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, we want 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 will give speeds fr om 2 t o 9.999999. We do not use “ floor” here, since speeds wit h decim als are OK. Test t he gam e. Now y ou will see t hat t he enem ies appear at different 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 you have creat ed any gam e before, 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 ween t he player and t he enem ies. Creat e a new script and call it “ PlayerEnem y1Collision” . Open t he obj ect window for t he “ obj Player” obj ect and select t he ev ent for collision wit h t he “ obj Enem y1” obj ect . That is t he ev ent but t on w it h t he t wo red ar rows point ing at each ot her. Click on t he select ion box next t o it and select “ obj Enem y1” . Good. 20

(21) Beginners Guide to Game Maker Programming Here y ou add an “ Ex ecut e a script ” act ion and select t he script “ Player Enem y1Collision” for t he act ion. Close t he act ion window and t he “ obj Player” obj ect window. Now we can concent rat e on t he script . We want t o m ak e it possible t o run int o a num ber of enem ies before t he player dies. Ot herwise t he gam e m ay be t oo difficult t o play. So, w e are going t o use som et hing we call “ Energy” t o m easure how m uch beat ing t he “ obj Player ” can t ake before it blows up. Let ’s say t he “ obj Player” st art s out having 100 energy unit s. Then, ev ery t im e it runs int o an enem y craft it will lose 30 energy unit s. Fair enough? Then, when it reaches 0 energy unit s, it will be dest roy ed. The enem y craft will be dest roy ed im m ediat ely when hit t ing t he “ obj Player” . I n t he collision event script we will t herefore need t o decr ease t he ener gy of “ obj Player” . I t could be done like t his: myEnergy -= 30; We also need t o check if t he energy is 0. I f it is, t he inst ance of “ obj Player” should be dest royed. Rem em ber t he “ if” st at em ent ? if (myEnergy = 0) then { instance_destroy(); } Here I hav e deliberat ely included a bad t hing about t he “ if” st at em ent . Can you see what is wr ong? I t is t he t est , “ = ” . This st at em ent will only check if t he energy is exact ly 0. But what about if t he energy is first 10, and t hen y ou run int o an enem y and lose 30 energy unit s, and t he energy becom es –20? Then t his “ if” st at em ent will not t rigger and dest roy t he inst ance of “ obj Player” . So, it is bet t er t o use t he “ < = ” t est operat or. I t m eans “ Less t han, or equal t o” . So, w rit e t his inst ead: if (myEnergy <= 0) then { instance_destroy(); } That will dest roy t he “ obj Player” if t he energy is 0 or less t han 0. We also want t he enem y t o be dest royed, so add t he line with (other) instance_destroy(); t o t he BEGI NNI NG of t he script . This int roduces t he “ wit h” st at em ent . The “ wit h” st at em ent is ext r em ely useful. I t allow s us t o do som et hing t o anot her inst ance. I n t his case w e w ant t o dest roy t he inst ance t hat we collide wit h. This is called t he “ ot her” inst ance in a collision event . So w it h t he ot h e r inst ance w e want it t o de st r oy it self. The code line abov e is t he sam e as w rit ing “ inst ance_dest r oy( ) ” in t he code of t he ot her inst ance’s obj ect . So, t he ent ire script “ PlayerEnem y1Collision” should now look like: with (other) instance_destroy(); myEnergy -= 30; if (myEnergy <= 0) then { instance_destroy(); } Now we only need t o set a st art ing energy lev el. We decided t o st art at 100 energy unit s. This should be set in t he “ CREATE” event of t he “ obj Player” . We had bet t er creat e a script for it , t his is what t his t ut orial is for aft er all. So, cr eat e a new script and call it “ Player I nit ” . 21

(22) Beginners Guide to Game Maker Programming Ent er t he follow ing line in t he script : myEnergy = 100; I f w e do not init ialize t he variable “ m yEnergy” t o anyt hing, an er ror will appear when t he “ obj Player” hit s an inst ance of t he “ obj Enem y1” obj ect . You could t ry t o run t he gam e ( save first ! ) before set t ing t he CREATE event of t he “ obj Player” t o see what it looks like when y ou forget t o init ialize a variable. I t could be good t o know, because it is easy t o for get it . When y ou hit anot her craft it say s: “ Unknow n variable or funct ion: m yEnergy” So, click “ Abort ” and let us set t he CREATE event of t he “ obj Player” obj ect . Open t he “ obj Player” obj ect , select t he CREATE ev ent . Here you will see t he “ Ex ecut e a piece of code” act ion t hat we added earlier. Double- click on it . I t should only cont ain one line of code; t he init ializat ion of t he “ gunReady” variable. This is a good t hing t o m ov e t o our new “ PlayerI nit ” script . So, select t he code in t he code window and copy it . Close t he code window and delet e t he act ion “ Execut e a piece of code” fr om t he act ion sequence. Add an “ Ex ecut e a script ” act ion and select t he script “ Player I nit ” . Now , open t he script “ PlayerI nit ” and past e t he code from t he act ion we j ust delet ed. I f y ou have done it right , t he script “ PlayerI nit ” should now cont ain: gunReady = true; myEnergy = 100; Now, save t he gam e and run it again. You should not ice t hat t he enem y aircraft disappear when y ou run int o t hem . When y ou run int o t he fourt h craft , t he inst ance of “ obj Player” t oo disappears. Right ! Now t here is a point in avoiding t he enem ies. 5.4 Shootout So far y our bullet s have done not hing t o reduce t he oncom ing swarm of enem y craft . The point of having a gun in a gam e is t o be able t o do som e dam age. So w ee need t he bullet s fired from t he “ obj Player ” inst ance t o dam age t he enem y craft . We will m ake a script t hat t akes care of t he collision bet ween a bullet and an enem y craft . Creat e a new script and call it “ Bullet Enem yCollision” . This script should work in alm ost t he sam e way as t he collision script for t he “ obj Player” and “ obj Enem y1” . The energy lev el of t he enem y should be decreased, and a check should be m ade t o see if t he energy lev el is 0 or less. Let ’s say t hat t he enem y craft st art out wit h an energy lev el of 100, t hey t oo. Then when t hey ar e hit by a bullet , t heir energy level should be decr eased wit h 50 unit s, which m eans t hat it t akes t wo bullet s t o kill an enem y. Here is t he script we will use: with (other) { // Lower enemy energy myEnergy -= 50; // Check if enemy energy is 0 or less if (myEnergy <= 0) { // If so, destroy enemy. instance_destroy(); } } // Destroy this bullet instance_destroy(); 22

(23) Beginners Guide to Game Maker Programming The difference bet w een t his script and t he previous is t hat here 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 “ wit 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 “ wit h” st at em ent . When you do like t his, you usually use t w o TAB charact ers 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 care. So, t his script lowers t he enem y ’s energy wit 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 herwise it w ould have cont inued t o m ove across t he screen. 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 window for t he “ obj Bullet ” obj ect . Find and select t he collision ev ent wit h “ obj Enem y1” . Add an “ Ex ecut e a script ” act ion and select t he script we j ust cr eat ed ( “ Bullet Enem y Collision” ) . Now we 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 art ing energy for t he enem ies t o 100. Save t he gam e and t ry 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 are w hat are m issing from our gam e. Fort unat ely Gam e Maker com es bundled wit h a nice explosion anim at ion. We w ill creat e an explosion obj ect from it . First , we need t o cr eat e t he sprit e. This t im e it will 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 have loaded it , you 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 will now see all 17 subim ages of t he sprit e. They are 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 window, t he one t hat is called “ Show Preview” . I f y ou look at t he im ages, y ou can see t hat im age 0 displays t he explosion already a bit exploded. I t seem s t hat t he anim at ion st art s som ewhere in t he m iddle of t he explosion. This is st range, and I have no idea why it is like t his. We will have t o change it in order for t he explosion t o look correct when used in t he gam e. 23

(24) Beginners Guide to Game Maker Programming Select im age 0 and click t he red arrow 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 way 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 wit h t he sm allest bright spot and im age 16 is an em pt y im age wit h j ust t he green backgr ound. I t should look like t his when y ou ar e ready: I f you check t he “ Show Preview ” box again, you should not ice no difference. But t hat is j ust because t he anim at ion is looped endlessly when looking at t he preview. When using it in t he gam e, w e will only see t he sequence once, and t hen it is m or e im port ant wher e it st art s. Close t he sprit e edit window and t he sprit e window. 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 wit h a quest ion m ark on it : I n t he code w indow t hat pops up, dest roy 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 have 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 when t he enem y is dest r oy ed. I n t he new script window t hat pops up, w rit e: instance_create(x, y, objExplosion); That will creat e an explosion at t he sam e coordinat es as t he “ obj Enem y 1” inst ance. Rem em ber, for an inst ance, t he variables x and y cont ains t he coordinat es for t hat inst ance. 24

(25) Beginners Guide to Game Maker Programming Save t he gam e and st ar t it . Now t here is a beaut iful explosion showing up whenev er an enem y is shot down. Bet t er add t he explosion t o t he “ DESTROY” ev ent of t he “ obj Player” t oo. You should be able t o do t hat on your own now . I w ill not t ell you how t o do ; ) . Use t he sam e “ obj Explosion” obj ect as for t he enem y. 25

(26) Beginners Guide to Game Maker Programming 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 will begin t o look at why t he bullet s do not com e out from t he m iddle of t he player plane. Open t he sprit e “ sprPlayer” and click on t he “ Advanced” t ab. Here y ou will 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 we place t he sprit e at locat ion ( 100, 100) , in t he r oom , it will look like t his: (100, 100) Then, when a bullet is creat ed at t he sam e locat ion as t he plane, it will look like t his: (100, 100) We would rat her want t he ball t o st art in t he m iddle front of t he plane. What we do is t hat we “ cent er” t he origins of bot h sprit es. St art wit h t he “ sprPlayer” sprit e. Click on t he “ St andard” t ab of t he “ sprPlayer” sprit e again. Here you can see t he widt h and height of t he sprit e. Not e t hat t he widt 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, where 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) . 26

(27) Beginners Guide to Game Maker Programming 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: (100, 100) The cent er of t he bullet sprit e will coincide wit 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 hrough fiddling a bit wit h t he “ inst ance_creat e” funct ion, but first , save and run t he gam e t o see t he difference. 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 indows for t he “ sprExplosion” 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 window. This is wher e an inst ance of t he “ obj Bullet ” obj et is cr eat ed. We want 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 window, 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 nowher 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 screen and t hen fly int o it . This will bring up a sm all problem wit h t he “ Out side” ev ent , but we will look at t hat as it com es up. Open up t he script called “ Cr eat eEnem y1” . This is wher 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, which m eans w e will have t o lower t he y coordinat e of t he cr eat ion point . Change t he first line in t he script t o read: 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 27

(28) Beginners Guide to Game Maker Programming t he enem y t o disappear when t he inst ance is above t he screen, only when it is below t he screen. 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 will check if t he enem y is locat ed below t he screen, 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 ” , which we use her e. This is needed, because ot her wise t he enem y w ould be dest royed as soon as t he origin was out side t he screen, which would m ean t hat half t he enem y sprit e would st ill be inside t he screen and visible. Not good. This script will m ake sure 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 are dest roy 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 window and t ry t he gam e. Now t he enem ies should disappear silent ly, wit hout and explosion. But t he explosion should st ill be cr eat ed if t he enem ies ar e shot down. 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 was 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 we are 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 would 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 way s, but w e will use a global variable. Creat e a script t hat is called “ Gam eSt art ” . Add t he following line t o t he script : global.playerMaxSpeed = 8; Not e t he w ord “ global” and t he dot before t he v ariable nam e. This is how global variables ar e used. To r ead a bit m ore about global variables, read sect ion 23.5 of t he Gam e Maker Manual. 28

(29) Beginners Guide to Game Maker Programming Now we need t o execut e t his script from som ewher 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 ePlayerDown” 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 player 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 will add som e ot her t hings t o t he “ Gam eSt art ” script . First , we 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 ore values t hat can be changed like t his, but I will st op here. To use t hese global variables, we 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; Now it is m uch easier t o change t hose param et ers of t he gam e. Save t he gam e. There should be no need t o run it , ot her t han t o see t hat no errors appear. Not hing should have changed regarding t he gam eplay. 29

(30) Beginners Guide to Game Maker Programming 6.4 Where’s my energy? We hav e been t alking a bit about t he energy of t he player and t hat if t he energy goes dow n t o 0 t he player is dest roy ed. How ev er, so far w e hav e not seen any indicat ion of t hat energy. Tim e t o m ake an energy m et er. The energy m et er will be cr eat ed wit hout using any kind of sprit e. I nst ead we will have a first look at som e of t he ot her drawing possibilit ies of Gam e Mak er. I n order t o draw anyt hing on t he screen, an obj ect is needed. We will creat e anot her cont roller obj ect t o do t his. Ther e will not be any sprit e drawn, but we need a sprit e anyway, j ust t o represent t he inst ance of t he cont r oller obj ect in t he r oom . So, cr eat e a new sprit e, call it “ sprPlayerCont roller” and load t he im age “ Dish.bm p” fr om t he “ Sprit es \ Space” folder of t he Gam e Mak er direct or y. Then creat e a new obj ect , “ obj PlayerCont r oller” and select t he “ sprPlayerCont roller” sprit e for it . Add an inst ance of t he new obj ect t o t he room . This obj ect will be used t o creat e t he “ obj Player” inst ance. Therefor e t his inst ance should be rem ov ed from t he room . Rem ov e t he inst ance of “ obj Player” ( t he SR71 plane) fr om t he room t hrough right - clicking on it . Now t he player will not exist when first st art ing t he gam e, w e will have t o cr eat e t he player from t he cont roller. The reason for doing t his is t hat it is not always clear in which order Gam e Maker cr eat es t he inst ances, at least I do not know in w hich order t hey are creat ed. So w e m ust m ake sure t hat first t he cont roller inst ance is creat ed, and t hen t he player inst ance. Anot her reason is t hat t he cont r oller need t o know t he inst ance I D of t he player inst ance, and t hat is easiest t o ret rieve t hrough cr eat ing t he player inst ance from t he cont r oller obj ect . The script “ Gam eSt art ” should be run by t he “ obj PlayerCont roller” inst ead of t he “ obj Player” . Rem ov e t he “ Execut e a script ” act ion from t he “ Gam e St art ” ev ent of “ obj Player” . Open t he “ obj Player Cont r oller” obj ect and add an “ Ex ecut e a script ” act ion t o t he “ Gam e St art ” event . Select t he “ Gam eSt art ” script . Good. Now we need a script t o cr eat e t he player. Cr eat e a new script and call it “ Cr eat ePlayer” . Add t his line: myPlayer = instance_create(screen_width / 2, 400, objPlayer); That will creat e an inst ance of t he “ obj Player” obj ect . The x coordinat e will be t he cent er of t he screen, and t he y coordinat e is som ewher e along t he bot t om of t he scr een. Not ice t hat I hav e w rit t en “ m y Player = ” in fr ont of t he “ inst ance_creat e” funct ion. That is because w hen t he “ inst ance_creat e” funct ion has creat ed t he inst ance of t he player, it ret urns a value. That is how a funct ion work s, rem em ber ? Usually we do not care about t hat v alue, and t hat is OK. But t his t im e we want t o st or e t he r et urned value in a local variable, “ m yPlayer ” . The value t hat is ret urned is t he “ inst ance I D” of t he newly cr eat ed player inst ance. We will use it lat er t o ret rieve som e local variables from t he player inst ance. I n t he obj ect window for “ obj PlayerCont r oller” , select t he “ CREATE” ev ent , add an “ Execut e a script ” act ion and select t he “ Creat ePlayer” for t hat act ion. Now it is t im e t o draw t he energy bar. Add a new script ( don’t you lov e t hem already? ☺ ☺) and call it “ Draw EnergyBar” . I n t he “ DRAWI NG” ev ent of t he “ obj PlayerCont r oller” obj ect , add an “ Ex ecut e a script ” act ion. Select t he “ Draw EnergyBar” scr ipt . 30

(31) Beginners Guide to Game Maker Programming Then, go back t o t he script . I int end t o draw an energy bar t hat look s som et hing like t his: First we draw t he gray background rect angle, and t hen we draw t he blue energy rect angle on t op of it . To draw shapes in Gam e Maker w e first need t o set a pen and brush color. Ent er t hese lines int o t he script : pen_color = make_color(150, 150, 150); brush_color = make_color(150, 150, 150); The “ m ake_color ” funct ion creat es a color out of t hree color values. The values inside t he parent heses are t he red, green and blue levels of t he color. The num bers range from 0 t o 255, which m eans t hat “ m ak e_color( 255, 0, 0) ” would creat e a very red color, and “ m ake_color( 255, 0, 255) ” would cr eat e a very violet color ( red and blue) . The values used in t he script ( 150, 150, 150) will creat e a m edium gray color. The pen color is used t o “ out line” t he rect angle, and t he brush color is used t o fill it wit h a color. When t he color set t ing is done, w e could draw t he rect angle. To do t hat we need t w o pairs of coordinat es, ( x1, y1) and ( x2, y2) . See t he im age below: (x1, y1) (x2, y2) We w ant t o put t he energy bar in t he low er left corner of t he screen, so w e could do like t his: x1 = 5; y1 = screen_height – 15; x2 = 110; y2 = screen_height – 5; draw_rectangle(x1, y1, x2, y2); Now w e have creat ed four variables and used t hem as coordinat es in t he “ draw_rect angle” funct ion. We could j ust have ent ered t he coordinat e calculat ions direct ly int o t he “ draw_rect angle” funct ion, but t he line would have been so long, so I chose t his way t o r epresent t hem . Finally we want t o draw t he blue bar t hat represent s t he energy it self. Change t he color t o som e green color and draw t he new rect angle: 31

(32) Beginners Guide to Game Maker Programming pen_color = make_color(0, 0, 255); brush_color = make_color(0, 0, 255); x1 = 7; y1 = screen_height – 13; x2 = 7 + myPlayer.myEnergy; y2 = screen_height – 7; draw_rectangle(x1, y1, x2, y2); Here w e see what t he “ m yPlayer” variable should be used for . I t is used for get t ing t he “ m yEnergy” variable from t he “ obj Player” inst ance t hat was cr eat ed in an earlier script . To get a local variable from anot her inst ance, y ou use t he inst ance I D of t hat inst ance, followed by a dot and t he nam e of t he local variable t hat you want . Voilà! Ther e is one t hing about t his t hat is dangerous t hough! I f t he player inst ance, wit h inst ance I D “ m yPlayer” is dest r oy ed, it is no longer possible t o access t he local variable “ m yEnergy” t hr ough “ m yPlayer.m yEnergy” . This r esult s in a fat al error and Windows m ay fr eeze com plet ely. The best t hing t o do is t o t est it t he “ m yPlayer” inst ance exist s befor e using it s local variables. This is done using t he funct ion “ inst ance_exist s” like t his: if (instance_exists(myPlayer)) Now, t his is t he com plet e code of t he “ Draw EnergyBar” script : pen_color = make_color(150, 150, 150); brush_color = make_color(150, 150, 150); x1 = 5; y1 = screen_height – 15; x2 = 110; y2 = screen_height – 5; draw_rectangle(x1, y1, x2, y2); if (instance_exists(myPlayer)) { pen_color = make_color(0, 0, 255); brush_color = make_color(0, 0, 255); x1 = 7; y1 = screen_height – 13; x2 = 7 + myPlayer.myEnergy; y2 = screen_height – 7; draw_rectangle(x1, y1, x2, y2); } Make sure t hat t he script cont ains t he code above, sav e t he gam e and t est it . A blue energy bar wit h a gray background will now be visible in t he lower left corner of t he scr een. 32

(33) Beginners Guide to Game Maker Programming 7 Life, the Universe and Everything I don’t know about y ou, but I am get t ing really t ired of t he green background. Tim e t o do som et hing about t hat . This will be t he last chapt er, since if I do not st op now , t he guide m ay never be finished. Hopefully, if I r eceive a good enough response, I will cont inue t o m ake a follow- up wit h som e m ore advanced program m ing, but t hat is for t he fut ure t o see. 7.1 A galaxy far, far away The m ost com m on back ground for space gam es would be som e st ars whooshing by. I know t hat t he SR71 and t he MI G41 do not fly in space, but I , being t he suprem e ruler of t his docum ent , hav e decided t o let t hem int o t he great void. I did not find any good enough space background in t he Gam e Maker direct or y, at least not a scr ollable one, so we will have t o cr eat e our ow n. But I will keep it sim ple. First we will creat e a backgr ound im age t hat looks like sim ple st ars. Cr eat e a new background. Call it “ bgrSt ars1” . Click t he “ Edit Background” but t on. This will open up t he Gam e Maker background edit or. I t is not t he best 2D graphics applicat ion in t he world, but it knows a few t ricks and is quit e good for a st art er. The first t hing we will do is t o set t he size of t he background im age. The backgr ound im age will be “ copied” over t he ent ire room if it is not as big as t he room . This is called “ t iling” . To not m ake t he background look t oo r egular we will have t o increase t he size of t his background im age a bit . Select t he m enu choice “ Transform - > Resize Canvas” . I n t he w indow t hat appears, look for t he t w o t ext box es j ust before t he t w o “ pixels” w ords. Ent er t he num ber “ 200” in bot h of t hese. I f t he check box “ Keep aspect rat io” is checked, y ou will only need t o change one of t hem , and t he ot her will follow. Clock OK. Now our back ground im age should be 200 x 200 pixels large. Select t he black color in t he color select ion box on t he right hand of t he window. Now select t he bucket icon t hat is called “ Fill an area” . Then click in t he im age. The ent ire im age should now be black. Next , select t he whit e color and click on t he pencil icon t hat is called “ Draw on t he im age” . Use it t o click a few st ars scat t er ed random ly ar ound t he im age. Do not draw t oo m any. I dr ew only 3, and t he result looked OK in t he gam e. This is how m y im age look ed: 33

(34) Beginners Guide to Game Maker Programming Click OK t o close t he im age edit or. Now, open t he r oom window t hrough double- clicking on “ Room 1” . Then click t he “ Backgr ound” but t on in t he room window. Her e it is possible t o select which back ground im ages should be visible in t he room . Select “ Backgr ound 0” . Then, in t he select ion box on t he right side of t he window , select t he background im age we j ust cr eat ed, “ bgrSt ar s1” . Also m ak e sure t hat t he checkbox “ Visible when r oom st ar t s” is checked. The checkbox es “ Tile Hor.” and “ Tile Vert .” should also be checked. Anot her t hing t o do here is t o uncheck t he checkbox m ark ed “ Draw background color” . This w ill t urn of t he green color t hat has been used as background so far. I t is no longer needed since t he new background im age will cover t he ent ire room . OK. We ar e done her e for now. Click OK, sav e t he gam e and t est it . I f everyt hing is as it should, t here should now be som e st ars as a background t o t he gam e. I t look s rat her boring t hough. Let s add som e m ov em ent t o t he st ars. Open t he “ Room 1” window and click on t he “ Background” but t on again. I n t he lower right corner of t he window you will find a t ext box m ark ed “ Vert . speed” . I t is curr ent ly set t o 0. Set it t o 3. Click OK, save and t est t he gam e again. But w e are not done w it h t he background yet . 7.2 Cool word: Parallax Tim e t o add som e dept h t o t he background. We will use an illusion called “ Parallax” . I t is based on t he fact t hat when y ou are m oving and looking sideways, like out of t he side window of your car, obj ect s t hat ar e far away, like t rees at t he horizon, looks like t hey m ov e past slowly, while obj ect s t hat are close t o t he car, like rails and road m ar kings, swish by really fast . This can be used t o creat e an illusion t o t hat t he hum an ey e is t ricked and t he brain t hinks t hat what t he ey es see has 3- dim ensional dept h. Creat e a new background im age, call it “ bgrSt ars2” , and resize it t o 200 x 200 pixels, j ust like above. Paint it black and add som e st ars t o it . But t his t im e t he color of t he st ars should be light gray, and not com plet ely w hit e. You can add a few m ore st ars t o t his background. Five, m aybe. Then open t he room window again and click t he Background but t on. Select “ Background 1” , select t he background im age “ bgSt ars2” for t hat back ground. Make sur e t he “ Visible when r oom st art s” checkbox is checked. Give t he new background a “ Vert . speed” of “ 2” . Close t he window and t r y t he gam e. I t st ill looks like a flat st ar field. That is because w e hav e forgot t en t o m ake t he new st arfield t ransparent . OK. Open t he background “ bgt St ars2” , and check t he lit t le box called “ Transparent ” . Then save and t ry t he gam e again. Alright ! Now t her e are t wo st ar fields m oving at different speeds, creat ing a sm all illusion of dept h. To m ake it even bet t er, we will add a t hird st arfield layer. Do j ust like t he second background, but call t his one “ bgrSt ars3” , and use a dark er gray color for t he st ars in it . You can add a bit m ore st ars t oo, m aybe 10 or so. Then check t he “ Transparent ” checkbox and open up t he room . Add t he new st arfield background as " Background 2” and set t he “ Vert . speed” t o 1. For t he t hird t im e, t est t he gam e. That looks quit e OK, does it not ? Wit h j ust a lit t le bit im aginat ion you will see a “ deep” space m oving past beneat h t he plane. 34

(35) Beginners Guide to Game Maker Programming 7.3 Enemy fire I t is now t im e t o m ak e t he enem ies shoot back at you. That will not be so difficult , now t hat you k now a bit about GML. The plan is t hat wit h random int ervals, t he enem y planes should fire a bullet at you. Whenev er you are hit by a bullet , your energy would decr ease by 10 unit s. First w e need a new bullet obj ect . We should not use t he one t hat is fired from t he player plane, even if we ar e going t o r euse it s sprit e. Creat e a new obj ect , call it “ obj Enem y1Bullet ” and select t he sprit e “ spr Bullet ” for it . Now we need t he enem y plane t o shoot t his bullet at t he player. Creat e a new script for t his and call it “ Enem y1Fire” . The script should creat e a bullet inst ance and set t he alarm t im er of t he enem y so t hat anot her bullet can be cr eat ed. Writ e t his in t he script : instance_create(x, y, objEnemy1Bullet); That will creat e an inst ance of t he “ obj Enem y1Bullet ” obj ect in t he sam e coordinat es as t he enem y plane is. But it is not going anywhere. We need t o give it a speed and aim it t owards t he player. Fort unat ely t her e is a Gam e Maker funct ion t hat m akes t his ver y easy . I t is called “ m ov e_t owards_point ” , and t hen you t ell t he funct ion t o which coordinat es t he inst ance should m ov e, and how fast . Now you t hink t hat it is as easy as t o j ust w rit e t he funct ion. But no, if we j ust wrot e t his funct ion, t he enem y plane would m ove t owards t he player, and not t he bullet . To do t hat w e need t o get t he I nst ance I D of t he creat ed bullet inst ance. So, change t he line you j ust w r ot e t o t his: myBullet = instance_create(x, y, objEnemy1Bullet); Now we have got t he inst ance I D of t he new bullet and can use it in a “ wit h” st at em ent . Like t his: with (myBullet) move_towards_point(objPlayer.x, objPlayer.y, 5); That will st art t he bullet in t he direct ion where t he player is and wit h t he speed 5. Good. Tim e t o set t he alarm t o go off aft er som e t im e again, t o cr eat e anot her bullet . Add t his line t o t he code: alarm[0] = 60 + random(60); That m ay seem a bit st range, but what w e hav e done here is t o add t he value 60 and a random value bet ween 0 and 59.99999. Thus t he t im e for t he next bullet t o be creat ed will be som ewhere bet w een 2 and 4 seconds ( 60 and 119.99999 fram es) . Here I do not care so m uch about t he decim als and such, because w e will not be able t o not ice t hem in t he gam e. The code in t he script “ Enem y1Fire” should now look like t his: myBullet = instance_create(x, y, objEnemy1Bullet); with (myBullet) move_towards_point(objPlayer.x, objPlayer.y, 5); alarm[0] = 60 + random(60); What is left t o do now is t o add t he script t o t he “ alarm 0” ev ent of t he obj ect “ obj Enem y1” . Do t hat now . You should know how by now . We also need t o m ake sure t hat t he script is run t he first t im e. This is done by set t ing t he alarm in t he script t hat is run at t he creat ion of t he enem y inst ance. That script is called “ Enem y1I nit ” . Open up t he script and add t he following line t o it : alarm[0] = random(60); 35

(36) Beginners Guide to Game Maker Programming Here w e do not care t o add t he “ 60” in fr ont of t he random funct ion. I j ust did not feel like it . Do it if you want , but t hen no enem y plane will fire unt il t hey have exist ed for 2 seconds. Now t hey fire t he first t im e bet w een 0 and 2 seconds aft er t heir cr eat ion. The bullet should, j ust like t he player ’s bullet s, disappear once it has m ov ed out side t he scr een. I n t he “ obj Enem y1Bullet ” obj ect , open t he “ Out side” event and add a “ Dest roy t he inst ance” fr om t he “ Obj ect ” t ab of t he act ion panel. Accept t he default set t ings and click OK. Finally, t he bullet should do som e dam age t o t he player. Creat e a new script and call it “ PlayerBullet 1Collision” . Here we should dest roy t he bullet inst ance as w ell as lower t he energy of t he player . We also need t o check if t he player energy is 0 or less, and t hen dest roy t he player. This m eans t hat t he code should look about t he sam e as in t he “ Player Enem y1Collision” script . Writ e down t his code in t he new script : // Destroy the bullet with (other) instance_destroy(); // Lower the player’s energy myEnergy -= global.bullet1Damage; // Check if the energy is zero or less. if (myEnergy <= 0) { instance_destroy(); } Not ice t hat I hav e use t he global variable “ global.bullet 1Dam age” t o lower t he energy of t he player . This variable m ust be defined. That is done t hrough adding it t o t he “ Gam eSt art ” script . Add t his line t o t hat script : global.bullet1Damage = 10; This m eans t hat when t he enem y bullet hit s t he player, t he energy will be lowered by 10 unit s. Add t he “ PlayerBullet 1Collision” script t o t he “ obj Player” obj ect , in t he collision ev ent wit h t he “ obj Enem y1Bullet ” obj ect . Com pare t he t wo script s “ PlayerBullet 1Collision” and “ PlayerEnem y1Collision” . The last “ if” st at em ent and t he st at em ent it cont ains look t he sam e on t he t wo script s. The first line, t oo, look t he sam e, but we will ignore it for now. Since we use t he sam e block of code in m ore t han one place, it is a good idea t o put it in it s own script . So, creat e a new script and call it “ CheckPlayer Energy” . Copy t he ent ire “ if” st at em ent fr om t he “ PlayerBullet 1Collision” script t o t he new script , so it looks like t his: // Check if the energy is zero or less. if (myEnergy <= 0) { instance_destroy(); } Then w e delet e t he “ if” st at em ent s from t he ot her t w o script s, and inst ead call on t his new script . Just t o show how a script can be called from anot her script . I nst ead of t he “ if” st at em ent s in t hose t wo script s, it should look like t his: CheckPlayerEnergy(); Ther e. That is how easy it is t o use a script fr om anot her script . 36

(37) Beginners Guide to Game Maker Programming To be on t he safe side, here is a list of how t he ent ire “ PlayerBullet 1Collision” script should look like: // Destroy the bullet with (other) instance_destroy(); // Lower the player’s energy myEnergy –= global.bullet1Damage; // Check the player energy. CheckPlayerEnergy(); And t he “ PlayerEnem y1Collision” script : with (other) instance_destroy(); myEnergy -= global.enemy1Damage; CheckPlayerEnergy(); Save t he gam e and t ry it . When t he enem y planes shoot at y ou, and you are hit , t he energy will decrease unt il t he plane is dest r oy ed. Good. But wait ! Once t he player plane is dest royed you get an error m essage saying “ Unknown variable or funct ion” and referring t o t he “ m ove_t owards_point ” funct ion when t he enem y bullet is creat ed. The reason for t his is t hat we m ade t he bullet s go for t he player plane, and once t he plane is dest royed, t he bullet s no longer know w here t o go. Or, m ore program m at ically speak ing, we ar e r eferring t o an inst ance t hat no longer exist s. So, we will have t o check if it exist s before firing t he bullet . Open up t he script “ Enem y1Fire” . This is where t he enem y bullet is cr eat ed. Now we need t o t est if an inst ance of t he “ obj Player” is available. That can be done w it h t he “ inst ance_exist s” funct ion. Here is it s definit ion: inst ance_exist s( obj ) Ret urns whet her an inst ance of t ype obj exist s. obj can be an obj ect , an inst ance id, or t he k eyw ord all. So, w e need t o use t he “ if” st at em ent and t he “ inst ance_exist s” funct ion in t he script . Make t he script look like t his: if (instance_exists(objPlayer)) then { myBullet = instance_create(x, y, objEnemy1Bullet); with (myBullet) move_towards_point(objPlayer.x, objPlayer.y, 5); } alarm[0] = 60 + random(60); Not e t hat t he “ alarm [ 0] ” set t ing is not included inside t he “ if” st at em ent . That is because w e want t he script t o act ivat e a new alarm ev ent ev en if t he player plane does not exist at t he m om ent . Nice. This will only creat e t he bullet and m ove it if t he player plane exist s in t he gam e. 7.4 Meaning of life To round up t his program m ing guide, I t hink it would be a good idea t o add som e life t o t he gam e, as w ell as som e scoring. Open up t he script “ Cr eat ePlayer ” . This is wher e t he player inst ance is cr eat ed by t he “ obj PlayerCont roller” . Add t he line: playerLives = 3; This m eans t hat t he num ber of lives t hat t he player has is st or ed in t he inst ance of t he “ obj PlayerCont r oller” obj ect . Now we need t o decr ease t he num ber of lives every t im e t he player ex plodes. That is done in t he “ CheckPlayer Energy ” script . Open it up. 37

(38) Beginners Guide to Game Maker Programming I nside t he “ if” st at em ent here, we want t o low er t he num ber of lives of t he “ obj PlayerCont r oller” . We also want t o m ak e sur e t hat t he player is cr eat ed again aft er som e t im e. For t hat , we will use t he alarm funct ion of t he “ obj PlayerCont roller” . So, inside t he “ if” st at em ent , before t he “ inst ance_dest r oy ” funct ion, add t hese lines: objPlayerController.playerLives -= 1; objPlayerController.alarm[0] = 60; That will decr ease t he num ber of lives wit h 1, and set t he alarm 0 event of t he “ obj PlayerCont roller” t o t rigger aft er 60 fram es ( 2 seconds) . Tim e t o cr eat e a new script t hat t akes care of t he creat ion of t he new player, if t here are any lives left . Call t he script “ LivesCheck” . Here w e should check if t he player has any lives left , and if so, creat e a new inst ance of t he player obj ect . Ent er t his code: if (playerLives > 0) { myPlayer = instance_create(screen_width / 2, 400, objPlayer); } else { game_end(); } Here I hav e int roduced t he “ else” st at em ent . I t can be used aft er an “ if” st at em ent . I t w or ks so t hat if t he expr ession in t he “ if” st at em ent ( player Lives > 0 in out case) is not t rue, t he st at em ent s inside t he “ else” st at em ent are ex ecut ed. I n t his exam ple t his would m ean t hat if playerLives is not great er t han 0, t he gam e will end. That is what t he “ gam e_end( ) ” funct ion is for. Now we need t o add t his new script t o t he “ Alar m 0” ev ent of t he “ obj PlayerCont r oller” obj ect . Do t hat . Save and t est t he gam e. You should now hav e t hree lives t o use befor e t he gam e ends. I t w ould be nice if t he lives could be displayed for t he user in som e way . To do t hat , open up t he script “ Draw EnergyBar” . This is wher e t he ener gy bar is drawn. How about drawing a sm all version of t he player ’s plane sprit e next t o t he energy bar for each life? Select t he sprit e called “ sprPlayer” and right - click on it s nam e in t he t ree st ruct ur e on t he left of Gam e Mak er’s m ain window. Select “ Duplicat e” from t he pop- up m enu. This will creat e a duplicat e of t he player sprit e. Open up t he new sprit e and call it “ sprLife” . Click t he “ Edit Sprit e” but t on. Select t he m enu “ Transform - > St ret ch” . Ent er “ 50% ” in t he “ Widt h” and “ Height ” boxes. Select “ Excellent ” in t he Qualit y select or. Click OK. Click OK again. Now we have a sm all version of t he player sprit e t hat can be good t o use as a life sprit e. We need t o change t he “ origin” of t he sprit e t hough, so t hat it is cent ered. Go t o t he Adv anced t ab of t he sprit e w indow and set t he “ Origin” t o X: 13 and Y: 19. Then click OK t o close t his window. Go back t o t he “ Draw Energy Bar” script . To draw a sprit e in t he scr een, we will use t he funct ion “ draw_sprit e” . Here is it s definit ion: draw_sprit e( n, im g, x, y ) Draws subim age im g ( - 1 = cur r ent ) of t he sprit e wit h index n wit h it s origin at posit ion ( x , y) . We will use t he sprit e “ sprLife” and it s first subim age, which has num ber “ 0” . So t he funct ion w ill be used as ( t he coordinat es are not decided y et ) : draw_sprite(sprLife, 0, someXCoordinate, someYCoordinate); 38

(39) Beginners Guide to Game Maker Programming We will also use a new language st at em ent , t he “ for” st at em ent in order t o draw t he correct num ber of life sprit es. Please have a look at t he definit ion of t he “ for” st at em ent in sect ion 23.11, page 70 of t he Gam e Mak er Manual. “ For ” loops are used t o repeat a bunch of st at em ent s. I n pr ogram m ing lingo t his is called “ it erat ing” . An exam ple of a “ for ” st at em ent could be t his: number = 8; for (i = 0; i < 5; i += 1) { number += 1; } What t his program will do is first t o set t he v ariable “ num ber” t o 8. Then it will ent er t he “ for” loop, which adds t he value “ 1” t o t he variable “ num ber ” a couple of t im es. But how m any? The result is t hat t he variable num ber w ill be “ 13” when t his is done. That m eans t hat t he “ for” loop has been run 5 t im es. The first t im e t he “ for ” loop is run, t he v ariable “ i” is set t o 0. Then t he expr ession in t he m iddle of t he “ for ” st at em ent parent hesis is checked ( i < 5) . I f t his is t rue, t he st at em ent inside t he “ for” loop is ex ecut ed ( num ber + = 1) . Then, finally t he last st at em ent of t he “ for ” loop parent hesis ( i + = 1) is ex ecut ed, w hich w ill m ake t he variable “ i” now hold t he value 1. Once again t he m iddle expression is checked ( i < 5) . I t is st ill t rue, and t he t wo following st at em ent s ar e ex ecut ed again. This happens again unt il t he variable “ i” becom es “ 5” . Then t he expr ession “ i < 5” is not t rue any m ore, and t he “ for” loop exit s. Good. What we want t o do is t o draw t he sam e num ber of sprit es as t he value of t he “ playerLives” variable. This could be done t hrough adding t he following code t o t he end of t he “ Draw EnergyBar” script : for (i = 0; i < playerLives; i += 1) { draw_sprite(sprLife, 0, 140 + 30 * i, screen_height – 25); } Wow , t hat was a lot at t he sam e t im e. First , t he “ for” loop will be execut ed as m any t im es as t he value of t he “ playerLives” v ariable, right ? The first t im e t he “ for” loop is ex ecut ed, t he variable “ i” will be 0. That m eans t hat t he x coordinat e of t he first sprit e t hat is drawn will be 140 + 30 * 0, which is 140. The next t im e t he “ for” loop is ex ecut ed, “ i” will be 1. Thus, t he x coordinat e for t he second sprit e will be 140 + 30 * 1, which is 170. Finally, t he last t im e t he “ for” loop is execut ed, “ i” will be 2, which m akes t he x coordinat e of t he last sprit e 140 + 30 * 2 = 200. Aft er t hat , “ i” w ill be 3, and t he “ for” loop is exit ed. The y coordinat e is t he sam e all t he t im e, 25 pixels fr om t he bot t om of t he scr een. This m eans t hat t her e w ill be t hree sprit es draw n next t o each ot her down t he bot t om left of t he screen, next t o t he energy bar. When t he player loses one life, t he variable “ player Lives” becom es 2, and t he “ for ” loop will only be run t hrough 2 t im es, t hus only draw ing 2 sprit es. Great , huh? 7.5 Scoring I first int ended t o include t he scoring part in t he last sect ion, but decided t o put it here, in it s own sect ion. Of cour se we need t o add som e scores t o t he gam e. Ot herwise it w ould be unint erest ing t o shoot dow n t he enem ies. Ther e is a built - in variable called “ scor e” t hat w e will use for t his. That m akes t he score aut om at ically show up in t he Windows capt ion. 39

(40) Beginners Guide to Game Maker Programming First , we need t o set t he scor e t o 0 when t he gam e st art s. Open t he script called “ Gam eSt art ” . Add t he line score = 0; t o t hat script . We also need t o decide how m any scor es t he player should get for dest roying one enem y. I t hink we should give t he player 100 scor es for it . Add t he following line t o t he “ Gam eSt art ” script : global.enemy1Score = 100; Good. Now w e need t o add t hat scor e t o t he “ score” variable whenever t he player has dest roy ed an enem y. This happens in t wo script s. First , open up t he script called “ PlayerEnem y1Collision” . Add t he following line t o t he end of t hat script : score += global.enemy1Score; Then, open t he script called “ Bullet Enem yCollision” . Her e it is a lit t le bit t rickier, since w e hav e t o add t he score addit ion inside t he “ if” st at em ent here. Just t o m ake sure no m ist akes are m ade, I w ill print t he ent ire script her e as it will look aft er t he addit ion of t he “ scor e + = global.enem y1Score” : with (other) { // Lower enemy energy myEnergy -= global.bulletToEnemy1Damage; // Check if enemy energy is 0 or less if (myEnergy <= 0) { // If so, destroy enemy. instance_destroy(); // NEW!!! Add score to the player score += global.enemy1Score; } } // Destroy this bulle instance_destroy(); That would be it . Save t he gam e and t ry it out . 40

(41) Beginners Guide to Game Maker Programming 8 Final words 8.1 End of this guide This repr esent s t he end of t his guide on program m ing Gam e Mak er 4.0. I hope you hav e enj oy ed reading it and following t he inst ruct ions, but m ost I hope t hat y ou hav e learned som et hing from it . I t is possible t o m ak e really great gam es wit h Gam e Mak er, and I am looking forward t o see t he gam es you cr eat e. I f I find t he t im e and inspirat ion I will m ake a follow- up t o t his guide wher e I will delve int o a lit t le bit m ore advanced program m ing. Hopefully I will also be able t o expr ess a few t opics t hat were request ed for t his guide, for exam ple a good explanat ion of variable array s. This, I hope, will at least ser ve as a good ent rance t o t he w onderful w orld of program m ing. 8.2 Communication I f y ou have any quest ions at all, or j ust feel t he need t o read what ot her Gam e Mak er user s w rit e you ar e always w elcom e t o t he official Gam e Maker Com m unit y. The w eb address is: ht t p: / / pub58.ezboard.com / bgam em akercom m unit y My curr ent hom epage, w hich does not look t oo beaut iful but cont ains a few goodies, can be found at : ht t p: / / hem .passagen.se/ birchdale/ carl/ index_en.ht m l Please send m e an em ail if you find any er ror or such in t his docum ent and I will t ry t o correct it . My m ail address can be found below. 8.3 Useless statistics WOW! According t o Word, I hav e spent 897 m inut es on t his docum ent , writ ing 96859 charact ers and 17523 words. That is 107.98 charact ers per m inut e. And I hav e sav ed it 189 t im es. ☺ 8.4 Bye Finally I want t o, again, t hank all of you who have helped m e wit h t his docum ent and ot her Gam e Maker quest ions. Rem em ber t his supposedly Norw egian prov erb when m aking gam es: NOTHI NG I S I MPOSSI BLE EXCEPT SKI I NG THROUGH A REVOLVI NG DOOR Regards Carl Gust afsson Karlskrona, Sweden carl.gust afsson@hom e.se 41

(42)

Dokumen baru

Download (41 Halaman)
Gratis