Note About This Book: Advanced Lingo For Games was written by Gary Rosenzweig in 2000 for users of Macromedia Director 7. It is presented here for free on an as-is basis, with no updating. Most of the information and code here can be used in the most recent version of Director. The book has been reproduced from the final editing files archived in 2000, and not the final proof galleys. So some minor differences between this version and the printed version my exist. The entire contents of this book are Copyright 2000, Gary Rosenzweig. No part may be reproduced or copied without written permission. The text here is provided for individual use only.
Want to thank me for making this book available for free? Just buy Special Edition Using Macromedia Director MX and we'll call it even!

Advanced Lingo For Games
by Gary Rosenzweig


Chapter 10 Section 2

Game Overview

The basic idea of the game is to have a formation of invaders march down the screen. They go right and then left, dropping lower each time they change direction.

The player, on the other hand, remains vertically stationary at the bottom of the screen. He or she can move the "ship," "gun," "turret," or whatever you want to call it, back and forth, and fire bullets at the oncoming invaders.

The game ends when either the invaders reach the bottom of the screen, or fire back at the player's ship, hitting it. Figure 10.1 shows the game at its start.

Figure 10.1
The screen at the start of a Sprite Invaders game.

Our example movie uses three different invader graphics, and the screen is arranged with a row of each.

Ship Movement

The ship in this game is controlled by the keyboard, rather than the mouse. When the mouse is used to control such an item, like in Chapter 8, "Falling Objects," the player has the ability to move the item across the screen very quickly. In fact, if the user pulls the mouse to one side or the other fast enough, it is really jumping from one location to the other without passing through any intermediate points. In addition, it is easy for the player to have their cursor positioned off of the game, where a mouse click will simply react with the computerŐs desktop rather than the game.

Keyboard control means that the ship must move one step at a time. This is important in this game because a major factor is to avoid the falling bullets of the invaders.

We will use the left- and right arrow keys for movement. We will also restrict the ship to staying at least 20 pixels away from the left and right sides of the screen.

Invader Movement

The invaders primarily move horizontally. When one or more invaders comes up against a side of the screen, all the invaders reverse direction. At the same time, they drop down the screen a little. Thus, they approach their goal by staggering.

An important factor is that all the sprites must move in unison. So, when a single sprite determines that it is too close to a wall, it must send a message to all the sprites to drop and start moving in the other direction.

A Lot of Bullets

The player should be able to fire bullets at a certain rate. Certainly this rate needs to be limited, or the invaders would never stand a chance. However, the player should be able to fire fast enough to have several bullets travelling on the screen at once.

This means that we cannot simply have one bullet sprite. Instead, we need to have a bank of bullet sprites.

By using a clever behavior, we can make these sprites work together to determine which sprites should be used when a new bullet is fired. The first sprite in the bank is always asked first: "can you fire?" If the sprite is not being used, it fires and begins its movement. However, if it is already moving, it passes the message on to the next sprite. This sprite then either fires or passes the message on.

After a sprite reaches the top of the screen, or it hits an invader, it becomes inactive and is ready for the next time it is asked to fire.

Using this method, all we need to do each time a bullet is fired is ask the first sprite in the sprite bank to fire. From then on, the behavior assigned to this sprite and the others after it will pass the message along until a sprite takes on the responsibility of being the bullet.

We use this same strategy for the bullets fired by the invaders. All you need to do is to make sure that there are enough sprites available. For instance, if the player holds down the spacebar, how many sprites will be needed to display the maximum number of bullets that can appear at once? Figure this out, either by math or by trial-and-error and you have your answer.

Levels

This game requires more than one level. A novice user may not be able to destroy all the invaders before getting killed or before they land. However, most people will be able to clear the board. What then?

The second level can have faster invaders. Maybe there will be more of them, too. You could even have them in different patterns.

We can set this game up to support as many levels as you want to build. If a player clears the board, the movie goes to another frame for the next level. The sprite behaviors there are set to make that level more difficult.

Lives

In video games, you almost always get more than one life. That would certainly be expected in a game like this one.

When the player is hit with an enemy bullet, they lose a life. In addition, the game should pause for a second to allow the player to catch their breath. Then, the level should start over.

However, if the invaders land, the game should be considered over, even if the player has never died. You might decide otherwise, but the example on the CD-ROM works this way.

Special Effects

At least three sounds are needed. The first is for when the player fires a shot. The second is for when a bullet hits an invader. The third is for when an enemy bullet hits the player's ship.

In addition to the sounds, a little animation is traditional in a game like this. The invaders can switch between members as they march across the screen. To do this, you will need two members for each invader.