image

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!

Learning the Sensing Blocks

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.

image

Figure 4-1 The sensing 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.

Getting and Using Text Input

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.

image

Figure 4-2 The ask () and wait block

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.

image

Figure 4-3 Scratch the Cat wants to know something.

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.

image

Figure 4-4 The answer block

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.

  1. Select New from the File menu in the Project Editor to create a new project.
  2. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  3. Drag the ask () and wait block from the Sensing Block Palette to the Scripts Area.
  4. Change the text in the ask () and wait box to What’s your favorite programming language?
  5. Drag an if () then, else block from the Control Block Palette and snap it to the bottom of the ask () and wait block.
  6. 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.

  7. 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.

  8. Click in the second block and type the word Scratch.
  9. Drag the say () block from the Looks Block Palette and drop it into the space after the then in the if () then, else block.
  10. Change the value in the say () block to That’s my favorite, too!
  11. Drag another say () block to the Scripts Area and drop it into the space after the else in the if () then, else block.
  12. Change the value of this say () block to I don’t know that one.

    Your Scripts Area should now look like Figure 4-6.

image

Figure 4-5 The () = () block inside of the if () then, else block

image

Figure 4-6 The beginning of a simple chat bot

Next, you make the chat bot more personable by having Scratch the Cat greet you. Follow these steps.

  1. 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?

  2. Drag a say () for () secs block from the Looks Block Palette and snap it to the bottom of the block that asks for your name.
  3. Drag a join () () block from the Operators Block Palette and drop it into the say () for () secs block.
  4. 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.

image

Figure 4-7 Adding a greeting to the chat bot

Now click the Green Flag and chat with Scratch!

Detecting Key Presses

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.

  1. Make a new project by selecting File  ⇒  New from the top menu.
  2. Click the Choose Sprite from Library icon in the New Sprite toolbar at the top of the Sprites Pane.

    The Sprite Library opens.

  3. Locate the Helicopter sprite in the transportation category, as shown in Figure 4-8.
  4. Select the Helicopter sprite and click OK to add it to the Sprites Pane.
  5. Select the Scratch the Cat sprite and remove it from the project by right-clicking it and selecting Delete, or by using the Delete tool from the top menu.
  6. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  7. Drag a forever block from the Control Block Palette and snap it to the when green flag clicked block.
  8. Drag two if () then blocks from the Control Block Palette and place them inside the forever block.
  9. Drag two key () pressed blocks to the Scripts Area and drop them into each of the if () then blocks.
  10. Change the value of the first key () pressed block to up arrow.
  11. Change the value of the second key () pressed block to down arrow.
  12. Drag two change y by () blocks from the Motion Block Palette and place them inside each of the if () then blocks.
  13. Change the value of the second change y by () block from 10 to –10.

    Your Scripts Area should now look like Figure 4-9.

image

Figure 4-8 The helicopter sprite

image

Figure 4-9 The up and down helicopter script

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.

Watching for Mouse Moves

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.

  1. 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.

  2. 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.

  3. Drag the if () then, else block containing the arrow key commands from the then part into the else part, as shown in Figure 4-12.
  4. Drag a mouse down? block from the Sensing Block Palette and drop it into the new if () then, else block.
  5. Drag a go to x: () y: () block into the then part of your if () then, else block.
  6. 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.

image

Figure 4-10 The finished helicopter script

image

Figure 4-11 Adding another if () then, else block

image

Figure 4-12 Moving the keyboard commands to the else section

image

Figure 4-13 The finished mouse-controlled helicopter script

Click the Green Flag. Click anywhere on the Stage, and the helicopter will instantly move to that spot.

Using Timing

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.

  1. Create a new project by selecting File  ⇒  New.
  2. Drag a when green flag clicked block to the Scripts Area.
  3. 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.

  4. Drag a say () block from the Looks Block Palette and change the words to Press the space bar in between 5 and 10 seconds!
  5. Drag a forever block from the Control Block Palette to the Scripts Area and snap it to the bottom of the say () block.
  6. Drag an if () then block to the stage and snap it to the forever block.
  7. Drag a key () pressed? block into the hexagonal space in the if () then block.
  8. Make sure that the value in the key () pressed block is space.

    At this point, your Scripts Area should look like Figure 4-14.

image

Figure 4-14 The first half of the timer game

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.

  1. Drag an if () then, else block inside the if () then block in the Scripts Area.
  2. 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.

  3. 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.

  4. 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.

  5. Drag the timer block from the Sensing Blocks Palette into the first opening in the () > () block.
  6. Change the number in the second part of the first () > () block to the number 5.
  7. Drag another timer block from the Sensing Blocks Palette into the first opening of the () > () block.
  8. Change the number in the second part of the second () < () block to 10.
  9. 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.

image

