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:

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!

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 "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.

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.



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.

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 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.

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.



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.



Mothership
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".

<br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

Aliens
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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

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!

<br style="clear:both" /> <br style="clear:both" />

Alien Movement
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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

Alien Bomb
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!!

<br style="clear:both" /> <br style="clear:both" />

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. <br style="clear:both" /> <br style="clear:both" /> <br style="clear:both" /> 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. <br style="clear:both" /> <br style="clear:both" /> <br style="clear:both" /> 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.

Alien Behaviors
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. <br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" /> 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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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.

<br style="clear:both" />

Updating Laser Behavior
Currently the Laser and Alien Laser do not do anything when they collide. Let's have them both get destroyed instead.

<br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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!

<br style="clear:both" />

Deleting Alien Bombs
Currently when the Alien Bomb touches the Ground, it will remain there forever, but we want them to be destroyed.

<br style="clear:both" />

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.

<br style="clear:both" /> <br style="clear:both" />

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!

<br style="clear:both" />

Finishing Up
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.

<br style="clear:both" />

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!