How to use the
Learning Portal

Learning Portal

Our hands-on, comprehensive lesson plans span a range of levels. Browse our free STEM and coding learning resources.

Marty Image

Marty's Random Story Maker


This cross-curricular story making game is great for encouraging learners to branch out and get creative while coding. With a variety of random story settings and Marty moves, there are endless possibilities to write an epic Marty tale! Re-use the code over and over again for new Marty story inspiration each time!

This cross-curricular activity is all about using imagination! After coding random backdrops and Marty moves, it is time use these in a Marty story. Marty can playback the moves for a performance of the story and an on-screen sprite can even be coded to join in with the action by telling the tale!

what you will need

What will you learn about?

  • How to change and add backdrops on-screen
  • How to use Operators, Event and Control blocks
  • How to create and use lists and variables

extra information for educators

This activity can be linked to concepts covered in the lesson packs Programming Concepts 1 and 2.

Programming concepts 1

Programming concepts 2

It also offers the opportunity for cross-circular literacy work encouraging learners to get creative with an independent narrative skills task based on Marty's movement output from code written during this activity.

Generating a random scene

We are going to start by setting the scene for our story. We are going to choose a selection of backdrops. Once we have added these, we will write code to randomly pick one for our story!

Create a new Scratch 3 project with the Marty the Robot extension added.

Getting Started with Scratch 3

We want to add more backdrops of the stage so that we have lots to randomly choose from.

On the right hand side of the Scratch screen, there is the Stage with Scratch cat currently loaded onto it. We want to change the background that Scratch cat is currently on by hovering on the blue circle with an image logo that can be found in the bottom right of the screen. Then find the option that says Choose a Backdrop. Select another backdrop by clicking on it.

Repeat this until you have added ten backdrops. So that the plain white backdrop that was there already at the start of this session is not included in our random selection, remove it by clicking on the small cross in the top right corner as shown.

You should now have ten backdrops. Here are ours!

Click on Sprite1 then the Code tab in the top right to get ready to write your code!

Now that we have our selection of backdrops, we want to randomly pick one.

Select the when key pressed block from the Events section of the coding blocks. We set it to operate when the space bar key is pressed.

Next select the switch backdrop block from the Looks section of the coding blocks.

Instead of just changing the backdrop to backdrop1, we want the backdrop to be changed to any number between backdrop1 and backdrop10. We need a pick random block from the Operators section of the coding blocks. Set it to choose a number between 1 and 10.

Putting it all together gives the following code:

Before we go any further, we want to make sure that the original cat sprite (Sprite1) isn't visible! To do this you need to change the setting for the sprite so that it isn't showing.

Try your code out! Every time space is pressed, the backdrop should randomly change.

Generating random Marty moves

Now we have set the scene, we are going to turn our attention to Marty. We are going to choose a selection of possible moves that Marty could make during our story. Then we will write code to pick a number of moves randomly. Later we will use these moves as a basis for our story!

Tell Marty to get ready.

To make a new variable, select the Variables section in the code blocks drawer followed by the Make a Variable button. You will be asked what you want to call your variable. We called our variable "action" as it will represent what movement Marty makes each time.

However, our action variable is a numerical variable which means it can only take numbers as values not words so we will have to use a number to represent each movement we want Marty to be able to make.

In our example here, we are going to have 4 possible moves for Marty to make so we want to use the pick random block from the Operators section of the coding blocks again. But this time set it to choose a number between 1 and 4.

Set action to pick a random number between 1 and 4.

Hint: If you want to have more or less possible moves for Marty, then change the maximum number that can be picked from here. Remember you will need to write code for the correct number of moves for your code in the following sections!

Now we want to write code to instruct Marty what to do if number 1, 2, 3 or 4 is picked. Each number will correspond to a different movement that Marty could make.

Select an if then block from Control blocks in the code blocks drawer. This block checks to see if a condition is true (such as the number 1 being picked) and if so then the blocks held inside it will run. If the condition is false (such as the number 1 not being picked), the code inside the block will be ignored.

Next we need an equal to block from the Operators blocks. Set action to be equal to 1.

Put the equal to block inside the if then block.

Then decide on what move you want Marty to make if the action picked randomly is equal to 1. We went for a wiggle!

Repeat this process for all of the other numbers that could be randomly picked in turn.

Here is the code for the moves we chose for numbers 2, 3 and 4.

So far, we have just randomly picked one Marty move. It would be a very dull story if we stopped here! We want to pick more moves so select a repeat loop from the Control blocks and decide how many moves you would like Marty to make for your story. We have chosen 4 moves.

We are going to put the code that we have written so far for the Marty moves inside this loop.

We would like Marty to finish by standing up straight ready for any further adventures so add the stand straight block from the Marty the Robot blocks after the repeat loop at the end of the code.

Our complete code for making random Marty moves looks like this:

Hint: Remember that you can change the total number of random moves picked for Marty to make by changing the number of repeats; our code picks from all possible moves each repeat so it is possible that some moves will get picked more than once in a story!

Recording Marty moves in a list

Unless you have a super-duper memory, remembering the moves that Marty made and then using your brain cells to fire your imagination and write an amazing Marty story could cause overload! However, lists to the rescue and we will write code to make a note of the Marty moves... phew!

It would be helpful to keep track of the randomly generated moves that Marty makes so that we have it to refer to when we write our epic Marty story. We can use a list for this.

A list (sometimes called an array in other programming languages) can be used to store lots of pieces of information at once. We often use lists every day too... to-do lists, shopping lists, wish lists... information is stored so that it can be looked up and used when we need it.

