Space Invaders Tutorial 1 Complex

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!

Creating a Game Master Agent: We will make one additional agent that will be our Game Master in our game. This agent will setup any global game variables that we need. Create a new agent for the Game Master and for the depiction put a black square in the middle. Additionally make the mask color white, so that this agent is transparent.



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 Game Master
We are going to make a cannon that can shoot three lasers at once. We will start with programming the Game Master agent behavior. For part 1 of the tutorial the Game Master has one function, to setup the amount of Lasers that can be on the screen at once. If the player could shoot an endless steam of Lasers the game would be too easy and probably not much fun. We will allow 3 lasers to be on screen at once.

Adding behaviors to Game Master agent: Open the Game Master behavior editor and add rules that match the picture below. Be sure to click Apply or OK when finished.



Explanation: By default, a variable used in Agent Sheets has the value zero. So, the first time we test IS "game_start" equal to 0, it will be true and this rule will run. We use a SET action to change "game_start" to be equal to 1, so that this rule will not run again in the future. We use an second SET action to set the global variable "@shots" to be three. We will use this global variable to prevent the Laser Cannon from shooting a constant stream of lasers in our game.

Programming Laser Cannon
We can now 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 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 message back to ourselves to "fire_laser".

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" Method behavior: The first thing that this method does is check to see if "@shots" is greater than 0. We do this to limit the number of Laser agents that the player can shoot from the Cannon and have on screen at once. As long as "@shots" is greater than zero we continue our rule and next we create a new Laser agent directly over our Laser Cannon. We do this with the NEW action, with a "dot" direction, and we specify the Laser agent depiction of what agent to create. Finally we use SET to decrement "@shots" by one, which means that we subtract 1 from the value held in "@shots".

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 Move action specifies that the Laser should move UP.

Deleting Player Lasers
During the play test, your Lasers should have gone up as high as they could in the Background area and then stopped. They did not disappear and after shooting 3 Lasers you could not shoot any more. We want to be able to shoot more than three Lasers total, so let's fix that now.

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. It is also a good idea to possibly put a temporary color in the depiction so that you know where it is placed on your worksheet.



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 stacked above or below it. In this case when it sees a Laser it Broadcasts the message "destroy_laser" to all the Laser agents. In a moment we will add behavior for "destroy_laser" to our Laser agent.

Adding new method to Laser: Open the Laser behavior editor and click the "New Method" button in the behavior editor window to create a new method. For the new On Method 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 first checks to see if a Laser is stacked above or below a Laser Destroyer agent. If so, we use the SET action to increment the amount stored in "@shots". Incrementing means adding 1 to the previous value of a variable. This will allow the Laser Cannon to shoot one more Laser. We then use the ERASE action to erase the Laser that received the message.

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



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 stacked above or below the Mothership; if there is, we destroy the Laser and change to the Exploding Mothership depiction. Finally, we Wait and then Erase the Mothership agent. The Wait action is used so that the player can actually see the Mothership exploding.

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.

Updating Laser Agent Behavior Again: When the Mothership Broadcasts a "destory_laser" message to the Laser agents nothing will happen, so we once again have to modify the Laser agent behavior by adding a new rule to our "destroy_laser" method. While this method is selected click the New Rule button to create the new rule and use the picture below as a guide for programming the new behavior.

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

Explanation: The Laser checks to see if it is stacked above or below a Mothership and if it is, it increments the "@shots" variable by 1 and then erases itself.

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 are three different types that you could include in your game. To have more than one type of Alien you just need to add a new depiction to the Alien agent.

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

Add Alien Agents to Worksheet: Add five rows of Alien agents to your worksheet, where each row has the same number of Aliens in it and the row starts on the far left side of the worksheet. After you are happy with your rows of Aliens be sure to save the worksheet. You can use the picture below as an example.

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

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

Alien Laser
Before we can program the Alien swarm, we need to create their laser.

Create Alien Laser Agent: Create a new agent and name it "alien_laser". We want this laser to look differently than the other Laser agent that we created, so that we can tell them apart when playing. Use the picture below as a guide for creating this agent.

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

Alien Laser Behavior: The Alien Laser behaviors are a lot like the other Laser 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" />

Explanation: Similar to how the player Laser moves UP, we want the Alien Laser to move DOWN, but only once every 0.2 seconds. This makes the Alien Lasers move slightly slower than the ones from the Laser Cannon, but you can adjust that if you would like. Later we will handle destroying Alien Lasers, but for now this is enough to get us going.

Alien Behaviors
In this part of the tutorial we are not going to make the Aliens move, however they will be able to shoot their Alien Lasers and also be destroyed.

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.

