Collecting objects
Programming Scratch Maze Games - Lesson 5
Objectives
- Understand and use broadcasts as event triggers
- To understand and use variables for different functions in game
Lesson Resources
Lesson 1 - Movement
Lesson 2 - Conditions
Lesson 3 - Debugging, variables and sound
Lesson 4 - Levels
Lesson 5 - Collecting objects
Lesson 6 - Evaluating the game
Introduction
Begin by recapping the last lesson.
- What did a broadcast do?
- How did we use them last week?
- What is a variable?
- Where have we used a variable so far?
Collecting objects
Another easy way to improve your game is adding objects for your character to collect. We’ll look at a couple of examples of this for our maze game. This is a good opportunity to challenge the students to do as much of this themselves as possible. It’s all applying things they have already done to a new context so should give you a good indicator of how well they have understood and remembered what they’ve done so far, and whose learning has gone to a greater depth.
Gaining lives
The first example is giving our player a way to collect extra lives, this could be really helpful if you’ve made level 2 really tricky with those extra baddies.
Let’s add a sprite that we can collect for extra lives, the heart sprite is perfect! Load it up from the library and set it to an appropriate size (about 20).

We want the heart to randomly appear in different places on the screen, staying at each place for about 3 seconds before moving again. We also want it to disappear when we collect it, make a sound (optional) and increase our lives by 1.
The heart moving around adds an element of luck for the player whether they are in the right place at the right time to collect it. Ask the students to talk to a partner about ideas they have for making this work. Can you reuse concepts we’ve already covered?
The random movement can be done in a very similar way to making the baddies move to random places, but instead of making the heart glide around the screen, we’ve used a ‘go to random position’ block so it jumps to new positions faster. The ‘wait’ dictates how long it stays in each position.

The second aim was to make the heart disappear, give us a life and make a noise. Here’s how.

Here’s all the code for the finished heart sprite:


The heart shows when the game starts and then, when touching the main character, hides, plays the ‘Magic Spell’ sound and adds one to the variable called Lives. You could repeat this with a duplicate of the heart (or two) that appears when level 2 begins.
Code for the level 2 hearts:

Collect objects to win
Another option for making collectable objects in your game is to adjust the purpose of the game, either in your existing levels or in another new level. In this example we’ve created a third level backdrop that once again has the same starting point and same colours as the other levels (so our previous code will still work). However, the layout of this level is quite different. Instead of one long continuous path this level has a number of different paths to choose from, and no end point.

The aim of this final level is not to get to the end, but instead collect a number of objects that are positioned around the maze.
First we’ll repeat the process we did for switching the backdrops to a new level. Touching the colour of the finish line on level 2 now switches the stage to the new Level 3 backdrop, sends the main character back to the start position (top left) and sends out a new broadcast message ‘start level 3’. The code previously there (‘well done’ etc) has been moved to the side for later.

As it’s the final level, we’re bringing all three baddies back for this level by using the new broadcast ‘start level 3’. All three of the baddies get this code.

Now it’s time to add the objects we need to collect, we’ve chosen a star.

Create a new variable that will be used to count the stars (you can call it points or something similar if you prefer).

The first part of the code for this sprite programs the sprite to hide when the game starts and also assigns 0 to the Stars variable and tells the variable display box on the stage to be hidden - there’s no point players seeing it until there are stars to collect (level 3).

The second part of the Star sprite’s code runs after receiving the broadcast to start level 3. The variable for Stars is set to 0, the variable display box is shown on the screen and the sprite itself is shown.

Then when the star and the main character sprite touch the Star sprite hides, makes a noise and adds 1 to the Stars variable.
Test the star works and then right-click the sprite thumbnail in the bottom right of the screen to duplicate it as many times as you want, then spread them round the maze. They will all work in the same way as the original star.

Finally, to make the game end when the stars are all collected, add one more ‘if then’ block to your main character’s ‘Forever’ loop. Take an ‘ = ’ block from the Operators category and put the ‘Stars’ variable name block in the first white space, type the number of stars (or objects) in your game in the second space, in the same way as we did with counting Lives in an earlier lesson. Then to finish things, put back the blocks we saved from earlier that play the winning music, show the ‘well done’ message and stop the game.

ENSURE THE STUDENTS SAVE THEIR WORK.
You can find a finished version of the example game shown in this plan HERE.

Plenary
Ask the students to talk to a partner about how have you used variables and broadcasts today. Ask them to feed back.
- What else could you do to your game?
- Can you think of any ideas based upon the things we have tried or ideas of your own?
Collect any ideas and then tell them that in the next lesson they will evaluating each others’ games and offering feedback to each other.
