Space Invaders Tutorial 3

From Scalable Game Design wiki
Jump to navigation Jump to search
Space Invaders type game described in this tutorial


The purpose of this tutorial is to create a ‘Space Invaders’ style game, which is a classic shooter game originally created by Tomohiro Nishikado in 1978 and it was published and manufactured by Taito. A description of the game follows:

Earth is under attack from rows of bomb dropping aliens, and you need to defend it! The rows of aliens begin at the top of the screen, and you control a laser cannon at the bottom of the screen. Your goal is to earn points by shooting the aliens before they can land. The aliens march back and forth, and each time the end of the screen is reached they will drop one row closer to the bottom. Shoot them all, and you move on to the next (tougher) level, but if even one of them lands the game is over. From time to time a flying saucer will pass by along the top of the screen; shoot this to earn extra points. Just above your laser canon are three shields; these can be used to hide from the alien's bombs, but will also block your own shots. You begin the game with a limited number of laser canons, and if all of them are bombed the game ends. (

The player cannon starts at a specified location on the ground in the game. The person playing the game can move the cannon in two directions (left and right) and also fire lasers. The object of the game is for the player to destroy all of the alien invaders with their laser beams. The alien swarm moves right, left and down. They also drop bombs downward trying to destroy the cannon. In our version the flying saucer will always be on the screen and will move randomly left or right. The game ends when the laser cannon is destroyed or an alien touches the ground. We will not keep track of a score, but by using a method similar to the step counter in the Sokoban tutorial you could make your own score!

Getting Started

By now you should have programmed at least one game with AgentSheets, so we will skip over some of the specific details about creating projects and how to use AgentSheets from the previous tutorials.

Make a New Project: The first thing you should do is create a new project and give it a meaningful name, such as "Space Invaders". When creating the project use 32 x 32 sized Agents.

Creating Agents: Create Agents for the background, the laser cannon, the ground, and the player laser.

Agent Depictions
Background Depiction
Laser Cannon Depiction
Ground Depiction
Player Laser Depiction

Next, make a new DEPICTION (NOT a new agent) of the Background agent. Call it "Laser Destroyer". It will look like this:

Laser Destroyer

This depiction will serve as an "absorber" for lasers that are fired but miss their targets. It will be the upper border of the game worksheet.

Have fun with creating these agents and use your imagination!

Making the Worksheet: As you have done before, make a new worksheet and use the filled rectangle tool to make a rectangular area of Background agents, with a row of Laser_Destroyer agents at the top.. Next, place Ground agents in a row along the bottom of the Background area. Finally place one Laser Cannon just above the Ground agents. Use the picture at the start of the tutorial as a reference.REMEMBER TO SAVE YOUR WORKSHEET EVERY TIME YOU MAKE A CHANGE THAT YOU WANT TO KEEP, ANDONLY WHEN YOU MAKE A CHANGE THAT YOU WANT TO KEEP!!

Your worksheet should look like the picture below.

Space Invaders beginning worksheet layout.

Programming the Laser Canon

Editing the Laser Cannon Behavior: Open the Laser_Cannon behavior editor. You will add rules to the While Running Method that will allow the Cannon to move and fire Lasers. Use the picture below for help.

Laser Cannon Player Control Behaviors

'Testing the Laser_Cannon behavior:' Now that you have programmed the basic behavior of the Laser_Cannon, you can test it in the worksheet. Click on the PLAY button in the worksheet and press the LEFT and RIGHT arrow keys. The Laser_Cannon should move back and forth, and it should not move off the right side of the Background field. Press the SPACE bar key and you should see a Laser agent appear on top of the Laser_Cannon. Then press a LEFT or RIGHT arrow key to move the Laser_Cannon from under the Laser agent. Then press the opposite direction arrow key to see what happens. Can you explain this?


Programming Laser

When the player shoots a Laser, we want it to move upward.

Adding Laser agent behaviors: Open the Laser behavior editor and add the condition and action to the empty rule in the "While Running" Method that match the picture below. Remember to click Apply or OK when finished so that the changes are applied to the agent behavior.

Laser Movement Behavior

Test the new behavior of the Laser agent in the worksheet. Click on the PLAY button and press the SPACE bar once. Notice that the Laser agent appears and then moves quickly to the top of the worksheet and stays "stuck" at the top. This is because an agent cannot move off the end of the worksheet.

Computational Thinking Pattern: Absorb
CT Spiral small.jpg
Absorb: What we want is for our Laser agents to disappear when they reach the top of the game window. To do this we will use another agent to detect the Lasers and signal that they should be absorbed, which erases them from the game window.

