Designing the Math Game

The program interface is built from a single Excel worksheet. The worksheet is formatted with colors and a large font to make it easy for the user to see the questions. The macro recording tool is activated while formatting the worksheet in order to save most of the interface design as VBA code. ActiveX controls (Option Buttons and a Command Button control) are drawn on the worksheet in a convenient location to provide the user with a selection of mathematical operators, and an easy way to start the program. The Math Game worksheet is shown in Figure 4.7.

The timer* The question*

The timer* The question*

The Math Game worksheet.

Option Button controls

Command Button control

The Math Game worksheet.

Option Button controls

Command Button control

User's answer

The only input required by the Math Game program is the user's answers to the questions as they are entered from the keyboard. The program must make it convenient for the user to quickly enter his or her answers in the required worksheet cell, so the program must keep the answer cell selected through the duration of the game. This can be accomplished pro-grammatically by selecting the cell when the user starts the game and setting the direction in which the selection moves after Enter is pressed on the keyboard (see Tools, Options, Edit, and the Move selection after Enter Check Box from the Excel application).

Program outputs include a timer written to a worksheet cell that counts down from 60 seconds, and the questions and score of the user's game. VBA contains an OnTime() method of the Application object that can handle the program's timer. The questions and answers can simply be written to the worksheet. Arrays are convenient tools for storing the questions and answers as the game is played.

As with previous programming projects, the program code can be entirely contained within the object module for the game's worksheet. The program must be initiated from the Click() event of the Command Button control. Other programming tasks will be assigned to various event, sub, and function procedures in order to properly compartmentalize the program.

The Math Game program is considerably more complex than the first three projects in this book; therefore, as you might expect, it's going to be longer. With slightly longer programs, I typically write a brief outline of the tasks that need to be accomplished based on the requirement list. Generally, the outline defines the sub and function procedures I need to write for the program. The Click() event of the Command Button control will serve as the main procedure for the program, looking very much like a program outline with procedure calls that follow the flow of the program. Other event procedures that are needed include the Click() events of the Option Button controls that are used to set the operator for each question. The project outline follows:

1. Format the worksheet (record formatting)

2. Add ActiveX controls and set their Design Time properties

3. Disable the ActiveX controls (sub procedure)

4. Clear the results from a previous game (Click() event of Command Button control)

5. Initialize variables (Click() event of Command Button control)

6. Select the answer cell (Click() event of Command Button control)

7. Get operands for the question (sub procedure)

8. Get an operator for the question (sub procedure)

9. Start the program timer (sub procedure)

10. Collect the user's answers and repeat steps 7 and 8 (Change() event of the worksheet)

11. Disable the timer (use the same sub procedure that starts the timer)

12. Enable ActiveX controls (use the same sub procedure that disables the controls)

13. Clear the game board (sub procedure)

14. Score the user's answers and write the results to the worksheet (Click() event of Command Button control)

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