Creating interactive mad libs in Scratch

Asking for Input

The ask() and wait block in the Sensing palette is used to collect input from the keyboard. The input is stored in a built-in variable called answer. Listing 1 shows the scripts that generates the on-screen display shown in Figure 2.

Figure 2: The end result of the "ask() and wait" block.

The first script provides an introduction to the story using the say() block and then the broadcast (ask questions) message, which coordinates the script and ensures the questions are asked at just the right time.

The cat sprite receives the ask questions broadcast message and prompts the user for words via the ask() and wait block. This block takes a text string as its input and displays it as a speech bubble for the cat. Make sure the value you enter in the ask() and wait block gives the user adequate directions so you get the correct values.

When the ask() and wait block runs, it displays a text input box at the bottom of the Stage that allows the user to enter text. The and wait part of the block keeps the input box on the Stage and halts the script until the user presses Enter. Pressing Enter stores the value in the answer block and allows the script to proceed.

After each ask() and wait block, the script in Listing 1 assigns the value in the answer variable to the appropriate project variable via the set() to() block, which is located in the Data palette. Each time the ask() and wait block runs, the value in the answer block is replaced with the new user input, which is why the script assigns the user-entered values into the project variables right away.

The set() to block for sound2, sound3, and sound4, use a nested join() block to create the repeated animal sound that is used throughout the rhyme. The nested join allows the comma to be entered.

An Alternative Story Approach

I chose to display the mad lib on the screen using sprites for the words because it more closely mimicked what you would see in a Mad Lib book. Additionally, the story will remain on the screen until the user starts the program again or until you as a programmer create the next mad lib in the series.

A much simpler and less visually appealing way to display the mad lib would be to use the say() block and have the sprite "speak" the story via a speech bubble. You would string a bunch of say() blocks together and use the join() block to combine the static part of the story with the variables in a way similar to the script in Listing 1.

If the mad lib you create spans more than one screen, then using the say() block method might be the most manageable. A major tradeoff of this approach, however, is that you have to control the timing of the say() blocks, and it most likely will always be too fast or too slow for the user.

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