Sending and receiving messages in Scratch!

Learning Objectives

  • Understand the concept of separate objects in a program.
  • Understand the benefit of sending and recieving messages in code
  • Appreciate the advantage of reusing code.

Learning Outcomes

Must complete the bossy lion code so that an animal flies (Level 4)

Should complete the above, extending the program with little help.   Use broadcast to make scenery change, and a character change position. (Level 5)

Could Start to build a sophisticated interactive story incorporating elements of all lessons so far. (Level 6)

Keywords

Words to learn: broadcast,  receive, object,  reuse.

Starter

Choose a number, between 1 and 10.  Don’t tell anyone this number. Click here and you’ll see a series of instructions.  Your number will appear next to a command, e.g wave left.  If you hear the teacher read that command, you must do exactly as the instructions state.  Only do anything when you here the teacher read YOUR command!!

The commands the teacher could read are: Wave left, wave right, nod head, shake head, pat head and cross arms.

You will notice that only some of the class respond to some of the commands.  More people respond to others.

Imagine that your classroom is a computer system.  Your group is a complex program.  Each person is an object, or individual piece of the program.  Working together, you carry out the programmer’s (teacher’s instructions).

The teacher gives a command.  The instructions you were given form a very simple algorithm.  If the command given is the one you have code for,  you follow the program.

In short,  the teacher broadcasts the message “Pat head”.  If, when you receive the message you have the “code” for it, you follow the instructions. otherwise you ignore the instructions.

In programming this is a very powerful idea for two reasons.

  1. We can program different behaviour for different objects.  Think of a game – in call of duty (single player!!) you expect an enemy to behave very differently from an allied NPC.
  2. We can reuse code quickly and simply.  We only have to write the list of instructions once, and whenever the teacher says “pat head”, we know what to do…

Main

First you’ll need to download this Scratch project (right-click on the link and select “save as” ) to your H: drive.

So, now start up Scratch 2 and go to the File -> Open menu and find the file you just saved:

If this has been successful, you’ll see a cartoon lion and some other animals in the output window:

Run the program by clicking the green flag.  What happens?

In this file, there are (at the moment) 6 objects which can have their own code.  Each animal can be programmed, as can the stage (this is important later).  For now, only the lion and the frog have any code.

Look at the lion’s code.  He broadcasts a message (jump).  The frog has specific code (when I receive jump) so it is the only object to respond.

Challenges

  • Make the grasshopper jump as well.
  • Make the bird and the bat fly across the screen.
  • Make the lion give the instruction to Jump, Jump, fly, Jump.  (Here he is reusing the code!)
  • Add your own animal that can jump AND fly!
  • Screenshot your work and stick it in your book

Extension

  • Make the lion ask what to tell the animals to do. If you type “jump” the animals jump 3 times.  If you type “fly” the animals fly across the screen.  If you type anything else, the lion says he doesn’t understand.
  • Add some comments to your code, explaining how it works (right click on the workspace and select “Add comment”)
  • Screenshot your work and stick it in your book