6 SpaceInvaders
6 SpaceInvaders
6 SpaceInvaders
Introducing Scratch
5 – Space Invaders
In this exercise we will recreate the classic video game Space Invaders. Although an old
game like space invaders might seem quite simple there is actually a lot going on. Plus there
are a lot of sprites. So getting it working like the original will take some work.
To save you some of the work, you can use existing sprites and sounds that are similar to
ones from the original game. Your teacher might be able to provide you with a copy of these
sprites & sounds or you can download them as a zip file from the website at the bottom of the
page.
3. If you have the collection of provided sprites, you can import the image called backdrop.
Fun Fact In the original Space Invaders game, every time you destroyed an enemy ship,
the rest of them began moving faster. That wasn’t intentional. Because of the limitations of
the hardware, the game simply played faster when there were fewer sprites on the screen.
When the game’s creators realised it got faster as you progressed they didn’t mind since they
thought it added more challenge. That ‘feature’ of speeding up wasn’t actually in the code
though. We won’t be adding that ‘feature’ in our version but for an added challenge, you
might be able to work out a way to make yours work that way.
2. Paint 2 costumes for the sprite. One for the normal version of the player ship and one for
the destroyed version of the player ship. You can import the provided versions of the
sprites if they are available.
Tip If you want to position a sprite exactly, drag a Go to x y box in to the script area. Type
in the coordinates you want to move it to. Then click that single
block to run it. You can then remove the block when done by dragging it back in to the script
area.
The coordinates for each shield in the example shown above are:
-140, -120
-50, -120
50, -120
140, -120
Important Remember to save regularly. Now would certainly be a good time to save
what you have done. There’s a lot of work involved in a project like this and you wouldn’t
want to lose a lot of work if something goes wrong.
We will use this variable to determine which way the invaders will move. When the invaders
reach the right side of the screen, the direction variable will change to a negative number (so
they will move left). When the invaders reach the left side of the screen, the variable will
change to a positive number (so they will move right).
2. Add the following code blocks (you can place them next to your existing blocks).
An if block checks to see if the sprite has reached the right side of the stage (190 x
coordinates). If it has, the direction variable will change so that the sprite starts
moving the other way (movie a negative x value goes to the left).
Another If block checks to see if the sprite has reached the left edge. If it has, it
changes direction to the right.
When the invader reaches the edge it changes direction but it also needs to move downward.
That way each time they move all the way across the screen they move closer to the player.
Eventually if the player isn’t careful, they get to the bottom and the game ends.
We already have If blocks that detect when the invader reaches the side of the screen. We
could add a block in there that makes it move down. The problem is that when an invader
reaches the edge, they all need to move down. Not just the one that reached the edge. So how
do we tell them all to move?
The solution is broadcasts. A broadcast allows a sprite to send a message to all the other
sprites. In this case, when an invader reaches the edge, it will send a message to all the others
that it’s time to move down a bit. As invaders sprites receive that message, they will then
move down.
3. Add a Broadcast block to the first If block.
8. Add the following code blocks. Make sure the message is set to “Move Down”.
Now any time a sprite receives the Move Down message, it will move down (negative y
value) as well as changing direction.
9. Test the program to check that the sprite correctly changes direction and moves down
when it reaches the edge.
Notice that our Direction variable is showing on the stage. That can be useful while you’re
testing to make sure it works but once you’ve got the movement working, you no longer need
it showing.
10. Right-click the Direction variable on the stage and choose hide.
If you have the sound files available then we’ll import the shoot sound. Otherwise you can
skip these steps along with any pink sound blocks.
3. With the Bullet sprite still selected click the Sounds tab.
5. Click the Upload sound from file icon and select the sound file called Shoot.
6. You can click the play button beneath the imported sound to preview it.
7. On the scripts tab add the following script blocks so that the bullet sprite is hidden until it
is needed.
Explanation – When the player presses the spacebar the following happens
The bullet will move to the current position of the player sprite
The hidden bullet will now become visible
The shoot sound will play
A loop begins which will make the bullet continually move upward
If you press the spacebar it should work fine. The problem is when the bullet reaches to top it
won’t stop. We’ll add some extra code to make it stop when it reaches the top.
9. Add an if block inside the forever block so that it looks like the following.
Explanation – The If block will check the current Y (vertical) position of the bullet. When it
goes past a certain point near the top it will do the following.
Return to the current position of the player sprite
Become hidden again
Stop everything in this script from running so that it no longer moves.
10. Test the program. Now when you press space the bullet will move upwards. This time
though, when it reaches the top it will be ready for firing again.
Currently we have those shields that aren’t doing anything but we don’t want bullets to go
through them.
11. Add the following If section to your code so that bullets no longer go through the shields.
The if block will activate when the bullet comes in to contact with the colour the shields
are made of (click the colour square in the block and then click on one of the shields to
select the colour).
Hint The parts inside the if block are the same as the if block you just did so you can right-
click and duplicate them.
Explanation – When the program starts, a loop for this sprite will begin. Within that loop is
an if block which checks to see if the sprite is touching the bullet sprite. When it is touching
the bullet:
A broadcast will be sent. We will use this in the bullet sprite so that it can return to the
player sprite.
10 points is added to the score
The costume changes to a small explosion
A sound plays
The sprite disappears.
6. Select the Bullet sprite and add the following code blocks.
Explanation – When one of the invader sprites get hit the bullet will
Return to the position of the player sprite
Become hidden
Stop other bullet scripts so that it stops moving until the player shoots again.
4. Click the Flip left-right button in the top corner of the drawing area so that the second
costume is reversed.
We will make the bomb appear at the top of the screen in a random position after a random
number of seconds. For the purpose of testing we’ll make the delay short for now but we’ll
make it longer once it’s working properly.
5. Add the following script blocks.
Explanation – When the program begins the bomb will be hidden. A loop then begins.
There will be a pause for 1 or 2 seconds (later we’ll increase the duration). Then the bomb
will appear in a random location along the top.
Now we’ll add a loop that will make the bomb move downward while swapping between the
two costumes. We only want it to keep on looping enough times for it to reach the bottom.
6. Add the following before the end of your existing forever block. In this example the loop
will repeat 70 times but you may need to adjust the number of repetitions until it’s just
right.
Now we need to add 2 if blocks. One of them will make a bomb hide and return to the top of
it hits a shield, since it’s not supposed to go through a shield. The other if block will send a
“Game Over” broadcast when the bomb touches the player sprite.
7. Add the following 2 If blocks before the end of your repeat 70 block.
New message
Lastly, we’ll add a hide block at the end of the main loop so that the bomb will disappear
when it reaches the bottom. Otherwise it will stay there until it is told to appear at the top
again.
8. Select the Player sprite and add the following script blocks.
9. Test the program. You might need to deliberately move under the bomb to see it work.
Our other game over condition is when the invaders get close to the bottom.
10. Select the Invader Yellow 1 sprite and add the following script blocks.
11. Test the game to make sure it ends correctly when the invader sprite reaches the top of the
shields.
Tip If you don’t feel like waiting for the invader to move down several rows, you can drag
it while the game is running. Simply drag it to a position a bit higher than the top of the
shields so you don’t have to wait so long.
Now to increase the random interval for the bomb and then make a few duplicates.
12. Select the Bomb sprite and change the wait interval amounts
14. Make 2 more duplicates (you can add even more if you want it to be extra challenging but
4 should do for now.
3. Add the following code blocks. One that will reset the score to 0 at the start of each game
and another that will reset our new variable to 0 at the start of each game.
4. Now add some new code blocks that will check to see if the Invaders Destroyed variable
has reached 50. Once it has, a message will be broadcast and the variable will be set back
to 0 so a new level can start.
6. In the “touching bullet” section we’ll add 2 new blocks. One to add to the variable and
another that moves the sprite further up when it’s hit. Otherwise it might keep on moving
down and give the player a shock when a sprite they already destroyed reaches the
bottom, making them lose. This will move it up enough to put it beyond any other
invaders that are still in play.
7. Finally add the following blocks. These will move the sprite to its original position when
it receives the New Level message.
2. In the starting code blocks, change the position so that it will start 30 pixels to the left of
your first invader.
3. You will also need to make the same change to your New Level blocks.
4. Test the program and you will now have 2 space invaders moving neatly together.
Now for the rest of them. Warning – this is the fiddly part.
5. Make more copies of the sprite until there are 20 of them. They need to be arranged in to
2 rows with 10 on each row. Use the following table so you know the coordinates for
each one. The invaders on each row are 30 pixels apart. Each row is 20 pixels apart.
6. Now make a duplicate of your last invader sprite. The next 2 rows will use different
costumes and will give a higher score when they are shot.
7. Change the name of this copy to Invader Orange 1.
8. Change both of the costumes by drawing / importing orange invader images.
Tip You can drag costumes up or down to re-arrange
their order.
9. In the scripts section change each switch costume block to refer to the orange costumes if
needed.
10. Change the starting and new level coordinates to the following
11. For the orange sprites they will score 20 points when hit instead of 10 so make the
appropriate change.
12. Now make copies of the orange sprite until there are 20 of them.
13. Modify the starting and new level coordinates for each orange sprite as follows.
17. In the scripts section change each switch costume block to refer to the red costumes if
needed.
18. Change the starting and new level coordinates to the following
20. Now make copies of the orange sprite until there are 10 of them.
21. Modify the starting and new level coordinates for each orange sprite as follows.
22. Now give the game a good test. When you hit the invaders, they’ll add to your score
depending on what row they’re on. When the bombs hit your or the invaders reach you,
game over. If you manage to destroy them all, they’ll reset to you can clear another wave
of them.
Tip If you have trouble destroying them all before getting hit or having them reach the
bottom, go to the stage and modify the number of invaders that need to be destroyed to a
smaller amount. Then you won’t have to destroy as many to see if the reset works.
23. Now that we’ve got the main parts working, hide the Invaders Destroyed variable from
your stage and move the Score variable in to a suitable spot.
Explanation – When the program begins, the Saucer will be hidden and then a loop will
start.
Waits a between 10 and 30 seconds before appearing in the top left corner of the
stage.
Plays a sound.
Starts a loop that repeats enough times for it to move to the other side (you might
need to adjust the number of repetitions until it’s about right).
While the loop repeats, it will move and flash different colours.
The saucer can be shot just like any of the other invaders, only it awards a cool 200 points if
you manage to hit it.
5. Add the following script blocks to the saucer (import the Invader Destroyed sound if
you want that to play when this sprite is shot).
3. Add to the first block of code so that it looks like the following.
Stage
Player Sprite
Bullet Sprite
Bomb Sprite
Saucer Sprite
Invader Sprites
Remember that each invader sprite will have different coordinates, scores and costumes.