Debugging, variables and sound
Programming Scratch Maze Games - Lesson 3
Objectives
- To debug a program, explaining errors you find and how to fix them
- To understand and use variables
- To add appropriate sounds to a coding project
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
Before you start
Before the lesson download the Debugging Challenge Scratch file and paste it somewhere on your shared area so the students will be able to access it during the lesson. Keep a backup copy elsewhere on your network. To save the file open the link above and then click the download button in the top right of the screen.
Also print or copy one Debugging Challenge worksheet per child in your class (or as many as appropriate if you want students to complete the task in pairs).
Recap last week’s lesson.
- What did we add to the game last week?
- What is a condition?
- Which blocks did we use to allow us to add conditions to our game?
Rather than opening their work at the start of the lesson, ask them to instead open the Debugging Challenge Scratch file. Open Scratch first and then go to File > Open from your computer and browse for the file.
Explain that the file is a maze game like the one they have been making, but this one has a number of deliberate errors in it. Ask them to have a go at debugging the game by looking for and fixing the FOUR key errors in the game.
Give each student (or pair if appropriate) a Debugging Challenge worksheet and ask them to:
- look for the FOUR errors in the code,
- label and describe each one on their sheet,
- fix the error on the computer,
- explain their fix on their sheet.
The errors are as follows:
1. The Cat sprite won’t move as ‘if touching colour’ is the colour of the path instead of the walls. This means the condition is always being met at the start of the game so the sprite is constantly being sent to it’s starting coordinate, which means it cannot move. Change the blue to the background colour to fix it.
2. (Once moving) The Cat sprite moves way too fast due to the number 23 in the ‘move ... steps’ block. Change the ‘move’ block number to a lower number such as 3 or 4.
3. Touching the baddie does nothing because of the position of the second ‘if then’ block which is nested inside the first ‘if then’ block. Separate the two ‘if then’ blocks so they both work independently of each other.
4. The finish line does not work for the same reason as error 3 (nested ‘if then’ blocks). Separate all three ‘if then’ blocks so they all work independently of each other.
STUDENTS DO NOT NEED TO SAVE THE SCRATCH FILE AS THEY CLOSE IT.
Review everyone’s understanding of the errors and the required fixes before moving back to their own games.
Ask them to load up their game, then swap seats with the person next to them. Play each others’ games and report back any issues and successes at this point. Does the character move well? Do the walls and baddie work if you touch them? Is it too hard, too easy or just right?
Variables
The next feature we’ll add are lives, so your player cannot have unlimited attempts at making it through the maze.
To add lives we need to introduce a new concept called a variable.
Variables are a place in a computer’s memory that can store a value, think of it like a notepad that can hold some information for us to use again later.
We give each variable we use a name and can then refer to it at any point we like in our code to apply and use its current value. On the notepads above, the variable’s name is ‘Score’. The value of Score could change but it will still be the same variable, and someone could ask “What’s the score?” at any point, which could be checked and reported back. Or the value of the variable could dictate how or when things happen, for example, the game ends when the score gets to 30. All this (and much more) can be done with variables in code.
The information a variable holds can be different things such as numbers, text, punctuation, or a True/False value.
The example of algebra is another good one. Here, the name of the variable is Y, but what is its value?
15 + Y = 22
3 x Y = ?
21 - Y = ?
We can tell from the first calculation that its value must be 7 (as 15 + 7 = 22). Once we know the value of Y we can then apply it to other calculations, such as the other two above. We read the name of the variable but we always apply its value to where it's being used.
So in Scratch that might be something like this:
These blocks all have the variable Y instead of a number in them. So if Y still = 7, the sprite would move 7 steps, wait 7 seconds and then turn 7 degrees. We can also report the variable’s value to the user in a few ways such as a speech bubble or by having it visible on the stage.
But of course we can change the value of Y! This would alter what the code would do anywhere that we have used the variable’s name. So it saves us time of having to edit each number separately if we want to try out different numbers for moving, turning or if the variable needs to go up or down on its own such as if it represented a score in a game.
So when we use a variable, at the beginning of the code we always start by giving them an initial value. We call that assigning a variable. You can also assign a new value to the variable at any other point in the code.
Variables in Scratch need to be created. Go to the Variables section and click Make a variable, then name it ‘Lives’. You can, in theory, call them anything you like, but encourage students to name variables appropriately for the job they are going to do. Having a variable called ‘Bananas’ that is counting how many lives you have doesn’t make a lot of sense and will only confuse other people when they look at your code or play your game.
So we’ve named our variable (Lives). Now we should assign it a value for the beginning of the game. The ‘set (variable name) to’ block does this:
Ask the students; where should we put this in our code and what number should we assign to our variable?
The value for the variable Lives needs to be assigned right at the start of the game, so the block needs to go right at the top of our code. As soon as the Flag is clicked and the game begins the player will then receive that many lives.
Students can choose how many lives their player gets. They may find they want to come back and adjust this value later based on decisions about how the game will work, this is fine to do so.
Ask the students:
- So what will change the number of lives our character has?
- When should we lose a life?
- Should you be able to gain lives?
Typically, a collision with the baddie is a good reason to lose a life. If they want to make their game more challenging they could also lose a life if their sprite touches the walls, although they can balance this extra difficulty by assigning a higher number of lives at the start.
The ‘change Lives by’ block lets you adjust the variable’s value at any time in your code. This is usually done based upon conditions that dictate when or what makes the variable change.
So add this block inside one (or both) of the ‘if then’ blocks that detect the collisions with the baddie sprite and/or the walls:
Note the value is -1 in both examples so the value for our Lives variable will go down (losing a life).
You may have noticed that when a variable is created a display box is added to the Stage. This is moveable. You can drag it anywhere you like on the screen so it’s visible but not (visually) in the way of things (sprites are not affected by its position and will pass by it without any problems).
So now a variable is assigned a value at the start of the game and also is changed by conditions in the game. Ask the students to play their games, test the variable by losing lives on purpose. What strange thing happens when you keep losing lives?
They should notice that the value for Lives goes into negative numbers. Why is this surprising? Why does it happen? We’re conditioned to expect 0 lives to mean the end of the game as this always happens in games we play! You can’t normally have negative lives in a game. However, remember that Lives is just the name of a value that we’re asking the computer to remember and track. Even though we named it ‘Lives’ this has no meaning to the computer. If we want something to happen when the variable reaches a certain value then we need to write some code for that.
So the final step for using this variable is to measure it, and make it trigger the end of the game when the player’s lives get to 0. We need another condition, and this collection of blocks to do the job:
The green ‘=’ block is found in Operators and the small dark orange block with the name of the variable (Lives) is in the Variables section. It’s vital they use that block and don’t just type the name of the variable into the ‘=’ block, otherwise the code does not check the value of the variable.
It all goes together like this and should be added inside the Forever loop (but not inside any other ‘if then’ blocks) with the rest of the code on your main character sprite.
Test this all out to make sure everything is working properly. If it’s too hard and their character is dying too quickly, ask them to consider the best ways to change this (adjust the maze, character size etc as discussed above, or assign a higher number to the Lives variable at the start).
Adding music and sound
This is an optional extra for your games and may depend on whether you have working headphones for your computers. here’s no point adding great music and sounds to the game if they can’t be heard!
Scratch 3 has vastly improved the options for working with music and sound in a project (compared to previous versions). There are many built in sound effects which are great for video games, plus you can now edit sounds within Scratch more easily. You can also upload sound files you already have on your computer.
Sound can play a really important role in a video game. Most people will have a clear idea in their head when you ask what sort of sound should you hear when your character dies, or collects a power up? Sounds can give the player non-visual feedback that they are on the right track, that something bad has happened, or that they’ve been successful. However, too much, or the wrong sounds can spoil a game and add to the frustration a player feels and might make them want to stop playing a game.
Theme tunes and background music can also give a very strong identity to games, some famous examples have become nostalgic classics, such as the Super Mario Bros and Tetris themes.
So choose your sounds carefully and use them sparingly!
Sound effects
In this example we’ll start with three short sound effects.
- A noise when we collide with the baddie.
- A noise when the character loses all its lives and dies.
- A noise when the level is completed.
Sounds in Scratch are attached to Sprites. You need to add a sound to a Sprite before you can use it in your project. Go the Sounds tab (in the top left of your screen) for your main character.
Depending on the sprite you chose, you may see some sounds are already there. You can listen to the sounds with the Play button under the sound wave. There are also some great editing tools to add effects to the sound, which are particularly useful for changing voice clips.
In the bottom left of the screen you’ll see the add sound button, hover over it to see available options:
In all three examples here, we’ve chosen sounds from the Scratch library, they’re called ‘Squawk’, ‘Win’ and ‘Lose’. We’ve also deleted sounds we don’t want by hovering over the file in the list and clicking the cross or right-clicking them and choosing delete.
Back in the coding blocks, the Sounds category contains the following two blocks for playing your sound effects. The drop down menu will show any sounds you have loaded for this sprite:
‘Play sound until done’ forces the whole sound clip to complete before the code that follows can run. ‘Start sound’ will play the sound clip and allow the code that follows it to run at the same time. Both have their places and which one to use depends on the situation.
In this example we’ve used ‘Start sound’ on all three occasions where we’ve added them to our ‘if then’ blocks for the different conditions.
The first Start sound is used to ensure that while the sound plays, the ‘Say well done... for 3 seconds’ message appears at the same time. Ensure the time that the written message is displayed for is long enough for the sound to complete, as the next thing that happens (‘Stop all’) will cut off the sound if it’s still going.
In the second instance, ‘start sound’ is used to ensure that while the sound plays, the character returns to the start position and the life variable changes straight away.
In the third instance, ‘start sound’ is used to ensure that while the sound plays, the ‘GAME OVER’ message appears at the same time. Again, ensure the time that the written message is displayed for is long enough for the sound to complete, as the next thing that happens (‘Stop all’) will cut off the sound if it’s still going.
Music
If you want some background music for your game you will need a longer sound file. There are some slightly longer music loops in the Scratch library, but even repeating these can get pretty annoying after they’ve played a few times.
So a better option is to source your own music file to upload into the game.
There are a number of websites out there that offer royalty free music for use in education projects. With all these sites please exercise caution when using and searching with students. We haven’t found anything inappropriate when using them, but we cannot accept responsibility for any content on these sites. Search them in advance of the lesson and in some cases you may wish to download a selection of tracks from the sites yourself before the lesson. Save these to your shared area and ask the students to choose from them.
Purple Planet Sounds - www.purple-planet.com
Their free download collection is exactly that, just credit the website with the full URL somewhere in your work as the source of the music.
Ben Sounds - www.bensound.com
Their full collection is free to download, just credit the website with the full URL somewhere in your work as the source of the music.
In this example we’ve used a track called ‘Puzzle Game’ from the ‘Kids’ section on Purple Planet (www.purple-planet.com).
Click on the Download button next to the track.
On the next screen click Download in MP3.
This will usually download it to the Downloads folder in Chrome. If your students can’t access this you could download some tracks for them and copy and paste them to your shared area, or get them to open up another window at the side of the screen for their ‘Documents’ or ‘My Music’ folder and drag and drop the file from the bottom of the screen to the folder at the side.
Back in Scratch, go back to the sounds tab and choose Upload sound from the add sound button. Browse for the location that you saved the sound file and add it to your Scratch project.
On the Code tab, add a new thread of code that will just play that track. This time we are using ‘Play sound until done’ as we want the whole piece of music to complete before the ‘Forever’ loop makes it play again. As it’s in it’s own thread of code this won’t affect any of our other code such as conditions, sound effects etc.
Try out your sound effects and background music.
ENSURE THE STUDENTS SAVE THEIR WORK AT THE END OF THE LESSON.
Plenary
Review where the children are up to with their work, ask them to quickly play a partner’s game and offer any appropriate feedback.
Assess their understanding of the lesson with these questions:
- What is a variable? (a named value that can be remembered by a computer and referenced in code)
- How have we used one today? (to count lives)
- What is assigning when we’re talking about variables? (giving it a value)
- What did you assign to your variable of Lives? (a value of 3 or 5 for example)
- When did you do it? (At the very start of the game)
- What made the variable value change? (adding the change variable block to the conditions that detect collisions with the baddie (or walls, or both)
- What did our variable control? (when (or if) the GAME OVER message appears and the game ends.
In our next lesson we’ll explore how to extend our games by adding levels to them!