Hour Of Code – Key Stage 2 – Diver Game Lesson using Scratch Online

I’ve created a lesson to help teachers do the hour of code with key stage 2 children. This is a one off lesson that should last an hour but could be easily extended to go beyond the hour. The idea is create a game that where the player controls the diver to collect jewels. The lesson is below.

Tell the children we are going to use the computers to create a simple computer game. This is because we are doing an hour of code. This is something lots of schools are doing around the country.
We are going to use a website called scratch to do this.
Techie: Requires Flash v10.2.
Show scratch on your screen (you may want to create a shortcut on the computers to )
If the children haven’t used scratch before very quickly show them the basic areas (if you need help there are lots of videos on YouTube, the scratch website and there is a tutorial in scratch itself).
Explain that we are going to create a game where a diver has to collect the lost jewels. However he needs to be aware of the nasty shark.
Step 1 – Show how to add a background – click on the new backdrop from library button and filter to the underwater theme. Any of the 3 underwater backgrounds can be used. Now let the children do this. Give them 1 minute!
Step 2 – Now we have a background lets add a diver. First we need to use the delete tool (scissors) to remove the cat. Show the children how to do this. Now the cat is gone show how to add a new sprite from the library and again filter to the underwater theme. There are 2 divers to choose from. Now let the children do this. Give them 1 minute again!
Step 3 – Lets give the diver some movement instructions. We are now going to code the diver so he can move up, down, left and right. Show the children that by clicking on the diver the screen on the right changes. This allows us to edit him. Click on the scripts tab at the top this is the coding area. Explain that coding in scratch is done by dragging and connecting blocks (like making a jigsaw). These blocks are split into 10 different categories. Talk Partners – How can we control the diver, what inputs could we use? Feedback and discuss, bring the conversation round to using the keyboard and the arrow keys. Explain that we can tell the diver what to do when a key is pressed by using the events blocks. Show how to add an event block for if key is pressed and then select the right arrow from the dropdown list. Explain we now need to add some blocks under the event block to make the diver do something. Show adding a move block from the motion group. Explain that we also need to make sure he moves in the correct direction so we need to add the point in direction block and set it to right (90). Now press the right arrow key and see what happens. Get the children to add this set of blocks 4 times and change the dropdowns to represent the correct directions and keys for the up, left, down and right arrows. (Hint they could right click on a block group and duplicate it).
Step 4 – Now we have coded the diver to move around the screen. Talk Partners – What happens when the diver moves? Try it on their screens. Feedback and discuss he sometimes swims upsidedown. Explain that we have just tested our code and we have found a problem this is called debugging and it’s a very important part of creating code. So let’s fix the problem (bug). Show how to add a rotation style block from the motion categories and set the dropdown to left-right. To make this work we need to add it under a when green flag clicked event block. These event blocks make things happen when the program is first run. Get the children to add this to their code. Give them 1 minute!
Step 5 – Now our diver moves around let’s give him something to collect. Show how to add a sprite by drawing it yourself (explain that this is a simple paint program). Give the children 5 minutes to create a jewel sprite.
Step 6 – The diver now has a jewel to collect. Let’s give he jewel some code to make it randomly appear in different places and when the diver collects it to give the player a score. To do this we need to give the game a memory. This is called a variable and it will keep the divers score. Show the children how to create a variable called score and how to set the score to ‘0’ when the green flag is clicked. Let the children do this now.
Step 7 – The game now has a memory so let’s code the jewel to add on a score every time the diver touches it. To do this we need to use a sensing block called touching. Show this to the children. Explain that to make it only happen when the diver touches the jewel we have to use a wait until block with the touching block added in. After the wait until block we can add a change score by 1 block from the variable options. However his will only make the jewel add up once. We need to use a control block called forever now to make the jewel repeat (loop) through the code forever. Put the wait and change blocks inside the forever loop and connect it under the set score block on the when green flag clicked event. Show this on your screen and now let the children do it.
Step 8 – The last thing we need to do with the jewel is make it randomly appear and disappear when touched to stop the score going up to quickly. To make the block randomly appear we can use a motion block called go to x: y:. Explain this will set the exact position the jewel appears. To make it random we need to use a pick random block from the operators section. Explain that the center of the screen is x: 0, y: 0 and the numbers go down or up from there. You can see the x and y values when you move the mouse around the screen. Show this to the children now. Ask the children What’s the lowest number, what’s the highest number as you move the mouse to the edge of the screen. Explain if we used these values the jewel may appear of the edge of the screen so let’s pick a random number between -200 and 200 for both x and y using the pick random block inside the go to block. This now needs adding into the forever loop above the wait until block. Get the children to do this now.
For Lower Key Stage 2 your hour of code may end here. However you could carry on and extend your hour by following the steps below. Upper Key stage 2 should be able to fit these steps into there hour.
Step 9 – Let’s check our work. We now have a diver that swims and when he collects (touches) a jewel he gets a score of one. Also the jewel moves every time it’s touched to stop the score from going up really quickly and to make the game a bit harder. So let’s add the shark. Add a new shark sprite from the library using the underwater theme. Now we have the shark let’s make him follow the diver. To do this we need to add a block called point towards and select the diver from the list. Now the shark will always point to the diver. To make it move towards the diver we now need to a move block. Finally we want this to happen all the time so we need to add a forever loop around the blocks and to make it start when the game starts we need to add a when green flag clicked block. Let the children do this now.
Step 10 – Now we have a shark that follows the diver however what happens with the shark now. Talk Partners. Feedback and discuss. Steer the discussion towards the fact that the shark moves very quickly and sits on top of the diver. Let’s fix this. First we need to slow the shark down from moving 10 steps to 2 steps. We can also add a wait 1 second block from the control section under the green flag block. This will give the player time to get started. Some children have also noticed that the shark swims upside down when going to the left. To fix this add a rotation style block before the wait and under the green flag blocks. Let the children do this now and test.
Step 11 – So now the shark moves a bit slower and the game is a bit easier. Let’s add some code to take 5 points away when the shark bites (touches) the diver. To do this we need to add an IF block. Explain that this carries out a test and if the test results in true it follows some extra code otherwise the code is ignored. From the control section drag in the IF Then block. Inside the If then block add a change score by block and change the number to -5. Finally add a touching block from the sensing section and it to the If block so it reads ‘If touching diver then’. Get the children to do this now.
Step 12 – What happens when the shark touches the diver. Talk Partners. Feedback and discuss. Steer the discussion towards the score goes down very quickly. Let’s fix this by pausing the shark. Instead of just using a wait command though let’s make him say something. Add a say for 2 secs block from the looks section and change the words to say ‘Yum Yum’. Now add this block below the change score block. Get the children to do this and test their games.
This should complete the hour of code for Upper Key Stage 2.
However you can go beyond the hour by adding a timer, power up options or sounds.

Look out for my Hour Of Code – Kodu Lesson coming soon.