meet thirsty - string operator in scratch

7
Meet Thirsty: String Operator in Scratch In this post, we will learn to use the string and the list features in Scratch. In the spirit of Shall We Learn, I will show you in a project. This project is called I Am Thirsty. We will create a stick figure sprite as our main sprite; we will also a drink sprite. You can help this poor thirsty guy by moving the drink sprite to him with your mouse. Then we will create many drink sprite based on the first drink sprite. First, create a new project and create a new sprite. If you don’t know how to do this, I recommend reading at least the first four lessons of Scratch Programming available on this site. Name this sprite Thirsty. Here is mine. To demonstrate how to use the new string features in Scratch, let’s create a variable to hold the result string of adding all the names of the drinks he has consumed. Click the orange Variables button at the left panel and click Make a variable button. Enter “drink list” as the list name and keep this list visible for all sprites. Then click OK.

Upload: jessica-chiang

Post on 12-Nov-2014

335 views

Category:

Documents


0 download

DESCRIPTION

A chapter in a new Scratch Project E-Book in progress. To download the demo project, please go to my site: http://shallwelearn.com

TRANSCRIPT

Page 1: Meet Thirsty - String Operator in Scratch

Meet Thirsty: String Operator in ScratchIn this post, we will learn to use the string and the list features in Scratch. In the spirit of Shall We Learn, I will show you in a project. This project is called I Am Thirsty. We will create a stick figure sprite as our main sprite; we will also a drink sprite. You can help this poor thirsty guy by moving the drink sprite to him with your mouse. Then we will create many drink sprite based on the first drink sprite.

First, create a new project and create a new sprite. If you don’t know how to do this, I recommend reading at least the first four lessons of Scratch Programming available on this site.

Name this sprite Thirsty. Here is mine.

To demonstrate how to use the new string features in Scratch, let’s create a variable to hold the result string of adding all the names of the drinks he has consumed.

Click the orange Variables button at the left panel and click Make a variable button. Enter “drink list” as the list name and keep this list visible for all sprites. Then click OK.

Page 2: Meet Thirsty - String Operator in Scratch

Create a broadcast message called “consumed”. This broadcast message will be sent by the drink sprites later.

Create scripts as shown below. Thirsty sprite sets drink list variable to an empty string when the game starts, then it waits for the consumed broadcast message. Each time a consumed broadcast message is received, it says what’s in drink list variable for 2 seconds.

Let’s move on to the drink sprite. Create a sprite or get one from Scratch library, and rename it to “tea”.

Select tea sprite’s Scripts tab, and create a local variable named “name”.

Page 3: Meet Thirsty - String Operator in Scratch

Create the scripts as shown below. Note that you need to add a empty space before the “tea” so the result concatenated string looks right.

Page 4: Meet Thirsty - String Operator in Scratch

Everything should make sense, except the bracketed area shown below, that says “set drink list to join name drink list”. What on earth does this mean?

Right click on this join block to select the help menu.

I think it’s pretty clear. It shows that you can join two strings (“I am a” and “cat”), as well as one string and one variable (“good night” and thing variable). What it did not show here is that you can also join two variables, which is what we will do.

Page 5: Meet Thirsty - String Operator in Scratch

The tea sprite will keeps checking whether it’s touching Thirsty. If yes, then it appends to the drink list variable its name variable, which was set to “tea” when the program starts.

To create other drink sprites, simply make copy of the tea sprite and change 1) the costume, 2) the value of the name variable, and 3) the starting location. Below is the coffee sprite based on the tea sprite.

Page 6: Meet Thirsty - String Operator in Scratch

I have a total of four drinks and this is what my game looks like when it’s done. To play, drag a drink and bring it to Thirsty.

This is what the stage looks like after I’ve given him three drinks: water, coffee, and tea.