About Space Invaders Game:

The object of the game is to destroy as many invaders as possible and keep them off the ground. Let’s start saving the world from an alien invasion.

Controlling the Rocketship with Arrow keys:

When you press the Left-and Right-Arrow keys on your keyboard, the Rocketship should move smoothly left and right. You can adjust the player movement speed by increasing or decreasing the CHANGE X BY value.

Test it! The movement of Rocketship with your arrow keys.

Shooting the aliens with Laser

Let’s set up our laser that we’ll shoot from the Rocketship at the aliens.

When spacebar is clicked, a laser should shoot out of your spaceship and destroy any aliens it hits. It should move up until it touches the edge of the screen or an Alien. If it hits, then hide the laser in either case.

Test it! Each laser should now destroy just one alien or disappear when it reaches the top edge of the Stage.

Simple Space Invaders Scratch Game Code

Make the Clone of Aliens:

Instead of duplicating, you can now use a single code block to clone as many aliens as you want when the game begins. Can you see why this is so much better? You only have to make changes to that first alien and all the clones will have those changes.

Steps involved in creating Aliens Clones:

  1. Move the original Alien sprite to the top-left corner of the Stage.
  2. The repeat 7 block repeatedly moves 60 steps to the right while making clones of itself, resulting in a row of 7 Alien clones.
  3. The repeat 3 block repeats the row-creating code to create 3 rows of Alien clones.
  4. 7 Alien clones in a a row multiplied by 3 rows results in total 21 Alien clones.
  5. set the X value back to -200 so the parent sprite starts each row in the same location.
  6. Add a HIDE block at the end of your alien cloning code, then add a SHOW block at the beginning of the code.

Test it! Each time you click the Green Flag button the original alien sprite will appear, create all the clones, and then hide during the game.

Give clones their marching orders:

The clones wait patiently until they receive the broadcasted message “Start Marching”, and then the clones should begin marching back and forth across the stage.

If you want a sprite to point only left or right, you can change the rotation to left – right.

When the invaders reach the right side of the screen (if X position is greater than 210),and left side of the screen (if X position is less than -210), they should broadcast the message “Turn”

When the alien clones receive the message “Turn”, they should do following two things:

  • When your marching aliens reach the right side, they should now change direction without flipping upside-down.
  • They also should move down the Stage, getting closer and closer to the player.

Test it! When the invader reaches the edge it changes direction and also moves 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.

Program Enemies to Drop Bombs

The game will be a lot more challenging if the aliens can fight back. Since they are bug-shaped, I think we should give them some exploding egg bombs!

You need to instruct the bombs

  1. To have different enemies attack at random.
  2. For every three to six seconds, each alien will create a clone of bomb.
  3. To drop toward the bottom of the Stage.

Randomize enemy attacks:

It will require a bit more programming to have different enemies attack at random.

Program your bomb so that, it will drop at random times and drop from different enemies instead of same place.

Create variable named ‘bomb x’  and variable named ‘Bomb y’. This variable will instruct the bombs where to fall from.  By using variable, each alien clone tell the bomb where it is.

Test it! When the green flag is clicked,the bomb drops from random enemies.

You need to instruct the bombs to drop toward the bottom of the Stage. It should move down until it touches the edge of the screen or an Rocketship, If it is then delete the Bomb in either case.

Add Sound to Your Game

Sound effects are a vital part of the gaming experience, and Scratch makes adding them to your project simple.

Add sound each time Rocketship shoot the laser  at the aliens.

Add sound each time an enemy bomb collides with the spaceship, the player loses one life.