During this workshop, participants are going to program a shooting game. A spaceship must protect the Earth from hordes of hostile aliens by firing missiles against them. To program this game, the Scratch platform will be used, which has the advantage of using visual code : no need to learn a programming language!
Preparation time for facilitator
Time needed to complete activity (for learner)
Name of author
Support material needed for training
Resource originally created in
The goal of this tutorial is to create a Space Invaders-inspired game in Scratch. A spaceship must protect Earth against legions of hostile extra-terrestrials who then descend the length of the screen. For this, it must shoot missiles on the enemies to defeat every last one. If even one alien reaches the bottom of the screen, Earth falls and the game ends. This being somewhat complex due to the use of cloning and duplicate code, it is recommended for an audience already familiar with Scratch.
Creating the backdrop
On the creation screen choose a new backdrop by clicking the icon in the bottom right corner:
Choose a backdrop from the Scratch library, for example ‘Moon’ from the ‘Space’ category (on the top of the backdrop library screen) and draw a red line at the bottom of the screen.
Creating a ship, an alien and a projectile
We will now the different sprites that will take part in the game. Click on the sprite icon near the bottom right corner and choose ‘Rocketship’. You can also use the paint tool (to draw your own ship) or upload an image from your hard drive.
Add also an alien and a projectile. Rename each of them ‘Spaceship’, ‘Alien’ and ‘Missile’ respectively (you can do this going to the window circled below).
Moving the spaceship
The spaceship should be able to move from left to right only. For this add the three groups of the blocks below in the script of the ‘Spaceship’ sprite.
Code explanation: when the game starts (i.e. when the green flag is clicked), the spaceship will appear (‘show’) in the assigned position. When the right arrow key is pressed, the ship will point to the right (90°) and move (10). When the left arrow key is pressed, the shop points to the left and moves (-90°) and moves (10). Note: the block ‘set rotation style left-right’ ensure the spaceship remains upright and doesn’t flip over when moving left (i.e. when moving -90° it will turn upside down by default). Click on the green flag to test the code. Adjust the values so the ship is placed in the centre of the stage at the beginning of the game and moves at the desired speed.
The projectile must not appear at the beginning; start by adding the following blocks to its script:
The ship needs to know to shoot missiles. To simulate this, program the missiles to appear and move:
Code explanation: when the space key is touched, the projectile will appear (‘show’) at the spaceship and will move up the screen (‘point in direction 0’) until it touches either the edge or an alien. Click the green flag the test the code.
Animating the alien
Up to now the alien has not been moving. There is therefore no challenge or game. We need to make the alien descend the length of the screen in a stilted manner. The game needs to end when the alien reaches Earth (the red line). Add the following blocks to the alien’s script:
Code explanation: when the game starts (when the green flag is clicked), the alien will take its starting position, appear, then, without stopping (‘forever’) will change its position, wait and move. Now add the following blocks, still in the alien’s script:
Code explanation: when the game starts, the alien will check constantly (‘forever’) is it is touching red. If yes, the games stops (‘stop all’). As we placed a red bar at the bottom of the screen, the game will stop with the alien reaches the bottom of the screen. Now we need to make sure the alien disappears when it is touched by a missile. Add the following blocks what you did just previously:
Code explanation: in addition to what was explained above, the alien will also constantly check if it is touching a missile. If it is, it will wait 0.1 seconds and then disappear (‘hide’). Test your code by clicking on the green flag.
With just one alien, the game is too easy. To multiply the aliens, there are various things we can do. The easiest is to duplicate the alien sprite. In each copy, you will simply need to change the starting position (‘go to x:… y:…) so that each clone starts in a different position. A more complex solution is to use the block ‘create a clone’ and modifying the alien’s script as follows:
Each clone created will now have the same code as the original. However, since they will be created after the green flag is clicked, the code will not be executed so they won’t move. Click on the green flag to see this happening. You will therefore need to change the clones’ code so they use ‘when I start as a clone’ instead of ‘when the green flag is clicked’. How do we do this? By adding to the alien’s code! Because this is this code that the clones will have. Right-click on the main code fo the alien and then click ‘Duplicate’ and change the first block to use ‘when I start as a clone’. As the rest of the code will be identical for the first alien as it will be for the clones, they will all move at the same time and will appear in the same position as each other which we don’t want. Therefore, change the clones’ code further to add a block that changes the sprite’s position as follows:
The ‘IF touching colour purple, THEN change x by -80’ block means that the clones will be separated between themselves. Note that in our example, the alien is coloured black and purple. Whatever image you use for the alien, the above code will only work if 1) you match one of the colours of the alien exactly (use the eyedropper tool) and 2) the alien does not come in contact with that colour anywhere else on the field (so for example don’t use black or white for this block). Try the game with this code installed to see the effect. When we test this, we will see that the clones are invincible. This is because the part of the code that makes them disappear, like the ones that make them move, still make them move. The solution is the same as for the code to make them move: duplicate the code, then change the first block to ‘when I start as a clone’:
Click the green flag to check that everything works the way it’s supposed to.
Here are some things you can work on to improve the game:
- change the sizes of the spaceship, missile and alien
- increase the number of aliens
- add different aliens that move at different speeds
- add a player life counter
- add an alien life counter
- create different types of projectiles
- create a point counter and make it show up when aliens are defeated
- add several spaceships
- for those looking for a programming challenge: you can try to optimise the example code as some instructions are not given in the neatest or most efficient ways possible
Facilitation tip: For more explanations on using Scratch, kindly refer to Introduction to the Scratch Interface