You can look at the complete Absorb Description for more information.

In order to make the laser disappear at the top of the worksheet, we add another rule to the Laser agent behavior. Open the Laser agent behavior and click on "New Rule". Then move the rule above the existing rule. (Do this by clicking on the red portion of the rule on either the left side or middle and dragging it up until a bright red line appears above the first rule, then release the mouse button.) Drag the "Stacked" condition that shows icons into the condition side of the rule. Select the "Laser Destroyer" icon in the condition. Drag the "Erase" action to the action side. The Laser behavior should now look like this:

Laser Behavior at Laser Destroyer

Be sure to test the new Laser behavior and reset the worksheet when you are done.


So far we have a fairly boring game with only a Laser Cannon that can shoot Lasers. Now let us add an important alien ship, the Mothership!

Create a Mothership Agent: Create a new agent and name it "mothership". For the depiction make something that looks like a UFO (unidentified flying object).

Mothership Depiction

Make Exploding Mothership Depiction: Our Mothership can be shot by the Laser Cannon, and if it is hit we want it to explode! Make a new depiction for the Mothership by selecting the Mothership in the Gallery window and clicking the "New Depiction" button or by going to the Gallery menu and selecting "New Depiction...". Name this depiction "ms_explode".

Exploding Mothership Depiction

Add Mothership Agent to Worksheet: Add one Mothership agent to your worksheet and place it in the row below the Laser_Destroyer agents.

Programming Mothership While Running Behaviors: Use the picture below to program the Mothership behavior for the While Running loop.

Mothership On "move" Behaviors

When Laser and Mothership Collide ...

We will not program the interaction between the Mothership and the Laser. This is known as "collision," and it is a common situation in AgentSheets programming. First, we program the Laser's detection of the Mothership, then we program the Mothership's response. See the two images below to add the new behavior to these two agents.

Mothership On "move" Behaviors

Mothership On "move" Behaviors

Computational Thinking Pattern: Collision
CT Spiral small.jpg
Collision: When the Laser runs into the Mothership we are dealing with a collision pattern. In this case we have chosen to signal the Mothership of the collision ("impact" message) and allow the Mothership to determine the consequence. However we could have chosen entirely different actions occur if we wanted, such as completely controlling all of the interaction from either the Mothership or Laser.

The main point about collisions is that two or more Agents are running into each other and we program actions to occur depending on what the conditions are. For instance, if two cars collide, we may want them to both become damaged in some way. We may even want to signal still other agents not involved in the collision that an event has occurred, such as updating a counter.

You can look at the complete Collision Description for more information.

Play Test: Mothership Movement

Let's see if our Mothership moves correctly and can be destroyed.

  • Does the Mothership appear to move randomly to the left and right?
  • Can you shoot the Mothership and destroy it?

If the answer to these questions is "No", then go back and check your behaviors for the Laser and Mothership. Then retest.


Now we will add in the main enemies in our game, the Aliens!

Create Alien Agent: Let's start by creating a new agent named "alien". The type of Aliens that will be in the game is up to you, but below is an example that you could include in your game.

Alien Depiction

Add Alien Agents to Worksheet: Add six rows of Alien agents to your worksheet, where each row has the same number of Aliens in it. Start the rows on the far left and add as many Aliens as you want in your row, but make sure to leave at least two Background agents visible on the right side of the rows. After you are happy with your rows of Aliens be sure to save the worksheet. You can use the picture below as an example, which has five empty spaces between the alien swarm and the right edge of the play area.

Alien Agent Positioning

Alien Explosion Depiction: When the Aliens are hit by the Lasers, they need to explode. Add an explosion depiction to the Alien agent. We will not use this depiction now, but we will soon!

Alien Explosion Depiction

Alien Movement

Now that we have our awesome alien swarm, let's make it move around the screen! First, let's give the Aliens the ability to move.

Adding new Alien Methods: Each Alien can move in three directions: Left, Right, and Down. Let's start by adding three simple methods to our Aliens that will have the Alien move in these directions. The Method names should be "move_left", "move_right", and "move_down". Use the picture below for guidance on creating these methods.

Alien Movement Method Behaviors

Movement Method Explanation: When an Alien receives the message "move_left", it will merely move left one space. Moving right or down is also handled the same way.

Making the swarm move: How do we actually get the Aliens to move together as a group? There are many ways we could make the aliens move, but we will let the Mothership control their movement.

