Project 3 – Space Invaders part 2
Building Retro Games: Pick a project
Objectives
- Understand and use sequence, selection, and repetition in programs
- 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
Begin by recapping the last lesson:
Tell me about the game we are making?
What have we added so far?
How are we controlling the cannon sprite? With keyboard inputs to change the X coordinate of the sprite.
What problem did we face getting the laser to work? It’s a separate sprite that needs to be able to move independently, but it always needs to appear to shoot from the cannon sprite, so some of the time it needs to follow where the cannon is, and then it needs to shoot up the screen from wherever the cannon happens to be when we press the spacebar.
How did we overcome this? The laser sprite is programmed to always follow the cannon sprite. We also used a variable (cannon x) that constantly changes to match the x coordinate of the cannon. We then applied that variable to the laser in the x coordinate of the ‘glide to’ block that sends it up the screen so it always launches from the cannon and moves up the screen in a straight line from wherever it happens to be fired from.
What should come next in our game? What’s still not working? While the laser fires well now, it doesn’t actually do anything when it hits the alien, so that will be our next step.
Let’s think through the pseudocode for this. Take to a partner and describe in your own words what should happen in a step by step, precise order.
It should be something like this:
- When we press the spacebar the laser sprite should fire up the screen
- If it hits the alien the alien should disappear.
- It might also make a noise at this point.
- It might also give the player a point at this point.
- The alien should reappear when the game is restarted.
To turn this into working code we’ll need another condition and the ‘hide’ and ‘show’ blocks from the Looks category (among other things).
The code should be written for the alien sprite and added inside the Forever loop that’s already there to make it move. Ask the students to have a go at using these blocks to get things working:
Here’s the solution.
The ‘if then’ block creates a condition to detect when the laser sprite touches the alien sprite. If the condition is met the alien hides. Remember, this is an example of ‘selection’ in our code (dictating when things happen). The ‘show’ block at the top of the script makes the alien reappear when the green flag block is clicked to start the game.
Adding a sound effect is easy and just like we did for the cannon. Again, choose something really short as a long sound effect every time you hit an alien will get annoying pretty quickly. We’ve gone for the effect called ‘zoop’, but there are plenty of other short, quick sounds to choose from.
Load in the sound for the alien sprite and then add the ‘start sound’ block after the ‘hide’.
To score a point for a hit we need to create a new variable for the score, do this in the Variables category.
We then need to set (initialise) the variable at the start of the game and decide when the variable value should change. Ask the students to decide where to place the ‘set’ and ‘change’ variable blocks and what values they should have.
Once they’ve tested that and are happy that everything is working, we can start to add more aliens into the game.
This is easily done by duplicating the working alien we have already created. Right-click the sprite thumbnail underneath the stage and choose duplicate.
Move the new alien so it’s beneath the first alien. You can also adjust the ‘move steps’ number (up or down) for the new alien so they move at different speeds across the screen.
Repeat this to add more aliens. We’ve added eight in this example, each one moving at a different speed between 2 and 10 steps. We placed two aliens on each row.
The final step for now is to measure the score variable so the game can detect when all the aliens are gone and the player has won.
Here’s what’s needed. Ask the students to put the blocks together and work out where’s the best place to add the code (which sprite).
The code should be added to the cannon sprite as shown below. Notice that a couple of the values have been changed. The variable should be measured to test when it is equal to 8 (the number of aliens and the maximum score), the message has also been changed as ‘hello!’ doesn’t seem very appropriate for the end of the game! So the message will be displayed and the game stops when the score gets to 8.
You can find a finished version of the game up to this point HERE.
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 a time limit for shooting all the aliens. You lose if the time runs out before you clear the screen of aliens.
- Make the aliens work their way down the screen each time they get to the end of a row (like the original game).
- If the aliens get to the bottom of the screen and hit the cannon it’s ‘game over’.
- Have a limited number of shots you can fire, but make objects appear (and disappear) randomly that you can shoot to collect more ammo.
- Create aliens that appear later in the game that shoot back at you, make their lasers damage your cannon or take away lives. Make the game end when the lives run out or the damage is too much.
- Add bonus items that appear (and disappear) randomly in different places. These could:
- Speed up your cannon’s shooting speed
- Slow down your cannon’s shooting speed
- Make some aliens disappear
- Make some aliens reappear
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:
- Making baddies disappear when shot
- More baddies moving at different speeds
- A variable for scoring
- A way to win the game
What are your ideas for taking the game further? Have you had any ideas already for how to make it work?
Take a selection of ideas and (if needed) share some of the suggestions from above.
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.
What did you think of this unit?
When you have completed the unit with your class, please take a moment to leave us some feedback with this short form, it'll really help us create more content you love in the future. Thank you!