Friday, July 10, 2015

CopyCat - A Simple Intro to User Input and Variables via Scratch

My child and I worked on CopyCat as a simple introduction to variables and user input in Scratch. Algebra is part of the Grade 4 Math curriculum in the USA, and this project could be a fun way to introduce the use of variables.


Design an interactive game in Scratch, where a CopyCat copies/repeats everything that you type in.

The Design Process:

  • Only a single sprite is required: the CopyCat. You can either choose from the list of sprites already available on Scratch, or draw your own. 

  • To provide user interaction in starting and stopping the game, we used the "green flag click" to start and the "space key click" to stop the game (both of which can be found under the section "Events" in the Scripts area in Scratch). You can choose any of the options that are available in "Events" to do the same.

Various sections in the Scripts area of Scratch

And now the fun part: CopyCat needs to copy everything that you type in.
How can we achieve this?

  • Under "Sensing" in the Scripts area in Scratch, you will find a block that asks for user input and waits for it. This is what we shall use, to ask the user to type in anything they like.

  • Once the user input is received, CopyCat needs to repeat it. But, how can CopyCat remember what the user typed in? Here is where the concept of variables comes into play. In the section "Sensing", you will find the variable "answer", which stores whatever the user typed in. 

  • I recommend selecting the box right next to "answer", so that it is visible on the screen and the kids can see how its value varies (hence the name variable), depending on the user input.

  • The CopyCat can now use this variable along with the "say" instruction (found in the "Looks" area of Scripts in Scratch), to repeat/copy whatever the user types in.

Ask the students to try writing the code upto this point:

  1. When "green flag clicked" (or other event), CopyCat asks the user to type in something.
  2. CopyCat repeats the user input, via the variable "answer". 
  3. When "space key clicked" (or other event), stop the program.

Let the students experiment with different values for the user input and observe how the variable changes accordingly. Once comfortable with the use of the variable, they can hide it by deselecting the box next to "answer".  It would be good to remind the students at this point, that this feature is helpful for debugging.

Here are three screen shots to demonstrate how the user input gets stored in the variable "answer".

  Asking for user input; variable is empty

User input entered; variable is empty till Return key is pressed

User input is now stored in the variable

  • The next step is to ensure that the game goes on forever, till the user clicks the spacebar. Discuss with the students as to what kind of a programming construct can be used to achieve this (the answer is a "forever loop", found in the "Control" section in the Scripts area). 

Once the above factors click into place, writing the final code should take minimal effort, as can be seen from our project.

To conclude, here's my inspiration for this project  :)

No comments:

Post a Comment