Designing Excetris

I constructed Excetris from an Excel worksheet and added the code to a standard module, but the program could just as easily be entered into the code module for the worksheet—take your choice. The worksheet cells that define the game board must be square and will match the size of the individual squares in a game shape. The game can easily be initiated from a form button or Command Button control by attaching the form button to a public procedure, or calling the same procedure from the Click() event of the Command Button control.I could also initiate the program with a Shape object and assign a procedure with the Assign Macro dialog shown in Figure 10.7.

While considering the game's design I focused on three major problems unique to Excetris.

• Creating and adding the different shapes to the game board.

• Rotating and moving the shapes left, right, and down.

• Tracking the location of each shape on the game board so they can easily be removed when required.

Creating Excetris Shapes

The program will use just the five shapes shown in Figure 10.11, but the program should be written to make it relatively easy to add more shapes later.

(Figure iq.ii)

The five shapes used in the Excetris game.

Each of the five shapes used in Excetris are built from four distinct Shape objects (msoShapeRectangle) that are positioned as shown in Figure 10.11. To make it easier to manipulate the four Shape objects as if they were a single shape, the program will include a custom data type that defines the properties of an Excetris game shape. The elements of the custom type will include the following:

• An integer between 1 and 5 that defines one of five shape types shown in Figure 10.11. The value of this element will be randomly generated making it easy to choose the next shape that is added to the top of the game board.

• A decimal value that defines the line weight of each Shape object. The value of this element sets the border thickness around each square in the shape.

• A long integer that defines the fill color of each Shape object. Colors will make the shapes more interesting. All four squares in a shape will have the same color, but that color will be randomly selected.

• A Range object that defines the location of the active shape relative to the worksheet cells it masks. This range maps the shape to the worksheet and is critical for tracking the shape's location as it moves down the game board.

• A decimal value that defines the size of each Shape object. Each of the four Shape objects is square so its size will be set to either the width or height of a cell in the game board. The size of each square exactly matches the size of the cells in the game board to make it easier to keep all of the shapes aligned.

• A Boolean that defines whether or not a newly added shape overlaps with an existing shape on the game board. The value of this element will be used to decide when the game is over.

Moving Excetris Shapes

You will notice from Figure 10.11 that each shape is built from four identical squares. As stated earlier, each square is a separate Shape object, but the program will have to manipulate these four squares as if it were just one shape. One option is to group the objects using the Group() method of the ShapeRange object. I decided against this option because of how

VBA sets the axis of rotation for some of the shapes shown in Figure 10.11. For example, consider the shape shown in Figure 10.12 and what happens if the four squares are grouped and rotated counterclockwise 90 degrees.

(Figure 10.12)

Rotating a grouped shape 90 degrees.

(Figure 10.12)

Rotating a grouped shape 90 degrees.

You will notice that the shape on the left starts with all of its squares directly above a worksheet cell; but after it's rotated counterclockwise 90 degrees (resulting in the shape on the right), each square is offset from the cells below it. This offset causes a problem because the shapes must maintain vertical and horizontal alignment with all other shapes on the game board. Even though it would be relatively easy to programmatically group shapes and move them, it is not as easy to compensate for the offset that results from rotating the shapes with less symmetry. I will, therefore, leave all four squares as separate Shape objects, but move them in a way that gives the illusion of one shape.

To preserve the shapes' vertical and horizontal alignment, I am going to use the Left, Top, Width, and Height properties of the worksheet cells below the squares. The active shape is moved by incrementing or decrementing the Left and/or Top properties of each of the four Shape objects depending on the required direction.

The new position of the active shape must be validated before moving the shape. To be valid, the new position must be entirely contained within the game board and there must not be any other squares occupying any part of it.

The downward movement of a shape is controlled by repeated calls to the same procedure set up with the OnTime() method of the Application object. This procedure must move the shape down one row each time it is called. Moving the shape to the left, right, and all the way down the game board is controlled by the player. The OnKey() method of the Application object can be used to assign a procedure to a keystroke. This allows the player to direct the movement of the active shape using the keyboard.

Removing Shapes

As shapes are added to the game board they will have to be assigned unique identifiers so they can be removed at a later time. The four Shape objects that make up the active shape will always be assigned the same name. These Name properties of each Shape object are changed to include the address of the worksheet cell they mask when the active shape comes to a rest. For example, the game board shown in Figure 10.13 includes a total of eight Shape objects. The four Shape objects that make up the active shape are assigned the names Squarel, Square2, Square3, and Square4. The four Shape objects that have come to a rest have been assigned names that include the cell addresses SquareE16, SquareE17, SquareF16, and SquareF17.

(Figure 10.13)

Using names to track the Shape objects added to the game board.

(Figure 10.13)

Using names to track the Shape objects added to the game board.

Shape Names:

Square1, Square2, Square3, and Square4

Shape Names:

SquareE16, SquareE17, SquareF16, and SquareF17

Shape Names:

Square1, Square2, Square3, and Square4

Shape Names:

SquareE16, SquareE17, SquareF16, and SquareF17

In addition to using the cell addresses in the Shape object's name, each cell masked by a Shape object will be assigned an x to its Value property; thus, when the game board is scanned, any row whose cells all contain an x are known to be completely masked by Shape objects. Furthermore, the location of each Shape object is easily identified because their name contains the address of the cell they mask—making it easier to delete them from the game board when required.

Program Outline

When playing a game, the Excetris program should proceed as outlined in the following:

1. A randomly generated shape appears at the top of the game board.

2. The shape moves down one row on the game board every second.

3. The player moves the shape to the left right or as far down the game board as possible using various keystrokes. The player can also rotate the shape counterclockwise 90 degrees with another keystroke.

4. When the shape can no longer move down the game board, it stops and another shape appears at the top of the game board.

5. If the player successfully positions the shapes such that a row or rows in the game board are completely masked by shapes, then the shapes are removed from the game board, the score is updated, and the other shapes above the deleted row(s) are moved down.

6. The game continues until a new shape added to the game board overlaps with an existing shape.

Biorhythm Awareness

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook


Post a comment