Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose...

14
Making a Timer in Alice By Jenna Hayes under the direc8on of Professor Susan Rodger Duke University July 2008 www.cs.duke.edu/csed/alice/aliceInSchools

Transcript of Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose...

Page 1: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

MakingaTimerinAlice

ByJennaHayesunderthedirec8onofProfessorSusanRodger

DukeUniversityJuly2008

www.cs.duke.edu/csed/alice/aliceInSchools

Page 2: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

This tutorial will teach you how to make a 2mer in Alice. Timers can be very useful if you are interested in making 2med games and have many other uses.  

StartanewAliceworld,andaddatextobjecttothatworld.Whenitasksyouwhatyouwantthetextobjecttosay,typein0.0.

Nowinyourobjecttreerightclickon0.0andrenameit8mer.Let’sgetstartedcodingour8mer.

Step 1: Adding the Text Object

Page 3: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Clickon8merinyourobjecttree,andthengototheproper8estab.ClickonthecreatenewvariablebuRon.CreateaNumbervariablenamed8meLeS.Fornow,setitsvalueto0.

That 0 is just a placeholder. We will write code in the method editor so that we can enter in whatever value we want before we play the world.  

Step 2: Creating a Variable

Page 4: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Createaclass‐levelmethodfor8mercalledini8alize.Theonlycommandwe’llneedinthismethodisonethatsetsthevalueof8meLeS.Soclickon8meLeSanddragitintotheini8alizemethod.Setitsvalueto1fornow.

Step 3: Initialize Method

Page 5: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Nowcreateanumberparameterinini8alizecalledamountOfTime.Draganddropitoverthe1inyoursetvaluetocommand.Nowwecansetthenumbertoadifferentvalueevery8meweusea8mer,withouthavingtochangetheini8alizecode.

Nowdragyourini8alizemethodintoworld.myfirstmethodsothatithappensrightwhenyourworldstarts.SetamountOfTimetoanynumberyouwant.

Step 3: Initialize Method

Page 6: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Nowweneedtowriteamethodthatwilldecrementthe8meLeSvariable,andhaveourtextobjectdisplaythesecondsasthey8ckdown.Createanotherclass‐levelmethod,calledcountDown.DragaDoinorderinsidethemethod,andthendragaWhileloopinsidethat.

Step 4: Count Down Method

Page 7: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Clickonworldinyourobjecttreeandthenclickonthefunc8onstab.Findthea>bbuRonundermath.

DragthatbuRonoverthetruepartofyourWhileloop;chooseanyvalues,wearegoingtoreplacethem.Nowfind8meLeSinthe8mer’sproper8estab,anddragitovera.

Step 4: Count Down Method

Page 8: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

DropaDoinorderinsidetheWhileloop.Nowweneedtochangethetextofourtextobjectevery8me8meLeSchanges.Clickon8merintheobjecttreeandthenclickontheproper8estab.YoushouldseethetextbuRon.

ClickthatbuRonanddragitintotheDoinorderinsidetheWhileloop.Setittodefaultstringfornow.

Step 4: Count Down Method

Page 9: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Nowweneedtoturn8meLeSintoastring,sowecandisplayitwithourtextobject.Todothis,clickonworldandthenthefunc8onstab,andscrolldownun8lyouseewhatasastring.

Draganddropthatoverdefaultstring,andwhenthemenupopsup,selectexpressionsand8mer.8meLeS.

Setthedura8onofthiscommandto0secondssothatit’svalueissetinstantaneously.

Step 4: Count Down Method

Page 10: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Nowweneedtomakesurethatittakesexactlyonesecondbeforethevalueof8meLeSisreset.DragtheWaitcommand,whichislocatedunderyourmethodeditor,intoyourDoinorderinyourWhileloopandsetitto1second.

Step 4: Count Down Method

Page 11: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Clickon8merintheobjecttree,andthengototheproper8estab.Clickon8meLeSanddragitintoyourmethodeditorrightunderyourWaitcommand.Onthemenuthatpopsup,chosedecrement8mer.8meLeSby1.

Now,sothatthe8merisdecrementedinstantaneously,setthedura8onofthedecrementcommandto0seconds.

NowdragyourcountDownmethodintoworld.myfirstmethodunderyourini8alizemethodandplayyourworldtoseewhathappens.

Step 4: Count Down Method

Page 12: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

No2ce anything strange about your 2mer? No maAer how many seconds it starts with, it always stops at 1!  

Thisiswhy:LookatyourWhilestatement.Itwillonlyrepeatitselfif8meLeSisgreaterthanzeroatthebeginningofthestatement.When8meLeSgetsdowntozero,theWhilestatementstopsandthetextobjectisneverreset.SoweneedtoaddacommandAFTERyourWhilestatementsothe8mergoesallthewaydownto0.

Step 5: Finishing Up

Page 13: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

Goto8merontheobjecttreeandthengototheproper8estab.FindthetextbuRonanddragitintoyourcountDownmethodunderyourWhilestatement.Reproducethesame8mersettexttocommandthatyouhaveinsideyourWhilestatement.Yourfinalcodewilllooklikethis:

Nowplayyourworldagain,andobservethatsweetsweet8merac8on!

Step 5: Finishing Up

Page 14: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon

This8mercanbeveryusefulforgamesinwhichyouhavetobeattheclock.Your8merwillneedtoberuninaDoTogetherwiththeothercodeforyourgame,orasaseparateeventinyourgame.

Youcanalsousetheseconceptstocreateascorekeeper(seemyscorekeepertutorialformoreinforma8on).

Step 5: Finishing Up