Space Invaders Tutorial 3

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!

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.

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.

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, AND ONLY 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.



'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?

 BE SURE TO 'RESET' THE WORKSHEET AFTER YOU FINISH TESTING !!! 

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.



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.

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:



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

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 behavior for the While Running loop.



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.





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.

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!

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.



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.

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.



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.



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.





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

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.


 * [[Media:Space Invaders Tutorial Basic_3.zip|Click here to download a project that follows the tutorial.]]