Wednesday, May 18, 2011

Programming Concepts in Scratch

On Monday I started my lessons on Scratch for my Introduction to Computers for Arts and Social Sciences class.  My goal is for students to understand boolean values and if statements, loops, variables, and the broadcast mechanism.

These are my notes and slides.  They assume a basic knowledge of the layout of Scratch and how the scripts work.  You are welcome to use them if you would like.

Boolean Values

Anything inside a Scratch block with the angled ends is a boolean value.  It represents something that, in the end, will be TRUE or FALSE.  You can think of it as asking a question and answering either yes (TRUE) or no (FALSE).


Some examples in Scratch are the 'mouse down?' and '□ < □' blocks.

Boolean values can be combined or modified with and, or, and not blocks.


For the final boolean value of the and block to be TRUE, both the boolean values inside must be TRUE.  If either (or both) is (are) false, then the whole thing is false.

For the or block to be TRUE, at least one of the two boolean values must be TRUE, but one can be FALSE.  If both are FALSE then the whole thing is false.

The not block basically just gives the opposite value.  If the boolean inside is TRUE, the result will be FALSE with not, and vice versa.


If Statements

The if blocks have little slots that are perfectly shaped for a boolean value.


The if block will check whether this boolean value is TRUE or FALSE.  If it is TRUE, then the code tucked inside will run.  Otherwise, it will not.  In the example above, the 'meow' will play because 5 is indeed less than 6, but if we swapped the 5 and 6 then it would not play.

One type of if block also includes an else.  In these blocks, the first section of code will run when the boolean is TRUE, but the second section of code will run if it is false.


Loops

When a loop starts, its code (shown in the image below as a star) will be run over and over until some kind of stopping condition is met.


The simplest kind of loop in Scratch is the forever loop.  In this case, there actually is no stopping condition - the code inside will continue to run until the program tells the script to stop running (such as when the red stop sign is pressed on the stage).



Another kind of loop is the forever if.  This loop also goes on forever like the forever loop, but the code inside will only run when the boolean value is TRUE.  The loop will check every single time whether the boolean is TRUE, and it can be different each time.  The loop will still keep going.  In the example below, the meow will play, but only if the space bar is being held down.


A repeat loop allows you to say exactly how many times it will run.  The stopping condition occurs when the code has run that many times.


The repeat until loop continues until the boolean value is TRUE.  As soon as it is, the loop stops for good.  It is possible to have the loop start and the boolean value to be TRUE right away.  In this case, the code inside will never actually run even once.



Variables

Imagine you have a box that you label.  You can put one thing in that box at a time, and check what's inside any time you like.  That box is your variable.


In the example above, we named our variable myObject, which is like labelling our box with "myObject."  To put something in the box, we use the set block.  In this example, we're going to put the word earth into the box.  We can then hide or show the variable in the Scratch stage if we want to.


Now we can check what's inside the box at any given time by accessing the variable.  In the example below, we play a particular sound based on what's inside.  Because we still have earth in the box, the meow will play, but the note will not.


If we want to, we can change what's inside the box by setting the variable again.  When we do that, whatever used to be in the box is essentially tossed out or even destroyed - it is forgotten forever.


Broadcast

In Scratch you can broadcast a message any time you like.  It's like shouting out to the world, "hey! something happened!"  You can name your message anything you like, but it's a good idea to keep it meaningful so you remember how you intended to use it.


Other scripts, both within the same sprite that shouts the message and in other sprites altogether, can listen for that particular message and act upon it.  They do this with the 'when I receive' block, specifying which message they care about.  More than one script can receive the message; in this case, both scripts will start running at exactly the same time.


If a script doesn't really care about that particular message, that's fine - it can just ignore it.  No harm is done.


One useful way a broadcast can be used is in a game.  If the main character is keeping track of his own health, he might broadcast a message saying he's dead so the other sprites can react accordingly.  Perhaps they will stop moving and a game over message will display.


Conclusion

To get a feel for all these ideas, practice is required.  Try little code blocks and experiment by changing them and seeing what happens as a result.  Scratch is super easy to manipulate and you never have to be afraid of breaking anything.

There is a great tutorial and example game by David Malan of Harvard University.  I recommend working through it.

2 comments:

Anonymous said...

That's interesting- while I have no idea what Scratch is, I learned the same exact concepts you explained for boolean values and if statements in geometry class!

Bob I said...

Great post! I teach Scratch to 6th graders, and I chose it for the very reason that it is a fun and relatively painless way to teach programming concepts.

Post a Comment

Comments are moderated - please be patient while I approve yours.

Note: Only a member of this blog may post a comment.