Scratch Programming
The Story Program

Program 1 - Story Program

Introduction

The story program is based on an old children's game called Mad Libs. In this game a person is asked to suggest a series of words. Those words are used to make a story.

Starting Off

Launch Scratch. Click on the cat sprite to select it and your screen will be like the screenshot. The red arrow shows where you will be creating your program, in the section labelled Scripts.

Scratch Program

In Scratch, we drag blocks from the left of the window into the section labelled Scripts. You will need to start with the blocks shown in the image below.

Scratch Program

These two blocks are Control blocks. They tell Scratch when to start and stop the script. This script is going to start when the Green Flag is clicked with the mouse and will stop when all of the commands have been executed.

Making Variables

The story in this example is going to be based on the following text,

Once there was a adjective boy called name.

He went to place to buy a object.

The four words in bold will be the variables in this program. Our program will replace the bold words with words entered by the user of our program. Our program has to store the words that the user enters. In programs, we use variables to store information that is not known before the program starts running.

In the top left of the window, click the Variables button to view the blocks for variables. Make the four variables as you see in the screenshot. The checkboxes to the left of each variable should not be checked.

When you make a variable, you choose whether it is going to be for the whole program (for all sprites) or just for the sprite you currently have selected. In this program, there is only one sprite so the choice doesn't make any difference to you.

Scratch Program

Asking The User To Enter Some Words

Our program needs to get some input from the user. There are lots of ways we can make programs respond to input from the mouse and the keyboard. When we want something like a word or a number to be entered, the best course of action is to use an Ask block. When we ask the user for information, we will need to store this information in the correct variable. The following screenshot shows two blocks which we can use to do this.

Scratch Program

The first block asks the question, the second block assigns the user's answer to the variable, in this case, adjective. You can test the program now if you like. If you show the adjective by checking the box next to it in the variables section, you can see that the word you enter is stored in that variable.

Create the blocks for the rest of the information you need from the user of your program. Your script should be changed to look like the following screenshot,

Scratch Program

Making & Displaying The Story

Start by telling the user that you have all of the information you need to make the story. I have used a think block to output this information in a thought bubble.

Scratch Program

Finally, you will need to add the blocks which tell the story. I have used say blocks with a number of seconds to indicate how long the message is to be on screen. You will be adding parts of sentences to your words. Don't forget to include spaces in the correct places or your story will look odd on the screen. The following screenshot shows the finished script for this program - at this stage, it's all you should have.

Scratch Program

Test Your Program

It is important to test your program properly. If you spot any mistakes, you need to fix them. The most common problem people get with this program is forgetting to put in the spaces or putting the variables in the wrong part of the sentence.

Analyse & Learn From Your Program

Although this is a simple program, it makes you use several important concepts of programming.

Input

Your program asks the user to enter some information and uses this information in the program. For programs to be useful, it is often necessary for the user to be able to enter information. You used an ask block to do this.

Variables

Your program uses variables to store information. The information stored in the variables changes depending on what the user chooses to enter. The word variable is used for this since the information stored varies depending on the user's input and the statements in the program. You created variables by clicking the variables button in the top left of the screen and clicking on the Make A Variable button.

Assignment

Assignment is the technique used to store the information that the user entered into the program. You stored values in your variables by using a set block to set the value of the variable to the answer whatever you wrote in your ask blocks.

Output

Your program uses think and say blocks to output information to the screen.

Operators

You used a join block to join the sentences of your story to the variables storing the user's words. The technique in programming where you add words and letters together to make longer strings of characters is called concatenation. This word has nothing to do with the fact that the sprite for this program was a cat.

Sequence

The last major programming concept that you have met is that of sequence. The blocks of your script are executed in the order in which you entered them. If you change the order of the blocks, things happen in a different order and your program may not work as intended or may not work at all.

Adapt Your Program

Now you know how to do this, you can make the program tell your own story. The best way to set this up is to write out your story in full before you start the program. For this program, our starting point was,

Once there was a adjective boy called name.

He went to place to buy a object.

We needed this information to be able to make the variables that we needed for the program. Write out your own story and adapt the program.

Extend Your Program

When you finish a small program like this, it's always worth exploring what you can do to extend the functionality of the program. Here are some ideas for this program,

New Story Button

At the moment, your program starts when you click the green flag. The script you wrote to make the story starts at the same time. You can change this.

Start by making your new story button. You can do this by clicking the first of the 3 buttons you see in the image. You will then be able to draw your button.

Scratch Program

In this sprite's script section, add the following blocks to start off,

Scratch Program

You need one more block, this should be a broadcast block which you will find in the Control section. You need to broadcast a new message, something like tell story would be good.

Go back to the script you wrote in the cat sprite to tell the story. Change the When green flag clicked block to a When I receive tell story block. Test your program.

Being Clever

You could find out if the user entered the name of a boy or a girl. You can adapt your program to use he or she depending on whether or not the name is of a male or female. To do this you will need to use an If...else block from the control section.

It is possible to find out if the first letter of a word stored in a variable is a vowel. In such cases, a becomes an. Again you would need to use an If...Else block as well as some of the blocks from the Operators collection.