A list consists of numbers paired with items. The number is just the position in the list. The item is the information stored at that position. If my wishes were "go to the Moon, bring dinosaurs back from extinction, learn to ride a unicycle and make friends with the Loch Ness monster", I could write them in list form as:

  1. Go to the Moon
  2. Bring dinosaurs back from extinction
  3. Learn to ride a unicycle
  4. Make friends with the Loch Ness Monster

My wish list has four pairs of numbers and items so the total length of my wish list is "4". We can look up information stored in lists. For example, in my wish list above the 3rd pair is the number "3" paired with the item "learn to ride a unicycle".

To make a new list, select the Variables section in the code blocks drawer followed by the Make a List button. You will be asked what you want to call your list. We called ours actions.

If we want to show or hide any list from the screen, we can use the show list or hide list blocks from the Variables code blocks.

When you have you code running, your list will fill up with data but we want to make sure that we start with an empty list.

To add data to a list, we need to use the add item to list block from the Variables block. We need one of these blocks for each possible move that Marty could make and we need to insert each block into the if then loop for each move.

For example, if action=1 then, as well as Marty doing a wiggle, we want to add the number “1” to our list of actions.

With the extra blocks added, our code looks like this:

Hint: Remember that if you have changed the number of possible move for Marty, then you will need to put an add to list block into all of your if then loops!

recording marty moves by description

We have stored the moves that Marty made in the actions list with a number corresponding to each possible move but it might be more helpful when we write our story if we have a word or few words to describe each movement stored in a list too.

Create another list to store the descriptions of each movement. We called ours What Marty did. Again, we want to make sure that we start with an empty list so we need a delete all block from the Variables code blocks.

Add this into the code after the delete all block for the actions list.

Then we need to go through each action in turn and add a description of each move. For example, we inserted an add block to put the description “wiggle” into the What Marty did list when action=1:

With the blocks for the descriptions and our earlier selecting a random backdrop code added, our code now looks like this:

write a marty story

Time to run the code. First your random backdrop will be picked. This is where your story will be set. Next generate your Marty moves. These are the actions, in order, that take place during your story. You need to think up imaginative ways to put them into your story. Think about what character Marty is going to be inspired by the backdrop maybe – is Marty still going to be a robot in your story, or will Marty take on a different character like superhero, pirate, astronaut…? What is the plot? When will the action be taking place - in the past, the future, the present? Your imagination is unlimited!!!

Playing back the marty moves

If you want to take your storytelling to the next level, you can code Marty to playback the random moves generated earlier and you can perform your story along with the moves. You could even make a costume for Marty!

We are not going to be adding any more blocks to the script we wrote earlier, all of the blocks we are going to use now will be part of a new script on screen.

Our code will go through each item stored in the actions list in turn and get Marty to make that move. Select the Variable section in the code blocks drawer followed by the Make a Variable button and create a new variable called "total actions". This variable keeps track of how far through the actions list Marty has got; it is the position number of an action in the actions list.

Select the when key pressed block from the Events section of the coding blocks. We set it to operate when the up arrow key is pressed. We want to make sure we have set it to use a different key to the key used to start our first script! Tell Marty to get ready. Set total actions to 0 (zero).

Now we want to write code to instruct Marty what to do if number 1, 2, 3 or 4 is on the list at each position. Each number will correspond to the different Marty movements that you decided on in the earlier part of this activity.

To get to position number 1 in the actions list, we need to increase the value of the total actions variable from zero by 1 using a change block from the Variables blocks.

We need an item block from the Operators block. This block reports on what a particular item in a list contains. We want it to report on the actions list. If total actions is equal to 1, it will report on the item at position number 1 of the actions list so it will return the number stored there.

Put this in an equal to block from the Operators blocks.

Select an if then block from Control blocks in the code blocks drawer again. If the item (action) is equal to 1 then we set Marty to wiggle in the first script so we want Marty to wiggle again and our section of code looks like this:

And so on for each of the different actions that Marty could make… if item equal to 2 then... if item equal to 3 then...

To get to positions 2, 3 and 4 in the actions list, we need to run this code again so will need to put it inside a repeat loop from the Control blocks. The length block from the Variables blocks reports on how many items a list contains. For example, our actions list contains 4 items so setting the code to repeat for the length of the actions list will repeat it 4 times in total.

Tell Marty to stand straight after the repeat loop.

Our complete code for making Marty playback to random moves now looks like this:

You might want Marty to pause between moves so that you can tell your tale. For this, we can use a blocks from the Sensing code drawer that checks to see if there has been a key pressed. Choose a different key to any chosen earlier!

Put the key pressed block into a wait until block from the Events block.

Finally, insert the wait until block into your code just after the change total actions by 1 block.

Challenge: adding a storytelling sprite

We have coded Marty to playback the randomly generated moves that we stored in a list and you have written your Marty story to go with the actions. We could add an on-screen sprite to tell the tale at the same time as Marty moves!

Your challenge is to add blocks into our routine so that each time a key is pressed Marty does a move and the sprite tells that bit of the story on-screen. Here are a few things you will need to think about,

  • How will you store your story? You will need to make another list!
  • Once you have typed in your story, how can you stop your script from getting very long? You could start two script running with the same event trigger!
  • How will you get the sprite to tell the story? You will need to use blocks from the Looks code drawer!

I need some help!

What Next?

You have written the code to give you the building blocks for a Marty story. Your code picks a random backdrop and random Marty moves. You have explored storing items in a list and looking up items in these lists to playback Marty’s moves and tell the story. If you want to keep crafting your epic Marty tales, here are a few ideas…!

  • Our existing code allows for the same move to be randomly chosen more than once! Can you write the code so a move cannot be picked more than once?
  • Fancy yourself as a filmmaker? You could experiment with recording your story along with all the Marty moves too!
  • Maybe your story could do with some dramatic audio effects... Explore the sounds available through the Sounds code blocks drawer and add code to make them play at just the right moment!