Project 2 – Snake part 2
Building Retro Games: Pick a project
Objectives
- Understand and use sequence, selection, and repetition in programs
- Use X and Y coordinates effectively to control a sprite’s movement
- Understand and use variables to control functions in a game
- Plan ways to add to and improve a program
Project 1 - Pong part 1
Project 1 - Pong part 2
Project 2 - Snake part 1
Project 2 - Snake part 2
Project 3 - Space Invaders part 1
Project 3 - Space Invaders part 2
Introduction
Recap last lesson and what we added to our game so far. What was working?
- The snake was moving (all the time)
- We could control which direction it moved in with the arrow keys
- We left a trail behind the head that was the snake’s body
What still needs to be done?
- Scoring points while you survive in the game
- Losing when the snake crashes into itself or the edge of the screen
The next steps are adding scoring to the game and a way to lose.
Scoring
Let’s start with scoring as that will lead into the other features that we still need to add.
For the scores we will need to use a variable. Students should have come across these before in other coding work, such as lesson 3 in our Scratch Maze Games unit of work.
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 give them a value to start with. 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 ‘Red’ (or whatever colour you used for your first bat).
We need to assign a value to the variable at the start of the game (give it a value). Most games start with 0 points, so we’ll do that too! Use the ‘set Score to’ block to do this and add it to the top of the script. Make sure the variable name is set to Score in the drop down menu.
In this game we score points just by surviving, so we should basically be scoring points whenever the snake is moving. How could we do this with the blocks we have available in the variables category?
‘Change Score by’ will do this job. To make it keep adding points it needs to be inside the ‘forever’ loop like this:
Try it out; the points should race up while the snake is moving around the screen now.
Losing
But we still can’t lose. Let’s fix that!
We need the game to end when the snake’s tongue hits its body or the edge of the screen. This will involve using another condition (if/then) block. Ask the students to have a go at making it work.
Here’s one way that covers both options with one condition, by using the option of an ‘or’ operator.
It measures to see if red (the colour or the snake’s tongue) is touching green (the colour of its body - make sure these colours are precise by using the colour picker tool) or the edge of the screen (‘edge’ is an option in the drop down box, the block initially has ‘mouse pointer’ or the name of other sprites in it, if you have any).
If either of those conditions are met we get a ‘GAME OVER’ message via a speech bubble, followed by a second one that reports the value of the score variable at that time, (the only way to insert the value of a variable into a sentence is by using the ‘join’ blocks) and then all scripts are stopped to end the game.
Sound effects
There’s a lot more we could add, but to finish off this simple version of the game you could add a few sound effects (if you have headphones to hear them!).
Two suggested places to put an effect are when the game starts and when the player loses the game. Have a look in the library of sounds by going to the Sounds tab for the snake sprite and clicking the ‘Choose a sound’ button in the bottom left of the screen.
There are lots to choose from, but encourage the children to keep them short and simple so people playing the game don’t get annoyed with them.
Here’s our finished script for the snake:
Ask the students to save their work again at this point.
Extending the game
The next steps are to give your students time and opportunity to work in pairs, small groups or independently to extend their game. The next one or two lessons should be spent on this.
Ask the class; what else could you add to this game?
This is where the students need to do some thinking. Ask them to get into groups of 4-6 and come up with ideas for ways to change, adapt or add new features to the game.
Here’s a few examples if you need them:
- Add apples that randomly appear in a different place every 5 seconds. Collect apples to erase the snake’s body line and survive longer.
- Change it so crashing doesn’t end the game but instead loses a life, takes away 100 points, clears the screen and carries on. The player gets 3 lives, then ‘game over’.
- Add ‘levels’ by making the snake get faster as your score goes up. Each time you hit a score target it could clear the screen and then speed things up. variable needed for snake speed.
- Add bonus items that randomly appear and disappear and can be collected for extra points.
- Add bad apples that randomly appear and disappear and take away your points if you crash into them.
You could generate a class list of ideas if wanted and then ask them children to pick one feature at a time. They can work on it with a partner, in a team or on their own to try and get it to work.
Give them each a copy of our planning and evaluation sheet and ask them to complete just the top ‘planning’ section once they have some ideas.
Encourage them to write out their pseudocode first (what and when they want it to happen in their own words), then try and work out how to achieve it using things they’ve learnt about.
Here’s a few blocks (in no particular order) that might be useful for some of the ideas above. Please note, that these are not full solutions but might help nudge your students forward if they’re struggling on their own. Don’t show them unless needed though, we want to encourage their independent thinking and problem solving at this point.
We’d love to see some of your finished games and what the students have added to them. If they’re saved to an online Scratch account send us a link to their project at teachictnt@ntlp.org.uk or Tweet it to @teachict_nt. Or send us a short video of them explaining their creations.
Plenary
Review what we’ve added in this lesson:
- Variables
- Scoring
- Losing
What are their ideas for taking the game further? Have you had any ideas already for how to make it work?
Evaluations
At the end of the project give the students time to revisit their Planning and Evaluation sheet and complete the ‘Evaluation’ section to show how they felt the extension part of their project turned out.
Assessment
Should you wish to use it, we have created an end of unit assessment sheet for you to record the achievements of your class. Make a digital copy (open the document > File > Make a copy) or print it off (open the document > File > Print).
The sheet has the relevant National Curriculum objectives for this unit of work as well as a number of statements to describe children working at age related expectations (ARE) or above ARE. Simply record the names of your students in the most appropriate column. If you feel students are not working at ARE they should be placed in the first column ‘below ARE’.