Explained in Code: Happy or Not

ScratchExplained in CodeNormal

This project explains how the Happy or Not terminals you see in shops work.


Create a new Scratch project

Create a new Scratch project and delete the cat sprite.


Draw button sprites

The terminals have 4 buttons on them:

  1. Very Happy (dark green & big smile)
  2. Happy (light green & small smile)
  3. Sad (light red & small pout)
  4. Very Sad (dark red & big pout)

Use the sprite editor to paint a sprite for each button and arrange them like the picture.


Create variables

We need a variable for each button that will store the amount of times it has been pushed. Create the following variables:

  1. very happy - this will store the amount of very happy
  2. happy - this will store the amount of happy
  3. sad - this will store the amount of sad
  4. very sad - this will store the amount of very sad

Once you have created them, arrange them on the screen like the picture.


When button pushed

Now we need to add one to each variable when it is clicked, add the following code to the 'very happy' button sprite:

when this sprite clicked change [very happy v] by (1)

Test your code by clicking on the 'very happy' button. You should see your 'very happy' variable go up by 1 each time.


Create wait variable and code wait

Often on a real terminal in a shop, you will see a young child quickly press the buttons. At the moment in your code if this happens your counts will go up very fast, how can we prevent recording all these extra clicks?

One way would be to only record a click after a certain number of seconds from the previous click, say 5 seconds. This would mean that even if someone pressed a button several times, it would only record one.

Create a variable called 'wait' and change your code to the following. This code does the following:

  • it checks if 'wait' is 0
  • if it is it records the click and set wait to 5
  • it count downs wait to 0

when this sprite clicked if < (wait) = (0) > then change [very happy v] by (1) set [wait v] to (5) end repeat until < (wait) < (1) > wait (1) seconds change [wait v] by (1) end


Copy the code to the other buttons

Test that the new code only let's you record a click every 5 seconds. If it working correctly then copy the code to the other buttons by dragging and dropping it.

You will need to change the following block to the correct variable for each button:

change [happy v] by (1)


Clear the variables

When we click the green flag we want to clear all the variables. Also there's a small bug in our code that can set the 'wait' variable to -1 if you click fast enough, so we need to put in some code to check for that and set 'wait' back to 0.

Add the following code to the backdrop:

when green flag clicked set [very happy v] to (0) set [happy v] to (0) set [sad v] to (0) set [very sad v] to (0) set [wait v] to (0) forever if < (wait) < (0) > then set [wait v] to (0) end end


Create a list, variable and add a button sprite

At the end of the day, the totals for all the buttons are recorded and then set to 0 ready for the next day. So let's try and code that!

Create a list called 'previous days' this will store the totals for each button at the end of the day along with the date.

We will also need to create a new variable called 'record', this will be used to build a string of text for the date and the totals.

And finally we add the Button2 sprite from the sprite library, we'll click this at the end of the day to add the record to the list and clear out the variables.


Create a cutom block

Click on the Button2 sprite in the sprite list.

Create your own custom block called 'add text', we're going to use this to add pieces of text together to build our record. The block should have 2 text inputs.

Add the following code to the Button2 sprite:

define add text (text) (text2) set [record v] to (join (record) (join (text) (text2)))


Record the totals at the end of the day

Now let's program the Button2 sprite. The following code will:

  • set the 'record' variable to blank
  • add the current date to 'record'
  • add the total for each button to 'record'
  • add the 'record' to the 'previous days' list
  • clear the variables storing the amounts

when this sprite clicked set [record v] to () add text (current [date v]) [/] add text (current [month v]) [/] add text (current [year v]) [ ] add text (join [very happy: ] (very happy)) [ ] add text (join [happy: ] (happy)) [ ] add text (join [sad: ] (sad)) [ ] add text (join [very sad: ] (very sad)) [ ] add (record) to [previous days v] set [very happy v] to (0) set [happy v] to (0) set [sad v] to (0) set [very sad v] to (0)


Try it out!

That's all the code finished!

Test your project:

  • Click on the green flag to set everything to 0
  • Click on the smiley face buttons. Do your counts go up? Does it wait 5 seconds each time?
  • When you click Button2 does it build the record correctly and add it to the list? (Note: make sure your list is visible!)

End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu