Skip to main content

Franky makes a decision



In the real life we are constantly making different decision based on different factors. For example, making a decision based on weather: if there will be raining I'll stay at home, otherwise I'll play outside. I guess that without any problems you could give some other examples making decisions based on various conditions.

As you can see, every of these examples rely on a piece of information that will let us figure out whether condition (is it raining?) is fulfilled or not. 

  • If there is a mess in my room I have to clean it up
  • If I am hungry I have a quick snack.
  • If I am sick I will stay in bed

Similarly in programming - to make our program smarter, we let it decide how to act if certain situations. In Scratch we have if ... then and if ... then ... else command blocks which can be translated to the humans speach as in following example:



To make our example way more colorful, lets move to the magical world of Franky the Mouse. I have provided you with a file containing a scene and Franky standing in the middle you can fetch from here.

First thing, after loading the scene make sure that we have selected Franky's sprite, since we are going to create a code for our mouse.


Here he is, standing in the front of signpost, Franky is getting back from his Grandmas house early this morning. Since he did not woke up entirely, he is wondering what he should do since in the week he goes to the school and during the weekend then he should go drectly to his home, where his parents are waiting for him. Fortunatelly, he has you to tell him whether it is Weekend or not, so that he can make a right decision.

First, we should allow us to answer Franky's question. Let's jump to the code editor and drag Following list of blocks to the code area of Franky:

Please note that x and y arguments of go to command comes from initial position of Franky's sprite. As a result we should get a result similar to one you can see in the picture below. Please note that everything entered to the input is available as a value of answer variable, which comes in a package with ask and wait command block.

You can enter response to the input box, but after hiting enter nothing is happening. This is because we did not said Frank what to do in certain cases. As a next step we add if-else command block which will let Franky to check our answer. If we will say yes then Franky will go home. Otherwise our small mouse will go directly to the school to meet his friends.

As you can see, the if statement contains places for commands to be executed when the condition is fulfilled or not. Let's move on and ask Franky to let us know about his decision. Please go th the Looks section of command toolbox and chose say ... for ... seconds. Results should looks like one below:

As a final Frankys action we should write a code that will let him go either to school of home after he is done with talking to us. Let's pick glide ... secs to command block from Motion category and put it directly after say command.
To shed little bit more light of what is going on in our code have a look at screenshot below:



Let's now click green flag and see what is going to happen. Franky will go to the school or home, depending on that he was told. As you did not make any mistake in the code, you should see similar result when answering yes to Franks question.


Hopefully you did have some fun with letting Franky make his choices. Stay tuned for a next bunch of scratchy news!


Comments