Space Invaders is a simple concept, a lone defender armed with just bullets and a shield must to try and stop the oncoming alien invasion. The aliens have an army slowly advancing, as well as a mothership, all of whom must be stopped to ensure the earths safety. This feels like the premise for a good sci-fi movie, but let’s figure out how to tell the story through games first before bringing it to hollywood.

Sprite Creation

  • You’ll need a sprite for the player, aliens, spaceship and bullets
    • Scratch’s options can be fairly limited when it comes to extra terrestrial life, this may be a good opportunity to design or upload your own
  • The player sprite will require two costumes, a regular one and a dead one
    • If you’re feeling creative you could do the same for the aliens and spaceship

Set up Player movement

  • Like previous tutorials, we’ll want to assign movement along the X-Axis to the keyboard arrows
  • Space is also a good key to use to fire bullets
  • For firing bullets we’ll want to create a broadcast “Fire”
    • This will be received by the bullet sprite
    • We should also add a Wait 1 second to create a pause between bullets

Set up Alien Movement

 

  • As a homage to the classic arcade game style, lets try and create the blocky movement
  • We’ll build all the code on one sprite and then copy it to the other aliens
  • Set a start position to the left of the screen
    • The start position will have to be adjusted when copying the code to other sprites
  • Try to copy this code exactly
    • If you make adjustments to any values they’ll likely all need adjusting, play around see what works for your game

Attack of the clones!

  • To allow us to create lots of aliens with slightly less sprites to keep track of, we’ll create a clone of each alien
  • Scratch sometimes prefers to break down tasks into multiple sequences especially if theres a lot of conditions and rules
    • This is also easier on the programmer’s (Your) eyes. A good programmer can not only build these amazing programs, they can also keep the code clean and readable to make it easier to spot mistakes
  • Under a new On Green Flag Clicked, add in a Show block and “Create clone of myself”
  • You can copy the exact same code as above and replace the green flag with a “When I start as a clone”
    • make sure to move the start position a bit
  • When you’re done it should look something like this

Now would be a good time to duplicate your sprite to set up an army of aliens. Make sure to adjust there coordinates so they’re nicely spaced out in a grid

Setting up Bullets

  • This sprite will start on receiving the broadcast Fire
    • when the script starts it should go to playerSprite and show
  • Add a repeat until loop with the condition touching edge
    • Change the Y by 10
  • After the loop the bullet should be hidden
  • As the sprite requires being hidden, we should add a On Green Flag Clicked just to hide it

Setting up collision

  • Under the emptier On Green Flag Clicked in the Alien sprite lets add an If alien sprite touches bullet forever loop
    • hide the sprite
    • Broadcast “Hit”
  • We need to add the same blocks to a new When I start as a clone Script
  • Let’s jump back to the bullet sprite. When it received “Hit”
    • Hide
    • Go to player sprite

 

The aliens have guns too!

    • You don’t bring a knife to a gun fight, so the aliens brought there own space guns
    • The Aliens technology may be confusing and scary, but this big code doesn’t need to be too

  • First lets create a new variable called EnemyFire
  • Add up the number of alien sprites you have (add them up from the sprites panel, don’t include clones)
  • Set EnemyFire to Pick random 1 to the number of alien sprites you have (In this example we have 9)
  • Let’s create the first If Statement
  • If EnemyFire = 1, go to the first alien sprite
  • Repeat change Y by -10 until touching edge
  • Hide
  • Let’s keep duplicating this If statement block for each alien, If EnemyFire = 2 and so on until all the aliens are assigned
    • This code might get big and scary looking but it’s super straightforward

Setting up collision for the Player

  • If the player touches the enemy bullet
    • Set the costume to the second, “Game Over” costume
    • Stop All
  • Lets do the same but if touching the colour of the alien sprites

That was a lot, lets stop and test it all works okay

  • Do the characters move as expected?
  • Do you get the nice clean group movements of the aliens?
  • Can the aliens be shot and in turn shoot the player?

We did promise a spaceship

  • The spaceship is a harder, more valuable enemy
  • We’ll set it to wait a random amount of seconds as we want it to only appear some of the time
  • We’ll also set it to a considerably faster move speed than the regular aliens
  • Lets get another On Green Flag clicked for another set of spaceship instructions
  • If it touches the edge, we want to hide it and send it back to its start point
    • This will provide the illusion of it looping or circling
  • If the bullet touches it
    • broadcast Hit so the bullet knows it’s done its job
    • Stop all other scripts in this sprite to stop the spaceship reappearing

Our Spaceship needs a gun too!

  • As you see in the previous code snippet, we broadcast “attack” as part of the spaceships appearance
  • This message is so we can make a new bullet
  • Since the spaceship is meant to be difficult, we want this bullet to spawn quite frequently
    • Otherwise its the same principal of the other bullets, go to the firing object and travel along the Y axis
  • We’ll need to add collision to the player sprite
    • You can simply duplicate the regular bullet if statement and add in the new bullet sprite

 

Lets add a score to thank the earths defender for their services

  • Create a new variable for all sprites called Score and set it to visible
  • Since we’re already working with the spaceship, lets add Change Score by 10 into the Touching Bullet condition
  • Going back to the regular aliens
  • Under the broadcast Hit, lets add Change Score by 1 since they’re less valuable
  • Finally lets go back to the player sprite, and right under the green flag add Set Score = 0 so that we always begin the game at 0

Test the game

Let’s add a shield

  • The original Space Invaders had 3 shields but we’ll just make one for now
    • Feel free to duplicate the shield when done to have more
  • Draw a shape to create a shield sprite
  • Duplicate it multiple times (let’s say 5). For each one use the eraser or paint to make it look worn down
  • When the green flag is hit we want it to go to the first costume, the one without any damage
  • We’ll make a new broadcast called Shield Hit
  • If the costume number is less than 5, we’ll jump to the next costume, but once it’s out of costumes the shield will be destroyed
  • Jump back to the main alien bullets sprite
  • We’ll want to add an if touching Shield loop, it’s very important this code goes inside the “repeat until touching edge” loop, like pictured below. You’ll need to copy this into each If Enemy Fire loop
  • You’ll also need to add the same code to the player bullet and spaceship bullet


Test the game again

  • Does everything work as expected?
  • Are you happy with how everything looks?
  • Find a friend to test the game with, we’ll use two stars and a wish to talk about what we love and what we think could be even better
  • Publish your game so everyone can try! Just make sure to add a description and some instructions to help out your player
Congratulations! Not only have you saved the earth, but you’re also well on your way to being a fully fledged games developer! After the game you just built we truly can aim for the stars!