Creating interactive mad libs in Scratch

Lead Image © kudryashka, 123RF.com

Story Time

We show how to create mad libs, by collecting user input and placing it in stories at predefined places.

Scratch provides the perfect environment to create and share interactive stories. I'll show you how to build an interactive story, or mad lib, that collects user input and places it into the story at predefined places.

I'll use the nursery rhyme Old MacDonald as the basis for the mad lib. The version of the nursery rhyme I'll use goes like this: "Old MacDonald had a farm, EIEIO. On his farm he had a dog, EIEIO. With a ruff, ruff here and a ruff, ruff there. Everywhere a ruff, ruff. Old MacDonald had a farm, EIEIO." Each verse uses a new animal and sound. The bold words will be replaced in the project.

Storytelling in Scratch is a great way to demonstrate event coordination and sequence. This project will rely on the use of variables to store dynamic information that is then displayed at the correct time in the story, including at the start of the project, when messages are broadcast, and when clicking sprites. From an educational perspective, this project asks words by parts of speech and type, providing some opportunity for you, as a Raspberry Pi Geek parent, to help your kids learn about language.

Create the Sprites

You need to get the words of the Old MacDonald nursery rhyme onto the Scratch stage, and the best way to do that is to use the built-in image editor to create sprites. Underneath the Stage, you can click on the paint new sprite icon next to the New sprite label to open the image editor (Figure 1).

Figure 1: Scratch's built-in image editor allows users to design their own sprites.

Select the text tool (the T icon) and then click into the image canvas to start typing. You can change the font type, size, and color. I'm not straying from the defaults, but feel free to apply as much design as you want.

A big consideration for this project is determining how to translate the lines of the nursery rhyme into sprites that can be manipulated on the Stage. It's unlikely that you'd get a good result by trying to type the whole rhyme into a single sprite because the line wrapping will be difficult to control.

You could create a sprite for each word, but that starts to get very tedious. I chose to use the substituted words as the break for my sprites. The words I'm replacing do not need to be created as a sprite, so my first sprite is "MacDonald had a farm." My second sprite is "EIEIO"; the third becomes "On his," and on it goes to the end.

I found that the approach of creating sprites from phrases of the story gave me the most control over positioning words on the Stage and provided more animation opportunities if I wanted to revise the story at a later time. I tried to create a sprite for each line, which seemed like the ideal compromise, but the image editor kept wrapping my lines in ways I didn't like.

My chosen approach creates 12 sprites, which are really 12 images created from text. After you create the sprites, you'll need to position them on the Stage by dragging and dropping the sprites in the order they appear in the story. You'll likely need to revise this spacing as you test the story, but don't forget to leave space for the substituted words.

Defining the Variables

The story is interactive in that it prompts the user for an adjective, a noun, a sound, and an animal. To store the word values as the user enters them, you need to create variables.

To create a variable in Scratch, click on the Data palette and then click on the Make a variable button. In the dialog box that pops up, give your variable a name, such as adjective. Variables can be either local (the For this sprite only radio button) or global (For all sprites).

After you create a variable, Scratch makes several additional blocks available in the Data palette that make it easy to work with the variables in your scripts.

This project requires the following variable names: adjective, adjective2, animal, noun, noun2, sound, sound2, sound3, and sound4. To understand why you need so many variables when you're only collecting four words, you need to know about reporter blocks in Scratch and how the project uses them.

Reporter blocks display the current value of the block it is watching. For each variable that you create, Scratch will create an oval-shaped reporter block. If you look at the reporter block in the Data palette after you create the variables, you will see that each has a checkbox. Clicking the checkbox displays the value in a Stage monitor on the Stage. As you'll see in the scripts here, you can show and hide the reporters with blocks in the script.

The Stage monitors can be positioned on the Stage, which means they can be positioned to fit into the story in the appropriate places. If you right-click on the Stage monitor, you'll find several display options, including normal readout, large readout, and slider. The large readout will work best in this script.

Scratch displays a variable in the Stage monitor one time; however, the story uses the words multiple times. Therefore, you need to create a number of variables so you can display a variable each time the word is used in the mad lib. The example in Listing 1 shows how to handle this problem using the multiple variables in the script that collects the user's words.

Listing 1: Two Scripts Assigned to the Cat Sprite

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content