Space Invaders Tutorial 1
In Progress (Beta) - Overview
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!
Follow the instructions below exactly, being very careful not to miss any steps and to proceed in the correct order. In building this game you will quickly learn many programming and logic concepts and will eventually be able to create your own original games using your imagination!
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 "SpaceInvaders". 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.
- The Background agent will be all black or some other color that you want. It will provide the location where the aliens and player cannon will move.
- The Laser Cannon agent is player controlled and it is the "hero" of the game.
- The Ground agent is what the Laser Cannon agent rests above. The Ground also has another purpose that we will talk about later.
- The Laser agent will be shot out of the Laser Cannon agent.
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. 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.
|IMPORTANT: Saving the Worksheet|
While the Worksheet Window is selected, go to File->Save; DO NOT CHANGE THE DIRECTORY UNDER ANY CIRCUMSTANCES! IF YOU DO THIS THE PROGRAM WILL NOT WORK. Name the worksheet "level" and press Save. You can test if saving worked, by pressing the "Reset" button in the worksheet window: if the screen doesn't clear (meaning no agents disappeared), you have successfully saved the worksheet window. If the screen did clear, place the agents in the correct positions again and try to save again following the directions above. If the screen still clears, Ask for Help.
Programming Laser Cannon
Let's move on to programming the hero of our game, the Laser Cannon!
Editing Laser Cannon Behavior: Once you have opened the Laser Cannon behavior editor, we will need to add rules to the While Running Method that will allow the Cannon to move and also fire Lasers. Use the picture below for help on setting up your Laser Cannon.
Laser Cannon Behavior Explanation: The first rule we have added allows the Cannon to move to the left when the "left" arrow key is pressed. First we use the KEY condition to see if the "left" arrow key has been pressed and then we check if there is a Background to our left with the SEE condition. If both of these condition are true we then tell the Laser Cannon to move one space to the left with the MOVE action. The next rule uses similar conditions and actions to make the Cannon move to the right. Our third rule checks to see if the "space" key has been pressed and if so we use the MAKE action to send a "fire_laser" message back to ourselves. The ONCE EVERY 0.5 SECS prevents a player from continually shooting a stream of Lasers.
|Computational Thinking Pattern: Generate|
Generate: When we want Agents to be produced automatically, we will use a Generate pattern in our programs. We can make the generated pattern random or uniform, however we are programming this behavior ourselves.
In the case of Space Invaders the Alien Bombs are generated in a randomly, however the player Lasers are generated as the player presses the space key.
There are many examples of generating patterns in everyday life, such as clouds sometimes producing rain or weeds springing from the soil. Both of these real world examples could be simulated using a computer with a generate pattern!
You can look at the complete Generate Description for more information.
Adding "fire_laser" On Method: We need to tell the Laser Cannon how to react to the "fire_laser" message when it receives it. Use the picture below to setup this method and behaviors.
Explanation of "fire_laser" On Method behavior: We create a new Laser agent above our Laser Cannon using the NEW action, with an "UP" direction. The Laser agent depiction is also specified for what agent to create.
When the player shoots a Laser, we want it to move upward.
Adding Laser agent behaviors: Open the Laser behavior editor and add rules to the While Method that match the picture below. Remember to click Apply or OK when finished so that the rules are applied to your game.
Explanation: We use the Once Every condition to specify that the Laser should only attempt to move once every 0.1 seconds. If we left this out the laser would move to the top of our game play area too fast. The See condition is used to test the area ahead of the Laser to make sure it contains a Background agent. If the two conditions are true we want to move up by one space and we use the Move action to do this.
Another useful feature is making our Laser destroy itself! This will be helpful later on, but we can program the behavior now.
Adding new method to Laser: Create a new method for the Laser agent and change the message text to be "destroy_laser". Use the picture below as a guide for programming this method.
"destroy_laser" Explanation: The "destroy_laser" method merely erases the Laser receiving the message.
GREAT JOB! You are now ready to test your game! Press the RUN button and try moving your Laser Cannon and also shooting some Lasers. What happens?
If something does not appear to be working correctly, go back and check your behaviors for the Laser and Laser Cannon. Retest.
If everything works then you are on your way to becoming an AgentSheets master! Keep up the good work!
Deleting Player Lasers
During the play test, your Lasers should have gone as high as they could on the Background area and then stopped. We want our Lasers to disappear when they reach of the top of the play area, so let's fix that now. We are going to use a computational pattern called Absorb to accomplish this.
|Computational Thinking Pattern: Absorb|
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.
Create a Laser_Destroyer Agent: Create a new agent and name it "laser_destroyer". For the depiction there are several choices, but making it all black or transparent are good ideas. Another idea is to make it something that matches the game theme, for instance stars are used in this version.
Add Laser_Destroyer Agents to Worksheet: Now make one horizontal row of Laser_Destroyer agents at the top of your game level. You may have to put the Laser_Destroyers over the Background agents. It is your choice whether to delete the Background or just stack the Laser_Destroyers over them.
Programming Laser_Destroyer Behavior: Once again, use the picture below to program the "laser_destroyer" behaviors.
Explanation: We want the Laser_Destroy to take some action when it sees a Laser below it. In this case when it sees a Laser it sends the message "destroy_laser" to the Laser agent. This message invokes the On "destroy_laser" method that we previously created. When the Laser receives the message it erases itself.
Let's see if the Lasers disappear when they reach the Laser Destroyers. Press the RUN button and shoot some Lasers. What happens?
If the Lasers are not being destroyed correctly go back and check your behaviors for the Laser_Destroyer. Retest.
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 behaviors for the While Running loop.
Explanation: The first rule makes the Mothership explode if it is hit by a Laser. First we check if there is a Laser below the Mothership; if there is, we Wait briefly and then destroy the Laser and change to the Exploding Mothership depiction. Finally, we Wait again and then Erase the Mothership agent. The Wait actions are used so that the player can actually see the Laser hitting the Mothership and also the Mothership exploding.
|Computational Thinking Pattern: Collision|
Collision: When the Laser runs into the Mothership we are dealing with a collision pattern. In this case we make the Mothership explode and the Laser disappear, however we could have entirely different actions occur if we wanted.
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.
You can look at the complete Collision Description for more information.
The second rule partially controls the Mothership movement. After one second the Mothership will move randomly left or right. To make this happen we use a new programming concept; 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 local variable "move_direction". Finally, we send a message back to ourselves called "move".
Programming Mothership "move" On Method: We will now implement the On Method for the "move" message that our Mothership will receive. Click the "New Method" button in the Behavior editor and use the picture below to help you setup this method.
Explanation: There are two rules for the Mothership that control its movement. First, the value of "move_direction" is checked and if it equals 0 and there is a Background agent to the left, the Mothership will move left one space. If however the value for "move_direction" equals 1 and there is a Background agent to the right, the Mothership will move right one space.
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_Destroyer. 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 tha 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|
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 two new While rules. Use the picture below for guidance on creating these two new rules.
New While Rules Explanation: The first new rule creates a new local variable called "alien_move" that will help determine how the Aliens will move. We use the "game_start" variable to make sure that we do not always set "alien_move" to 12. Remember, a variable automatically has a value of 0 when they are created in Agent Sheets.
Why are we making "alien_move" equal to 12? Let's 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
The next rule we added executes every 1.2 seconds, which is also how fast the Alien swarm will move (see warning below). First we decrement (subtract one) from the value stored in "alien_move". Then we use the Make action to send a "move_aliens" message back to ourselves.
|Warning: Mothership and Swarm Movement Speeds|
If you make the alien swarm speed the same as the mothership movement speed you must combine the two rules, otherwise one of the rules will not run!
Having any two rules in one Method with the same IF conditions will cause only one of them to be used if all conditions are true. AgentSheets uses the first set of rule conditions it finds from top-to-bottom that are true and runs the actions, it then skips any remaining rules afterward.
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.
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.
|Warning: Rule Order!|
The rule order for the "move_aliens" On Method is important!
Try moving the "Is alien_move equal to 6" rule to the bottom of the method rule list and then run the game to see what happens.
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.
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 behaviors are a lot like the other Laser agent behaviors that we made previously. Please use the picture below to help you create the behaviors needed for the While Running loop.
Destroying Alien Bombs: We will sometimes need to destroy the Alien Bombs, so we will add an On Method that will react to the "destroy_bomb" message. Use the picture below to help create this method.
Explanation: Similar to how the player Laser moves UP, we want the Alien Bomb to move down, but only when it sees a Background agent underneath it. The Alien Bombs will also move slightly slower than the Lasers from the Laser Cannon, but you can adjust that if you would like. Destroying the Alien Bombs is handled exactly the same as for the player Lasers.
We now have a moving Alien Swarm and their Bomb attack, so let's give the Aliens the ability to shoot their Bombs and also make it so they can be destroyed by the player Lasers.
Adding the Alien Behaviors: Open the Alien behavior editor and add the following rules to the While Running section.
Explanation: The first rule in our While Running section should look familiar; it is almost exactly the same as our Mothership rule that makes the Mothership explode when it is hit by a player Laser. In this case, the Alien will explode if hit by a Laser and the depiction shown will be the Alien explosion we made earlier.
The second rule makes the Aliens fire Alien Bombs. Once every second, there is a five percent chance that an Alien that has a Background below it will fire an Alien Bomb. To make the Alien Bomb actually fire, the New action is used to create a new Alien Bomb agent.
GREAT WORK! You are now ready to test the Alien agents. Press the RUN button and try shooting the Aliens. What happens?
Updating Laser Cannon Behavior
As you discovered from the last Play Test, the Laser Cannon is not destroyed by the Alien Bombs. This will make our game too easy and not fun to play.
Update Laser Cannon While Loop: Open the Laser Cannon behavior editor and add a new rule to the While Running section. Use the picture below to setup this rule.
Create a new depiction for the Laser Cannon showing it exploding: We need a new depiction to represent an exploding Laser Cannon, after it has been hit by an Alien Bomb.
Add "destroy_cannon" Method to Laser Cannon Behaviors: Add a new method to the Laser Cannon and use the picture below to setup how it should behave.
Explanation: The new rule added to the While Running section for the Laser Cannon tells it what to do if it sees an Alien Bomb above it. The new method that we create actually makes the Laser Cannon explode, which is very similar to when an Alien or the Mothership explodes. An important difference is that when the Laser Cannon is destroyed a message is displayed to the player that the game is over and the worksheet is reset.
You are now ready to see if the Laser Cannon will be destroyed by Alien Lasers. Press the RUN button and allow an Alien Laser to fall onto the Laser Cannon. What happens?
If not, check the new rule and method you added and retest. If everything works great job!
Updating Laser Behavior
Currently the Laser and Alien Laser do not do anything when they collide. Let's have them both get destroyed instead.
Update Laser While Loop: Open the Laser behavior editor and add a new rule to the While Running section. Use the picture below to setup the this rule.
Explanation: First the Laser checks to see if an Alien Bomb is above it. If so, then it sends a message to the Alien Bomb to destroy itself and a message is sent back to the Laser to also destroy itself. This makes both the Laser and Bomb disappear when they meet!
You are now ready to see if a Laser and Bomb colliding will destroy them both. Press the RUN button and try shooting an Alien Bomb as it comes downward. What happens?
If not, check the new rule you added and retest. If everything works great job!
Deleting Alien Bombs
Currently when the Alien Bomb touches the Ground, it will remain there forever, but we want them to be destroyed.
Add Behavior to Ground Agent: Open the Ground behavior editor and modify the While Running method. Use the picture below for guidance on how to setup the rules.
Explanation: Once every 0.2 seconds the Ground checks to see if there is an Alien Bomb above it. By checking every 0.2 seconds this ensures that the Alien Laser does not appear to hang over the Ground too long. Once the Alien Laser has been seen we wait 0.1 seconds so that the player actually sees it hitting the Ground and then we erase the Alien Laser. We could have also sent a "destroy_bomb" message to the Alien Bomb!
Press the RUN button and watch as Alien Bombs hit the ground. What happens?
If not, check the rule you added and retest. If everything works, great job!
We have now created all of the agents and programmed all of their behaviors for this part of the tutorial. Let's have one final playtest to make sure everything is working correctly.
Play Test: Game Checklist
Press Run and see if everything works correctly. Check
If your answer to one of these is no, 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.), or 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!