Space Invaders Tutorial 3
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. (MobyGames.com)|
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!
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.
Next, make a new DEPICTION (NOT a new agent) of the Background agent. Call it "Laser Destroyer". It will look like this:
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.
|See detailed description of these agents|
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.
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.
|See detailed description of these rules.|
Laser_Cannon Behavior Explanation:
'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?
|Why doesn't the Laser_Cannon move back?|
When you press an arrow key to move the Laser_Cannon from under the newly created Laser agent, the Laser_Cannon moves onto the adjacent Background agent. However, when you attempt to move the Laser_Cannon agent back, there is no longer a Background agent visible. It is actually there, but stacked underneath the Laser agent. Agents can only "SEE" or "SEE A" the top agent if multiple agents are stacked on top of each other.
BE SURE TO 'RESET' THE WORKSHEET AFTER YOU FINISH TESTING !!!
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.
|See detailed description of these rules.|
Laser_Cannon Behavior Explanation: We use the Once Every condition to specify that the Laser should only attempt to move once every 0.1 second. If we left this out the laser would move to the top of our game play area too fast. The Move action causes the Laser agent to move up one cell.
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|
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:
|Why did we change the order of the rules?|
The Importance of Rule Order: AgentSheets examines the rules within the behavior of each agent from top to bottom, selecting and performing the first rule whose conditions are satisfied. If the conditions of two rules are true at the same time, AgentSheets will choose the first rule in the sequence, ignoring all rules below that. Therefore, it is important to put the preferred rule first in the list. Usually, that means rules that one would consider 'exceptional' situations before rules that are more common. In this case, we want the laser to disappear as soon as it reaches the top of the worksheet, so we make sure it is first in the list. Later, when we introduce the 'collision' of a laser with an alien invader, we will want that rule to appear before the timed movement rule. Otherwise, the laser might 'miss' seeing the invader and move past it.
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).
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".
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.
|See detailed explanation of Mothership movement behavior.|
Explanation:: The rule in the While Running method controls the Mothership movement direction. Each second the Mothership will move randomly left or right. To make this happen we use two new programming concepts: agent attributes and random values. In AgentSheets we can produce random values in the Set action by using the "random(x)" command where "x" is a number. In our program we are using "random(2)" which produces either a 0 or 1. It is important to note that the number 2 is not included in the numbers produced. We assign this random value to the agent attribute (in computer science, a local variable) called "move_direction". An agent attribute is a piece of information associated with a specific agent in the worksheet. Each agent will have its own value for this attribute. Thus, an agent's behavior can be controlled by its own attributes. Agent attributes have numerical values. After setting the value for the movement direction, we send a message to ourselves, with the Make action, to a separate method called "move".
Programming Mothership "move" On Method: To make the decision as to which way to move each second, we create a separate method, by clicking the "New Method" button, and naming it "move", consistent with the name specified in the Make action in the While Running method. The two rules in this method determine which way, IF EITHER, the Mothership will move. As noted above, the Random (2) action causes the "move_direction" attribute to be set to either 0 or 1. If the attribute is 0 AND IF there is empty space to the left, the Mothership moves left. Otherwise, the Mothership moves to the right, but only if there is empty space to the right. Note that we do not need to test whether the "move_direction" attribute is 1, since that is the only other possible value if it is not 0.
Since the random number generator will, on the average, be a 0 or 1 about the same number of times, the Mothership will hover around its starting position. However, as with tossing pennies, there could be a series of consecutive 0s or 1s, which is why the "see" condition is required to ensure the Mothership does not move off the playing area.
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.
|See detailed explanation of new behavior for Mothership and Laser|
Explanation: In the Laser behavior, we add a rule, in which the Laser detects the Mothership above it, and simply sends the Mothership a message ("impact"), then erases itself. Why not erase the Mothership at this time? A valid question. That is, in fact, one way to handle the collision between these two agents. However, suppose you would like for the Mothership to have multiple "lives"; that is, the Mothership must be hit multiple times before exploding? This means that the decision as to what the Mothership will do when it is hit must reside within the Mothership, not the Laser. This messaging approach to agent interaction provides the most general flexibility in handling collisions between agents.
Note that this rule is placed before the periodic movement rule, so that, as an exceptional condition (like the approach to the Laser_Destroyer background agent), we want to be sure to evaluate this rule before the periodic movement rule. The relative order of the two rules at the top of the Laser behavior is not important, since they are mutually exclusive; that is, only one can occur at a time. This means that there is no ambiguity as to which one of these rules is true; whereas, it is possible that one of these could occur at the same time as the "once every" timer occurs.
In the Mothership behavior, we add a new method, called "impact" (the same name as the message name in the Laser behavior. For now, there is only one rule with the actions shown to make a change the depiction to the exploding Mothership, make an explosion sound, wait briefly, then erase the Mothership from the worksheet. The wait action is necessary so that the player will see the explosion depiction briefly.
|Computational Thinking Pattern: Collision|
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.
Let's see if our Mothership moves correctly and can be destroyed.
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.
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 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!
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.
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|
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.
|Click here for a detailed description of the new Mothership behavior.|
New 'While Running' Rules Explanation: The new rule creates a new agent attribute (in computer science this is called a local variable) named "alien_move" that will help determine how the Aliens will move. We use the "game_start" agent attribute to make sure that we do not always set "alien_move" to 12. In AgentSheets, an agent attribute is automatically given a value of 0 when it is created in AgentSheets. However, if you save a worksheet after the simulation has run, and if agent attributes have been changed, the new values will be saved in the worksheet. This is usually NOT what you want, so it is always a good idea to reset the worksheet before saving it.
To understand why we set "alien_move" equal to 12, we use a diagram to help figure this out.
In the tutorial game there are five spaces between the right-most Alien and the right-edge of the game window. That means it will take the Alien swarm 5 moves to go from the left to the right. The Aliens will also need to move 5 more spaces to go back to the left. This gives us 10 moves total. Each time the Aliens move their 5 spaces, they need to go down one space. As there are two edges the Aliens will move down twice. This gives us the other two moves, which totals 12.
If the number of spaces your Alien Swarm has to move left or right does not equal 5, then your "alien_move" variable will have a different value. To easily figure out the value for your "alien_move" variable you can use this formula, where Spaces would equal 5 in the tutorial version:
alien_move = (2 * Spaces) + 2
We also added two new actions to the action side of the original rule. The first action decrements (subtracts one from) the value stored in the "alien_move" agent attribute. Then we use the Make action to send a "move_aliens" message to a new method within the Mothership behavior. (That is what the "dot" means in the move direction field.)
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.
|Click here for a detailed explanation of the "move_aliens" Method:|
On "move_aliens" Method Explanation: The first two rules deal with moving the Aliens down. When "alien_move" is equal to 0 or 6, then we are at an edge and a message is broadcast to the Aliens telling them to "move_down". However, when "alien_move" is equal to 0, we have to reset the "alien_move" variable to 12 so that our Script will run again! If we did not reset "alien_move" to 12, our Aliens would stay in place after completing our Script one time.
The third rule controls the Alien movement to the right. When "alien_move" is greater than 6, we want to the Aliens to move right. To do this all we have to do is broadcast the "move_right" message to all of the aliens.
The fourth rule controls the left Alien movement. In this case we are concerned about "alien_move" being greater than 0. We then broadcast the "move_left" message to all of the Aliens.
Let's see if our Alien Swarm moves correctly based on our Script.
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 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.
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.
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.
|Computational Thinking Pattern: Generate|
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.
|Click here for a detailed explanation of the Alien Bomb behavior and additional behavior additions to other agents:|
Explanation: The Alien Bomb has actions similar to the Laser. The Bomb must recognize when it is time to disappear. For the Laser, there is the Laser_Destroyer depiction of space to allow the Laser to know when the end of the worksheet is reached. The Ground agent serves as this indicator for the Bomb. When the Bomb is stacked above the Ground ("collision" computational thinking pattern), it erases itself. The Ground serves to "absorb" the Bomb.
The Alien Bomb also recognizes a collision with the Laser Cannon, just as the Laser recognizes a collision with the Mothership. As with the latter, the Bomb simply sends the Laser Cannon an "impact" message; it is the responsibility of the Laser Cannon to decide what to do. This allows the Laser Cannon, for example, to require multiple impacts before being destroyed; although, the current implementation only allows one hit. More advanced options are discussed in a separate tutorial.
Other than a collision with the Laser Cannon or Ground, the Alien Bomb, moves periodically downward.
The Laser behavior has two new rules that are almost identical to the rule that handles collision with the Mothership; that is, the Laser sends an "impact" message to the Alien and to the Alien Bomb, then erases itself. As with the Mothership and the Laser Cannon, it is the responsibility of the Alien and the Alien Bomb to determine the appropriate action for this message.
A new method is added to both the Alien and the Alien Bomb to handle an "impact" message from the Laser. As with the Mothership and the Laser Cannon, the Alien and Alien Bomb simply erase themselves when receiving this message. More complex behavior, such as requiring multiple impact messages before destruction, can be added to these methods.
Finally, we added a rule to the Alien behavior to drop Alien Bombs periodically. Note the three conditions: there must be empty space below the Alien, the creation of a new Alien Bomb happens periodically ; and there is a certain randomness in the Bombs because of the "% chance" condition. The combination of the time interval in the "once every" condition and "% chance" condition helps determine the game difficulty. Note, also, that the "New" action places the Bomb directly above the Alien. This is an aesthetic decision to make it appear that the Bomb is emanating from the Alien, rather than simply appearing in space below the Alien.
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
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!]
- Space Invaders tutorial part 2: Shields and more.