Computational Thinking Pattern: Scripting
CT Spiral small.jpg
Scripting: What we are going to be doing is providing a Script for the Aliens, telling them how and where to move. This is similar to a choreography pattern for dancing, following the directions on a treasure map, or following the script while acting in a play. With this method there will be a "boss" agent that tells the other agents what to do.

You can also think of this tutorial as a script that you are following to complete the Space Invaders game!

You can look at the complete Script Description for more information.

Updating the Mothership behavior: Now we need to start updating the Mothership behavior by starting with adding a new rule and additional actions to the existing rule in the Mothership's 'While Running' Method. Use the picture below for guidance on creating these two new rules.

New and changed rules in 'While Running' Method for Mothership Alien Swarm Control

Adding New Mothership Method: We now will add a new method to the Mothership that will react to the "move_aliens" message. Create this method as you have done previously and then use the picture below to help setup the rules.

Mothership On "move_alien" rules

Play Test: Alien Swarm Movement

Let's see if our Alien Swarm moves correctly based on our Script.

  • Does the Alien swarm move to the right the correct number of spaces?
  • When the swarm reaches the right-edge does it move down one space?
  • Does the Alien swarm move to the left the correct number of spaces?
  • When the swarm reaches the left-edge does it move down one space?

If the answer to these questions is "No", then go back and check your behaviors for the Aliens and Mothership. Retest.

Alien Bomb and Interactions with Other Agents

The game will still not be terribly fun if the Aliens never attack, so let's give them something to shoot back at us!

Create Alien Bomb Agent: Create a new agent and name it "alien_bomb". Use the picture below as a guide for creating this agent, but feel free to be creative!!

Alien Bomb Depiction

Alien Bomb Behavior: The Alien Bomb is similar to the Laser behavior. Please use the picture below to help you create the behaviors needed for the While Running loop.

Alien Bomb Behavior

Added Laser Behavior: Now is a good time to handle the interactions between the Laser and the Alien Bomb, as well as the Alien. Please use the picture below to help guide you through the added rules in the Laser bahavior.

Updated Laser Behavior

Added Laser Cannon and Alien Behavior: Please add an additional Method to the Laser Cannon behavior, called "impact", to handle the message the Alien Bomb sends when it encounters the Laser Cannon. (Note the new depiction of a Laser Cannon Exploding.) Please add an additional Method the the Alien behavior, to handle the "impact" message the Laser will send when it encounters the Alien. In addition, add the rule in the While Running Method of the Alien to periodically drop bombs into space. The updated Laser Cannon and Alien behaviors are shown in the images below.

Updated Laser Cannon Behavior

Computational Thinking Pattern: Generate
CT Spiral small.jpg

Generate: When we want agents to appear automatically, we use a computational thinking pattern called "Generate." Basically, this is a periodic creation of a new agent, based on one or more conditions. In this project, we want the Aliens to periodically, and with some randomness, generate Alien Bombs to attack the Laser Cannon. This is done with a New" action in a rule in the While Running Method.

You can look at the complete GenerateDescription for more information.

Updated Alien Behavior

Finishing Up

GREAT WORK! You have now created all of the agents and programmed all of their behaviors for this tutorial. Let's have one final play-test to make sure everything is working correctly.

Press the RUN button and try shooting the Aliens. Try to "knock out" as many Aliens as you can, as well as the Mothership. Be sure not to get hit by an Alien Bomb!!


Play Test: Game Checklist


  • Does the Laser Cannon move left and right when pressing the arrow keys?
  • Does the Laser Cannon shoot Lasers when you press the Spacebar?
  • Do the Aliens drop Bombs?
  • Do the player Lasers get absorbed by the Laser Destroyers and do the Alien Bombs get destroyed by the Ground?
  • Do the Lasers and Alien Bombs get destroyed when they hit each other?
  • Can the Aliens, Laser Cannon, and Mothership be destroyed?
  • Does the Mothership move randomly left and right?
  • What happens to the Aliens when you destroy the Mothership?
  • What happens if the Aliens reach the ground? [How could you improve this?]
  • What happens if you destroy a column of Aliens on either side of the swarm?

If one or more of these areas is not the way you wish, go back to the related section and see what might be wrong.

Otherwise, if everything works correctly SUPER JOB!

What's Next?: You have created part 1 of your own Space Invaders style game! You can now go back and make any changes you want to make (like redrawing an agent, adding other behaviors etc.)

When you are satisfied with this version of your game, you can go on to Part 2, which will add Shields, give the player more Laser Cannons than just one (multiple lives), and some other cool features! [Note: the Part 2 tutorial is still a work in progress!]