47
SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Embed Size (px)

Citation preview

Page 1: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

SCRATCHLesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Page 2: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

REVIEW OF LESSON ONE

In Scratch Lesson One, the basics of the programming window were introduced, including the four parts of the programming environment – Command Center, Programming Window, Costume Design, and Display Window

Basic commands from four different command sets were explored – Control, Motion, Looks, and Pen

Simple animation and drawing techniques were demonstrated, and several exercises were provided, including the challenging Olympic Rings!

Page 3: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

OBJECTIVES OF LESSON TWO

In this lesson, three command groups will be explored – Sensing, Variables, and Operators

The goal is to create interactive programs that allow the user to input data while the program is running, and have that input shown or “echoed” in the program.

This requires the use of variables for storage of the input data, as well as operators to “join” the input with other statements to be shown to the display window.

Page 4: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

OBJECTIVES OF LESSON TWO

Let’s get started!

Page 5: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Launch the Scratch Programming Environment, and then get a Green Flag Hat from the Control Command Set to start a new program.

Page 6: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now click on the Sensing Command Set and get the ask and wait command

Page 7: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Also click on the answer check box to make it show on the display window.

Page 8: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

When you run the program, the cat asks the question shown, and waits, just like the command says.

An input box appears at the bottom of the screen for you to provide an answer.

Page 9: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

After you type in a response and press ENTER, your answer appears in the answer box at the top, and the input box disappears.

Nifty, huh?!

Page 10: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

The answer box is a storage location in memory, called a variable. It stores whatever is entered into the input box provided by the ask and wait command.

Page 11: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now let’s do something with this answer to continue the program. Let’s have the cat say “Hello” to you!

Page 12: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Go to the Looks Command Set, get the say for 2 seconds command and add it to your program.

Page 13: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

When you run the program, the cat indeed says “Hello!” after you type in your name.

Page 14: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

But wait…something is missing!

It didn’t include your name!

Page 15: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

To include your name in the “Hello!” message, something else is required.

You must create the message by joining your name with the word “Hello”.

Page 16: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Here’s how to do that…

Go to the Operators Command Set and get the join command.

Page 17: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

You probably notice something different about this object…it doesn’t have a “notch” like the other commands you’ve used before.

Page 18: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

That is because it is not a command, but a value object that can be placed into a command. Other names for values are arguments or parameters.

Page 19: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now, click and drag this value object (or parameter or argument) into the say command, replacing the “Hello!” in the white box.

Notice how the white box is outlined in white just before you drop it into place.

Page 20: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

When you run the program now, it says “Hello” to ….wait, something is still missing!

We forgot something! Your name, of course. Hmmm… can you figure it out?

Try to think of how to get your name to show up instead of the word “world”, before you go to the next slide.

Page 21: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

There are actually two ways to do this…

The easy way is to just click in the white box and change the word “world” to your name…that is called “hard-coding”, but not the best way.

Page 22: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

The other way is the one that makes the most sense. Hopefully it is the one you figured out.

Go back to the Sensing Command Set, and click and drag the answer variable to replace “world” in the second white box of the join command.

Page 23: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Finally, it works!

Congratulations! You have just completed your first interactive program!

Page 24: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now, let’s tweak this a bit.

There are two issues in this output. First, the word “hello” should be capitalized.

That’s easy to fix…just click inside the “hello” box and delete the “h” and replace it with “H”

Page 25: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

The next one is a bit more complex. There should be an exclamation mark after your name.

How can we make that happen?

Think about it…(Hint: use another join command).

Try it before you go to the next slide.

Page 26: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Here’s the fix…

Using still another join command, put the answer variable into the first box, and change the second box to contain the exclamation mark.

Then put the new join into the second box of the first join, creating a compound, or nested join statement!

Page 27: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now the program is just right!

Page 28: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

USING MORE THAN ONE VARIABLE

The next step in developing an interactive program is to do something fun, like a Madlibs game, a silly interactive story that asks several questions, and then puts the answers into a predefined story.

Here is an example: Give me a noun (answer might be “cat”) Give me an adjective (“brave”, perhaps) Give me an adverb (“stupidly”, maybe?) Give me an exclamation (how about “Ouch!”)

Page 29: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

USING MORE THAN ONE VARIABLE

The story could be something like this:

"Ouch!” he said stupidly as he jumped into his convertible cat and drove off with his brave wife.“

Now the question is…how can we make that work with Scratch?

The problem is that we must remember all of the answers before the story actually happens, then put all the answers into the story.

Luckily, there is a way…here’s how…

Page 30: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Above you see the four questions demonstrated on the previous screens.

This will work just fine, but each time a new question is asked, the old answer is destroyed! Not good!

Page 31: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

There must be a way to save the current answer somehow…

…using variables, of course!

Page 32: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

To make some variables to use, click on the Variables Command Set and make four variables called noun, adjective, adverb, and exclamation.

Page 33: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now get four copies of the set to command and change them as shown.

Page 34: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Then switch to the Sensing Command Set and drop the answer object into each variable…

Page 35: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

…ending up like this!

Page 36: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now move each set to command into its rightful place in between the ask and wait commands…

Page 37: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

…which gives you this!

Page 38: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now when you run the program, each variable stores a copy of answer so that all four answers are “remembered” at the end.

Page 39: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

When the program is complete, all of the variables contain the answers you entered, and now you are ready to output your story!

Page 40: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Now comes the painstaking process of putting together the story, one step at a time, starting with “Ouch, a joining of the “ symbol and the exclamation variable.

Page 41: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Here we join the “Ouch value object with “, he said and the adverb object and put that part of the output into a say command.

Page 42: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

And now we have the first line of the story complete!

Page 43: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

…and it works!Now, build the rest of the story in the same manner.

Page 44: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

Here are the next two commands to finish!

Page 45: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

…and here is the rest of the story…

Page 46: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

…and here is the rest of the story…

Page 47: SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables

NOW IT’S YOUR TURN!!!

Using the Madlibs example shown in this lesson as a guide, create your own Madlib, with at least five questions, and at least four output screens for your story.

You may use any other skills learned in the previous lesson, and are welcome to use different backgrounds and sprites for your project.

Have fun!