Book Image

Scratch 2.0 Beginner's Guide: Second Edition

By : Michael Badger
Book Image

Scratch 2.0 Beginner's Guide: Second Edition

By: Michael Badger

Overview of this book

Table of Contents (18 chapters)
Scratch 2.0 Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating a talking head


In previous examples, we've seen how easy it is to import images from Scratch's built-in libraries or from files on our computer. Now, it's time to circle back and check out the webcam. This exercise will also use the PicoBoard's sound sensor to detect our voice.

To start, create a new project, and delete or hide the Scratch cat and then complete the following steps:

  1. With the stage selected, click on the Backgrounds (Backdrops in 2.0) tab, and then click on the camera icon. A new window is displayed and shows what the webcam sees.

    Tip

    On Scratch 2.0, an Adobe Flash Player settings' dialog box will prompt you to allow or deny access to the computer's camera and microphone. You must allow access in order to use the webcam.

  2. Look at the camera with your mouth closed, and click on the camera icon to import the image. Then open your mouth, and take another snapshot. Click on Done to close the dialog box. We now have two background images.

  3. Name the first imported image silent and the second one talking.

  4. Now let's script our snapshots. Click back to the Scripts tab, and start with a when flag clicked block.

  5. Add a switch to background () block, and select the silent background. This block is called switch backdrop to ().

  6. Now we'll need to continually evaluate whether or not the detected sound sensor value is greater than 15.

  7. Add a forever block followed by an if (), else block from the Controls palette. Add the greater-than block to the condition value. From the Sensing palette, add the () sensor value block, and select the sound sensor. Enter 15 for the block's second value.

    Tip

    If you are completing this exercise without the PicoBoard, substitute the loudness block for () sensor value to use the computer's built-in microphone.

  8. If the sound is greater than 15, we want to alternate between the two backgrounds. Add a switch background to (talking) block, followed by a wait (.1) secs block, which is then followed by a switch background to (silent) block.

  9. As the else condition, add a switch backdrop to (silent) block.

  10. Click on the green flag and talk. Feel free to experiment with the detected loudness value and the wait value of the script to see what effects you end up with.

  11. Let's make one more enhancement to this script to give our animation the feel of a 1980s television station that went off air. Add the following block to the if condition: set (ghost) effect to ((100) – ((sound) sensor value). Then to the else condition, add this block: set (ghost) effect to (100). Depending on how loudly you talk, you might want to experiment with multiplying the (sound) sensor value by some number, such as five. Now the image will fade in and out based on the loudness. The following screenshot shows our script:

What just happened?

By combining the backgrounds that we saved from the computer's webcam and the detected sound levels, we very quickly and easily integrated external data into our Scratch project. The fact that our talking head looks a little goofy makes it fun. It reminds me of translated movies where the English translation is dubbed in over the original movie. The sound never quite matches the mouth movements, but it is often close enough to get the point across.

By adjusting the ghosting effect, we took some of the emphasis off the timing of the script. The visual strength of the image depends on the strength (loudness) of the detected sound. With the sound sensor on the PicoBoard, a higher sound value corresponds to a louder noise. The sensor will report values from 0 to 100 like the loudness block.

When we apply the ghosting affect while speaking, we calculate the new ghost value by subtracting the sound value from 100. You'll need to experiment with your sounds, but if the sensor measures a sound value of 20, then the ghost value becomes 80. This ensures our image is visible but not in full strength. Remember that with ghost, 0 is fully visible and 100 is transparent.

Have a go hero – using sound to move a sprite forward and backward

The talking head makes a great way to introduce Scratch programming in the physical world because it's quirky and generally makes people chuckle. It's really well suited for an audience such as workshops or presentations. I've seen this project done with animated lips, and I've used it in the past to strobe two colors based on the sound.

You could also use the sound value in other projects to change a sprite's costume, to move a sprite, or to apply a graphical effect. Explore the sound values some more by writing a script that moves a sprite forward and backward based on the detected loudness of the sound.

Sharing Scratch 1.4 projects online

If you create a project in Scratch 1.4, you can still share that project to the new Scratch 2.0 website. Other people will be able to view your project on the website. The Scratch 2.0 website will not export a 1.4 file, and as soon as you edit the older file online, it will be saved as a new Scratch 2.0 format.

Tip

You can tell a Scratch 1.4 file by its file extension; they end in .sb while Scratch 2.0 projects have a .sb2 extension.

Sensing the environment with the PicoBoard

Each of the PicoBoard's sensors is described in the following table:

Sensor

Values

Description

sound

0 to 100

Captures the sound from via the board's built-in microphone. A quiet noise will register a lower value than a louder noise.

light

0 to 100

A higher value means that the sensor is detecting more light. As you block light from reaching the sensor, it reports lower values.

button

true or false

Clicking on the button causes the sensor to register a true value, and it will continue to register a true value for as long as the button is pressed.

slider

0 to 100

Move the slider to select a value. Could be used to provide numeric input, such as setting the size of a sprite to the selected value.

resistance

0 to 100

The board includes four analog ports (A, B, C, D) with alligator clips to measure the resistance in a circuit.