LEARNING SCRATCH (OR any other language, for that matter) is all about discovering and finding out new things. In the real world, you use your five senses to find out new things. In the Scratch world, you use a category of blocks called the sensing blocks.
In this adventure, you see how to use the sensing blocks to detect things such as typing on the keyboard, mouse movements, and more!
The Sensing Block Pallet contains 20 blocks, as shown in Figure 4-1. These blocks are colored light blue. There are 4 stack blocks, 5 Boolean blocks, and 11 reporter blocks.
Sensing blocks can be used for a number of different things. They can be used to keep track of how long things take within your program. They can be used to ask questions and then store the answers to be used within other blocks, and they can also be used to detect whether a key has been pressed on your keyboard or if there’s been movement of your mouse.
You can also use Sensing blocks to detect whether your sprite is touching something, how far away from another sprite it is, or where a sprite is on the Stage.
Sensing blocks tell your project what’s going on so that it can do things in response.
When you type words or numbers using your keyboard, that’s known as “text input” to Scratch. The ask () and wait
block, shown in Figure 4-2, is how you make a sprite ask for text input.
When you use an ask () and wait
block in your project, it causes a sprite to ask a question. A text input area appears at the bottom of the Stage, as shown in Figure 4-3.
When you’re done typing your answer, click the checkmark on the right of the text input or press Enter (or Return on a Mac) to submit your text input to Scratch.
When you submit input, the value from the input area gets put into another sensing block, the answer
block, which you can see in Figure 4-4.
The answer
block stores whatever value you put into the text input area. You can then use the answer
block to make different things happen. Follow these steps to create a simple chat bot program that uses the ask and wait
and answer
blocks to ask questions and store answers.
when green flag clicked
block from the Events Block Palette to the Scripts Area.ask () and wait
block from the Sensing Block Palette to the Scripts Area.ask () and wait
box to What’s your favorite programming language?if () then, else
block from the Control Block Palette and snap it to the bottom of the ask () and wait
block. Drag the () = ()
block from the Operators Block Palette and drop it into the space after if
in the if () then, else
block.
Your Scripts Area should now look like Figure 4-5.
Drag the answer
block from the Sensing Block Palette and drop it into the first space in the () = ()
block.
Notice that even though the () = ()
block’s open spaces are squares, you can still drop oval-shaped blocks into them.
say ()
block from the Looks Block Palette and drop it into the space after the then
in the if () then, else
block.say ()
block to That’s my favorite, too!say ()
block to the Scripts Area and drop it into the space after the else
in the if () then, else
block.Change the value of this say ()
block to I don’t know that one.
Your Scripts Area should now look like Figure 4-6.
Next, you make the chat bot more personable by having Scratch the Cat greet you. Follow these steps.
Drag an ask () and wait
block from the Sensing Block Palette and snap it to the bottom of the when green flag clicked
block.
Leave the value in this block set to What’s your name?
say () for () secs
block from the Looks Block Palette and snap it to the bottom of the block that asks for your name.join () ()
block from the Operators Block Palette and drop it into the say () for () secs
block. Drag an answer
block from the Sensing Block Palette and drop it into the second space in the join
block.
Your script should now look like Figure 4-7.
Now click the Green Flag and chat with Scratch!
Typing text is just one of the things you can do with the keyboard. If you’ve ever played a game on your computer that uses the keyboard, you know that you’re not always typing numbers or words in answer to questions.
The key () pressed
block detects when a key is pressed. With this block, you can make a space ship fire its rockets when you press the space bar, or turn left and right with the arrow keys.
Follow these steps to make a helicopter that moves up and down when you press the arrow keys.
Click the Choose Sprite from Library icon in the New Sprite toolbar at the top of the Sprites Pane.
The Sprite Library opens.
when green flag clicked
block from the Events Block Palette to the Scripts Area.forever
block from the Control Block Palette and snap it to the when green flag clicked
block.if () then
blocks from the Control Block Palette and place them inside the forever
block.key () pressed
blocks to the Scripts Area and drop them into each of the if () then
blocks.key () pressed
block to up arrow
.key () pressed
block to down arrow
.change y by ()
blocks from the Motion Block Palette and place them inside each of the if () then
blocks. Change the value of the second change y by ()
block from 10 to –10.
Your Scripts Area should now look like Figure 4-9.
Click the Green Flag and then press the up and down arrows on your keyboard. The helicopter goes up and down, depending on which key you press.
When you move or click your mouse, or use a touchpad, Scratch can sense that too by using the mouse down?
, mouse x
, and mouse y
blocks! Here’s how to change the helicopter script so that you can control the position of the helicopter using a mouse.
Start with the finished helicopter script, which is shown in Figure 4-10.
If your script doesn’t look like this, take a moment to make it match. Figure 4-10 also contains the answer to the previous challenge.
Drag the if () then, else
block so that it surrounds the if () then
block in the Scripts Area, as shown in Figure 4-11.
To make sure that it’s in the right place, position it so that it snaps inside of the forever
block.
if () then, else
block containing the arrow key commands from the then
part into the else
part, as shown in Figure 4-12.mouse down?
block from the Sensing Block Palette and drop it into the new if () then, else
block.go to x: () y: ()
block into the then
part of your if () then, else
block. Drop a mouse x
block from the Sensing Block Palette into the x: ()
part of the go to x: () y: ()
block and a mouse y
block into the y: ()
part.
The final program should look like Figure 4-13.
Click the Green Flag. Click anywhere on the Stage, and the helicopter will instantly move to that spot.
The timer
block keeps track of time. You can use the timer to make things happen in your scripts after a certain amount of time, or find out how many seconds it takes a user to do something.
One game you can build with the timer is a game where you guess the number of seconds that have happened since the Green Flag was clicked. To build this game, follow these steps.
when green flag clicked
block to the Scripts Area. Drag a reset timer
block from the Sensing Block Palette to the Scripts Area, and snap it to the bottom of the when green flag clicked
block.
This reset timer
block resets the timer to 0 when you click the Green Flag.
say ()
block from the Looks Block Palette and change the words to Press the space bar in between 5 and 10 seconds!forever
block from the Control Block Palette to the Scripts Area and snap it to the bottom of the say ()
block.if () then
block to the stage and snap it to the forever
block.key () pressed?
block into the hexagonal space in the if () then
block. Make sure that the value in the key () pressed
block is space.
At this point, your Scripts Area should look like Figure 4-14.
In the next part, you create the code that checks whether the space bar was pressed in between 5 and 10 seconds. Follow these steps.
if () then, else
block inside the if () then
block in the Scripts Area. Drag a () and ()
block from the Operators Block Palette to the Scripts Area.
Don’t snap this block into any other blocks at this point. The () and ()
block test two different things and produces TRUE if both of the tests produce TRUE.
Drag a () > ()
block into the first space in the () and ()
block.
This block checks whether the first value is greater than the second one and produces TRUE if so.
Drag a () < ()
block into the second space in the () and ()
block.
This block checks whether the first value is less than the second one and produces TRUE if so.
timer
block from the Sensing Blocks Palette into the first opening in the () > ()
block.() > ()
block to the number 5.timer
block from the Sensing Blocks Palette into the first opening of the () > ()
block.() < ()
block to 10.
Drag the completed () and ()
block into the hexagonal space in the if () then, else
block.
Your Scripts Area should now look like Figure 4-15.
The last part of the timer game is the part that tells the users whether they were successful in pressing the space bar at the right time. Follow these steps to finish the program!
say ()
block from the Looks Block Palette into the first section of the if () then, else
block, after the word then
.say ()
block to Good Job!say ()
block to the Scripts Area and snap it into the else
part of the if () then, else
block.Change the value of this say ()
block to Nope! Not quite right!
When finished, your Scripts Area should look like Figure 4-16.
Besides detecting input from the keyboard, mouse, and timer, the sprites in Scratch can also detect other sprites and colors on the stage by using the touching ()
block, the touching color ()
block, and the distance to ()
block.
One cool use for the touching color ()
block is to create a maze. Here’s how you can do it!
The maze we created is shown in Figure 4-18.
The next step in creating the maze game is to shrink the sprite to a size where she can fit through the maze and to add the code that moves the sprite around the screen when you press the arrow keys.
when green flag clicked
block from the Events Block Palette to the Scripts Area.forever
block from the Control Block Palette to the Scripts Area and snap it to the when green flag clicked
block.if () then
blocks from the Control Block Palette to inside of the forever
block.key () pressed?
block from the Sensing Block Palette to the hexagon shape in each of the four if () then
blocks.key () pressed?
blocks to the four arrow keys: up, down, left, and right (or any four keys of your choice).change x by ()
block into the if () then
block for the left arrow and set its value to –1.change x by ()
block into the if () then
block for the right arrow and change its value to 1.change y by ()
block into the if () then
block for the up arrow and change its value to 1. Drag a change y by ()
block into the if () then
block for the down arrow and set its value to –1.
You should now be able to click the Green Flag and move your sprite around with the arrow keys. Your Scripts Area should look similar to Figure 4-19.
Now you can move a sprite around the screen using the arrow keys, but it’s not really a maze because you can still go anywhere on the stage you want to go. Follow these steps to contain the sprite within the maze.
if () then
block and snap it inside of the if () then
block containing the key (up arrow) pressed?
block.touching color ()?
block from the Sensing Block Palette and drop it inside the hexagon shape in the new if () then
block.touching color ()
block to the same color that you used to draw the maze by clicking the color in the touching color ()
block and then clicking the wall of your maze.
change y by ()
block from the Motion Block Palette and snap it inside the if (touching color ()?)
block.change y by ()
block to –1.Repeat Steps 1 through 4 for each of the other three arrow keys.
Make sure that the up and down arrow keys have change y by ()
blocks and that the left and right arrow keys have change x by ()
blocks. Make sure all of your x: and y: values match the values in Figure 4-20.
Your finished script should look like Figure 4-20.
You should now be able to move the sprite through the maze. If it gets stuck, you may need to make the sprite smaller or make the maze bigger. To save your maze, give it a title and click File ⇒ Save.
In this project, you take the maze game that you created in the last section and make it into a game where you need to navigate a sprite through a maze to get to a final goal as quickly as possible.
To build the game, follow these steps:
Select the Apple sprite in the Sprite Pane.
A new, blank Scripts Area appears.
when green flag clicked
block from the Events Block Palette to the Scripts Area.say () for () secs
block from the Looks Block Palette to the Apple’s Scripts Area and snap it to the when green flag clicked
block.say () for () secs
block to How fast can you find me?say () for () secs
block from the Looks Block Palette to the Scripts Area and snap it to the first say () for () secs
block.say () for () secs
block and change the text to Set?say () for () secs
block and change the text to Go!reset timer
block from the Sensing Block Palette and snap it to the last say () for () secs
block.forever
block from the Control Block Palette and snap it to the bottom of the reset timer
block.if () then
block inside the forever
block.touching ()
block from the Sensing Block Palette into the hexagon shape in the if () then
block.touching ()
block to Sprite 1
(or the name of the Scratch the Cat sprite, if you’ve changed it). Drag a play sound ()
block from the Sound Block Palette into the if () then
block.
You can leave the sound set to the default “pop” sound or change it to another.
say () block
from the Looks Block Palette and snap it to the play sound ()
block.join () ()
block from the Operators Block Palette and snap it into the say ()
block.join () ()
block.join () ()
block. Drag a stop ()
block from the Control Block Palette and snap it to the bottom of the say ()
block.
When you’ve finished the script for the Apple sprite, it should look like Figure 4-22.
There’s one last thing you should do before this game is ready to go: Put Scratch the Cat back at the starting line every time the game is started. Follow these steps to make that happen!
Drag a go to x: () y: ()
block from the Motion Block Palette and snap it just under the when green flag clicked
block.
The coordinates of the current spot where Scratch the Cat is sitting on the stage are already filled in for you, so there’s no need to change them!
That will just about do it! Click the Green Flag to try playing the game!
The ultimate goal of people who create chat bot programs is to make the chat bot appear to be as human as possible. One way they test this is by using the Turing Test. Visit the link here: http://www.cnet.com/videos/what-is-the-turing-test/
to watch a video explaining the Turing Test.
Achievement Unlocked: Using Scratch’s A-maze-ing Sensing Blocks!
3.141.31.240