SpaceInvaders Tutorial 1

Overview
The purpose of this tutorial is to build a Space Invaders style game. By the end of this tutorial you will have created a playable game that resembles the picture above. Throughout this tutorial, if you are confused as to how your game should look, you can use the above picture for guidance.

The game you will create in this tutorial is modeled after the classic shooter game ‘Space Invaders.’ Tomohiro Nishikado originally created the game 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. The alien swarm will not move in this version, but they will shoot lasers downward, attempting to destroy the cannon. To make the game easier, there are shields that the player can hide behind, but lasers will destroy the shields after it is hit a few times. In our version the flying saucer will always be on the screen and will move randomly left or right. We will also 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. 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 rest 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. Now place one Game Master agent on top of a Ground agent. Finally place one Laser Cannon just above the Ground agents. Use the picture at the top as a reference.

Programming Game Master
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
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: 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 has two functions. First, 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.