Levels
Programming Scratch Maze Games - Lesson 4
Objectives
- Understand and use broadcasts as event triggers
- To extend a video game by adding levels
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
Recap the last lesson. What is a variable? (a named value that can be remembered by a computer and referenced in code) How have we used one in our game? (to count lives) What is assigning when we’re talking about variables? (giving it a value) What made the variable value change in your games? (adding the change variable block to the conditions that detect collisions with the baddie (or walls, or both).
Adding levels
In this lesson we’ll look at how to extend the game by adding levels. In Scratch all this really means is controlling the Stage, and programming which backdrop is showing. This does, however, have knock on effects for the sprites in the game. We will need to code which sprites should be showing, and when and where they should be when levels start and finish.
The first step for creating a second level is to go back to the backdrop button and choose Paint.
Click the blue Convert to Bitmap button at the bottom of the screen.
Draw a second maze that uses the exact same colours as your first one. Also, start the path in the same place as your first level (top left in this example.). This saves work later as our code for touching the colour of the walls and sending the sprite to the start will still work, as the coordinate for the beginning of both levels will be roughly the same.
Getting the exact colours you used can be done with the colour picker tool when you go to choose a colour. Select your first background, choose the colour picker and select the background colour. Then fill in your second background.
Then return to background 1 and use the colour picker to match the colour of your path, then (with the brush tool on 100) draw a path on your second background and add another, thinner finish line that is a colour you haven’t used anywhere else in the game so far (see below).
Then name your two backgrounds ‘Level 1’ and ‘Level 2’ in the ‘Costume’ name box above the drawing tools.
Controlling which backdrop is showing at any time is easy. In the purple Looks blocks find ‘switch backdrop to...’ The drop down list provides the names of all your backdrops, plus a few other options.
We’re going to need two of these blocks, ask the students where they might go, when do we want the game to set the backdrop to either of our levels?
The first use is one we take for granted when we play a game. We need to code it to start the game on the correct level backdrop, this will reset things back to the start for anyone coming to play the game. So the first ‘switch backdrop to...’ block goes right at the top of our main character’s code (ensure it says ‘Level 1’).
The other time the background should change is obviously when we complete Level 1. We can use the finish line we made for Level 1, but change how it works. We don’t want the game to stop when we touch it now, so remove the blocks that do that and leave them at the side (we’ll use them again later). Replace them with a ‘switch backdrop to...’ block and make sure it names Level 2. Then add another ‘go to x y’ block to ensure the main character sprite jumps to the correct start position as Level 2 begins. Test it out to make sure the right backdrops show at the right times.
Ask the students What will the finish line on Level 2 do now? Will it trigger Level 3 to start? (if so, repeat this process for a third level and backdrop) Or will the game end there? (if so, add back in the blocks we put aside to give the ‘well done’ message and end the game.
Broadcasting
Next we’ll explore how to make different characters or objects appear on different levels. As going to a new level is effectively just switching the backdrop, then we also need a way to hide or show sprites at different times. This is done with a technique called broadcasting.
Begin by explaining broadcasts. What is a broadcast? Have you heard this word use anywhere else? TV, radio or social media broadcasts will probably be mentioned. This just means sending a message out, in radio, TV or social media it’s sending out a program or video across the airwaves or internet.
Broadcast messages in Scratch are basically trigger blocks that can make all sorts of things happen at a specific time. Any sprite or stage can send or receive a broadcast message.
Go to the Events blocks and the ‘broadcast’ block
Click on the name of the default message (message1) and create your own:
You can name the messages as anything you like, you can number them, or name with the thing they will trigger (which is advisable as if you end up with a few it’s easier to remember which one does what), for example:
Placing this block into your code will broadcast the message to all sprites and backdrops in your project at the point that that code runs. The second part to a broadcast is telling the other sprites or backdrops what to do when they receive the message.
Let’s look at an example that makes our baddie disappear when we switch to level 2.
Step 1 - Create and send the message.
The ‘broadcast’ block is added to the ‘if then’ block that detects when our main character has touched the finish line at the end of level 1. Three things now happen as a result of touching that colour:
- The broadcast message ‘start level 2’ is sent
- The backdrop switches to Level 2
- The sprite is sent to the coordinate for the start of the second maze.
Step 2 - Receiving the message and acting upon it.
The second part of this code is for the baddie sprite and requires a ‘when I receive’ block (find this in events, just above the broadcast blocks). The code attached to this block will be triggered to start immediately after the message has been sent. Here we are telling the baddie sprite to ‘hide’.
Whenever you hide a sprite you will also need a ‘show’ block somewhere else in your code to bring the sprite back when the game restarts otherwise it will be hidden forever!
Here, we’ve added it to the code that makes the baddie move, just under the ‘green flag’ block, but not inside the ‘forever’ block, otherwise the sprite won’t disappear when we want it to.
Adding more baddies with broadcasts
But levels generally get harder as games progress, so just removing our baddie doesn’t make sense. So we’re going to add some new, tougher baddies to level 2. This can all be done with the same broadcast message that we’ve already created.
Right-click your existing baddie sprite and duplicate it twice (or choose brand new sprites from the library if you prefer).
We now have ‘Gull’, ‘Gull2’ and ‘Gull3’.
To make it easier to tell them apart, we’re going to slightly recolour each baddie. Go back to the Costumes tab to do that and just use the fill tool to colour them in quickly.
Duplicating a sprite also duplicates the code for that sprite. That will save us some work, but also means we’ll need to make a few little adjustments to the code for our two new baddies.
The ‘hide’ and ‘show’ blocks are swapped around so when the game starts with a click of the ‘green flag’ the new baddie is hidden (we don’t want to see them on level 1).
The ‘show’ block is now moved and attached to ‘when I receive - start level 2’ so it will appear as soon as level 2 begins. The glides blocks to make it move also go here.
Then make exactly the same changes on the third baddie.
Note - if you have used the random blocks for the coordinates in the glide block you won’t need to change anything, as all three sprites will choose different random positions each time they move. However, if you just used a sequence of 4/5 glide blocks you will need to enter new coordinates for your new sprites, otherwise all three of them will go to the exact same places at the same time, and it will look like there’s only one baddie.
The final step to get the baddies working is to remember to add some more code to the main character sprite that detects the collisions with these new baddies. This is an exact copy of the code from the first baddie earlier and should sit inside the ‘forever’ block with all the other ‘if then’ statements. Touching any of the three baddies now makes a ‘squawk’ sound, sends the player back to the start and takes away 1 from the variable for lives.
ENSURE THE STUDENTS SAVE THEIR WORK AT THE END OF THE LESSON.
Plenary
Ask the students to think about their games so far with a partner.
- Does your game always restart on level 1?
- Does the backdrop switch to level 2 at the correct time?
- Does your hero character begin level 2 in the correct position?
- Are other characters or objects correctly hidden or showing on level 1 and 2?