Space Invaders is such an arcade classic that we have to make our own version. This one is a little on the simple side and needs some extra tweaking to make it worthwhile. The key to this is to get the invaders to behave as a single unit.
This is the trickiest part of the program. The alien squadron should move until one of its number reaches the left or right side. At this point, all of the invaders change direction. The aliens are shown in blue here. I prefer them to be white against the black background.
Moving The Squadron
Click on the image for a larger version. There is a lot going on in this function. The first part is trying to determine if any of the aliens has reached the edge of the screen. The last part does the moving. The variable a is used to allow for the simplest of animations on the alien sprites.
Like the enemies, the ship is shown in blue here. White on a black background is my preferred option.
Update the On Start event to call the new function. Add the call block - do not try to add another On Start or the program will not work as you expect.
The firing variable is used to make sure that you can only have one active bullet at any time.
If the bullet reaches the top of the screen, we have to destroy it,
Enemies have to die when they are shot. The sprite and othersprite variables in this code are not normal variables - do not try to make them. Instead, drag them from the function definition into the place where you need them.
Aliens Fight Back
This will get the aliens dropping some bombs. We can't use projectile for the kind of sprite here or the aliens will blow themselves up.
Challenges & Extensions
This guide gave a basic outline of the key elements of a Space Invaders game. There are many improvements to be made and some standard features that could be added.
- The first step would be to improve the graphics and make them your own. There are also some settings (speeds, for example) that need tweaking to make the game more playable. The sounds could be improved too.
- There is currently no scoring or lives system in the game. This could be added.
- Currently, there is no benefit to be had in shooting all of the aliens. They don't return. The game update events both make an array of the aliens that are present in the game. If the length of that array is 0, then all of the aliens have been killed. This would be a good time to call the function that makes a new squadron.
- The aliens do not get any quicker or lower on the screen over time. This would be relatively easy to add. There is a variable that determines how frequently the game updates for alien movement. There is also code within that update event to determine if the aliens have reached the edge of the screen. These things could be altered over time to make the game more challenging. You would also need to make an event to check if the aliens are overlapping with the ship and cause death if they are.
- The traditional game of Space Invaders has barricades to protect the ship from the alien bombs. The barricades deplete over time as they are shot or bombed. You could add these features.
- The traditional game also has a UFO that appears periodically and strolls across the top of the screen. Shooting the UFO gets a bonus for the player. Make sure that the UFO is not of type enemy or it will move with the aliens.
- The frequency of the alien bombs is not quite right. As the aliens get fewer in number, the chance of dropping a bomb should increase. This needs a mathematical formula.