The second rule makes the Aliens fire Alien Lasers. Once every second, there is a five percent chance that an Alien that has a Background below it will fire an Alien Laser. To make the Alien Laser actually fire, the New action is used to create a new Alien Laser agent.

Updating the Laser behaviors again: Once again we have to add a new rule to our "destroy_laser" method. This time we check if the Laser is stacked above or below the Alien before erasing and incrementing "@shots". Use the picture below to setup this new rule for the Laser behaviors.

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

Updating Laser Cannon Behavior
Currently the Laser Cannon is not destroyed by the Alien Lasers. 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 the this rule.

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

Create a new depiction to the Laser Cannon showing it exploding: We need a new depiction to represent an exploding Laser Cannon, after it has been hit by Alien Lasers.

<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 Laser stacked above or below it. The new method that we create actually makes the Laser Cannon explode, which is very similar to when an Alien or the Mothership exploded. 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.

Updating Laser Behavior
Currently the Laser and Alien Laser do not do anything when they meet. Let's have them 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.

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

Explanation: First the Laser checks to see if an Alien Laser is stacked above or below it. If so, then it Broadcasts a message to all Alien Lasers to try destroying themselves. The "@shots" variable is incremented and then the player Laser is destroyed with Erase. We cannot use the "destory_laser" method here because the Alien Laser will be destroyed and if we checked to see if the Laser is stacked above or below it, that test will always fail.

Ground Behavior
Currently when the Alien Laser touches the Ground, it will remain there forever, but we want the Alien Lasers 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.

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

Explanation: The Ground checks to see if there is an Alien Laser stacked above or below it. Once the Alien Laser has been seen we Broadcast the "destroy_A_laser" message to all Alien Lasers, which will attempt to destroy the Alien Laser.

Deleting Alien Lasers
We have been sending "destroy_A_laser" messages to the Alien Laser, but we have not specified how the Alien Laser should behave when it receives this message. Open the Alien Laser behavior editor and click the New Method button. For the On message use "destroy_A_laser" and use the picture below to help make your Alien Laser behavior rules.

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

Explanation: The behaviors we setup for the Alien Laser are similar to the player's Laser. If the Alien Laser is stacked above or below a Cannon, Ground, or Laser we will erase the Alien Laser.

Shields
To make the game fairer, we will add in a Shield that will help protect our Laser Cannon from the Alien Lasers. The Shield can be destroyed by Alien and Laser Cannon fire.

Create Shield Agent: Create a new agent named "shield". We need two additional depictions for the Shield, which we can name "left_shield" and "right_shield". Use the pictures below as examples of how to make your Shield depictions.

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

Programming the Shield While Behaviors: Use the picture below to add the three rules to the Shield While Running loop.

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

Add Shields to the game worksheet: Now add some Shield agents to the game worksheet that we are making. You can use the picture at the top of this tutorial to help. Generally you will start with a Left Shield, next adding a Middle Shield section, and finally closing the Shield off with a Right Shield section. You will also want your shields to be at least two spaces above the level of the Laser Cannon. Save your worksheet when you are finished adding the Shields.

Explanation: The first rule sets up a "Health Point (HP)" variable for each Shield agent on the worksheet. To make the game simpler, we will give each Shield a "HP" value of 2. The variable name is only a description, but what we are going to do is allow each Shield segment to be hit two times before it is destroyed. Using the "game_start" value prevents the "HP" value from being reset to 2 each time the While Loop runs.

The second and third rules check for laser fire from either the top or bottom (Alien Laser or Laser). If a laser is stacked above or below, the appropriate message is Broadcast to the laser to destroy itself, there is a small pause with WAIT, and then a "update_status" message is sent back the Shield that detected the laser.

Updating Shield Status
We now need to program what happens when a Shield is hit by a Laser. To start we need to make three new agent depictions for the Shield agent.

Add new depictions to Shield agent: The three new depictions that we need will show the Shields after being hit by a single Laser. The Shields should look damaged in some way.

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

Programming the Shield "update_status" Method Behaviors: Add a new Method to the Shield behavior and use the picture below to program how the behaviors should act.

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

Programming the Shield "update_pic" Method Behaviors: Add another new Method to the Shield behavior and use the picture below to program how the behaviors should act.

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

Explanation: We make the extra depictions to show the player that their shields have been hit by their Laser of an Alien Laser. In this version of the game the shields can only be hit twice before being destroyed, but it would be easy to make them stronger.

The "update_status" method checks to see the current Shield condition (based on HP) and updates the HP as necessary and also calls the "update_pic" method to change the way that the Shields look.

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