Figure 4-15 The timer game with the timer test in place

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!

  1. Drag a say () block from the Looks Block Palette into the first section of the if () then, else block, after the word then.
  2. Change the value of the say ()block to Good Job!
  3. Drag another say () block to the Scripts Area and snap it into the else part of the if () then, else block.
  4. Change the value of this say () block to Nope! Not quite right!

    When finished, your Scripts Area should look like Figure 4-16.

image

Figure 4-16 The finished timer guessing game

Sensing Touching and Distance

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!

  1. Select File  ⇒  New to create a new project.
  2. Click the Paint Brush icon in the Backdrop Pane to open the Paint Editor and create a new backdrop.
  3. Select any color from the Color Palette.
  4. Use the Rectangle tool to draw a large rectangle, as shown in Figure 4-17.
  5. Select the Line tool and use the same color to draw a maze inside the rectangle.
image

Figure 4-17 Draw a rectangle to start the maze backdrop

The maze we created is shown in Figure 4-18.

image

Figure 4-18 A maze backdrop

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.

  1. Click the Scripts tab to close the Paint Editor and Backdrop menu.
  2. Select the Shrink tool from the top menu and use it to shrink Scratch the Cat to a size where she’ll fit inside the maze.
  3. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  4. Drag a forever block from the Control Block Palette to the Scripts Area and snap it to the when green flag clicked block.
  5. Drag four if () then blocks from the Control Block Palette to inside of the forever block.
  6. Drag a key () pressed? block from the Sensing Block Palette to the hexagon shape in each of the four if () then blocks.
  7. Set the keys for the four key () pressed? blocks to the four arrow keys: up, down, left, and right (or any four keys of your choice).
  8. Drag a change x by () block into the if () then block for the left arrow and set its value to –1.
  9. Drag a change x by () block into the if () then block for the right arrow and change its value to 1.
  10. Drag a change y by () block into the if () then block for the up arrow and change its value to 1.
  11. 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.

image

Figure 4-19 The maze with the arrow key movement configured

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.

  1. Drag an if () then block and snap it inside of the if () then block containing the key (up arrow) pressed? block.
  2. Drag a touching color ()? block from the Sensing Block Palette and drop it inside the hexagon shape in the new if () then block.
  3. Set the color inside the 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.
  4. Drag a change y by () block from the Motion Block Palette and snap it inside the if (touching color ()?) block.
  5. Change the value in the change y by () block to –1.
  6. 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.

  7. Click and drag your sprite to the beginning of the maze and click the Green Flag.
image

Figure 4-20 A working maze app

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.

Building the Apple Patrol Game

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:

  1. Open the maze game in the Project Editor and select File  ⇒  Save as a Copy from the top menu.
  2. Rename the new project Apple Patrol.
  3. Click the Choose Sprite from Library icon in the New Sprite menu above the Sprite Pane to open the Sprite Library.
  4. Locate the Apple sprite and add it to your project.
  5. Position the Apple sprite at the end of your maze by dragging it within the stage.
  6. Use the Shrink tool in the top menu to make the Apple sprite fit between the walls of the maze, as shown in Figure 4-21.
  7. Select the Apple sprite in the Sprite Pane.

    A new, blank Scripts Area appears.

  8. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  9. Drag a 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.
  10. Change the text in the say () for () secs block to How fast can you find me?
  11. Drag another say () for () secs block from the Looks Block Palette to the Scripts Area and snap it to the first say () for () secs block.
  12. Change the text of this block to Ready?
  13. Add another say () for () secs block and change the text to Set?
  14. Add another say () for () secs block and change the text to Go!
  15. Drag a reset timer block from the Sensing Block Palette and snap it to the last say () for () secs block.
  16. Drag a forever block from the Control Block Palette and snap it to the bottom of the reset timer block.
  17. Snap an if () then block inside the forever block.
  18. Snap the touching () block from the Sensing Block Palette into the hexagon shape in the if () then block.
  19. Change the value in the touching () block to Sprite 1 (or the name of the Scratch the Cat sprite, if you’ve changed it).
  20. 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.

  21. Drag a say () block from the Looks Block Palette and snap it to the play sound () block.
  22. Drag a join () () block from the Operators Block Palette and snap it into the say () block.
  23. Type Seconds you took: into the first box in the join () () block.
  24. Drag a timer block from the Sensing Block Palette and drop it into the second box in the join () () block.
  25. 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.

image

Figure 4-21 Placing the apple

image

Figure 4-22 The Apple sprite’s script

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!

  1. Click Scratch the Cat in the Sprite Pane to view her Scripts Area.
  2. Use your mouse to drag Scratch the Cat across the stage to the place where you want her to start each game.
  3. 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!

Further Adventures in Coding

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.

image

Achievement Unlocked: Using Scratch’s A-maze-ing Sensing Blocks!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.240