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 7 Section 2

Blocks and Pieces

For the purposes of this chapter, a "block" is defined as a single square element on the screen. A "piece" is a collection of blocks that fall together from the top of the screen. Blocks never fall by themselves. They are always a part of a piece.

To make these pieces, we will use a list that contains the positions of each block in a piece. Here is an example:


[[0,0],[0,-1],[1,0]]

Each small list contains the position of a block. The position [0,0] means that the block will be located at exactly the center of the piece. The [0,-1] means that the block will be one space up from the center. The [1,0] means that the block will be one space to the right of the center.

In addition to this description, we also have to provide descriptions for each of the orientations of the piece. The user will be able to turn the pieces in 90 degree increments, which creates a potential of four descriptions that we need per piece. Some shapes will need only two positions, if their third and fourth orientation is the same as the first and second. Some pieces look the same no matter what the rotation, so we will only need one description.

Timing the Fall

Each piece should fall at a certain rate. We could have the piece drop one space every frame, which would allow us to set the drop rate by simply setting the movie's tempo.

However, if we lock the drop rate to the tempo, we are also restricting the number of moves that the user can make over time. They will only be able to move the piece one space to the left or right for every space it drops.

A better way to do this is to use a timer to determine the frequency of drops. Then, you can have the tempo set very high, and allow the user to move left and right as much as he or she wants in any given period of time.

The way a timer works is that every time a drop occurs, a property variable is set to the time when the next drop should occur. Then, once per frame, the timer is checked to see if it's time for the next fall. On fast machines, hundreds of frame loops could go by before the timer triggers a drop. On slow machines, only a few frame loops may occur. No matter how fast the machine is, the drops should happen at a constant rate.

Keyboard Input

Instead of using the mouse for user interaction with this game, we will use the keyboard. There are several ways to take keyboard input in Director.

One is to use the on keyDown and on keyUp handlers. They work like on mouseDown and on mouseUp. The only difference is that the key's value is present in these handlers as both the key and the keyCode. The first is the actual character of the key pressed, and the second is a special set of values for things like arrow keys.

At least four keys are needed for this game. The first two are the left- and right-arrow keys, which will be used to move the pieces left and right. Keys are also needed to allow the user to rotate the pieces left and right.

Special Effects

Because this game is more complex than most in this book, and because there is a lot of code, I've left out sounds. However, a Lingo programmer should have no problem finding the right spots to add the puppetSound command. Examples of using sound in games can be found in most other chapters.