"The Same Game", Part 3 of 5: Adding Menus to Control Difficulty Levels

By Ben Marchant

Adding Menus to Control Difficulty Levels

Welcome back! With our playable game in hand, we are now ready to polish off the game. In this article we'll focus on adding customizability to the game through menus, including adding difficulty levels. To do this, we'll update the menu that was included by the MFC Application Wizard, add new commands and set up event handlers for them. After some practice, the game with only three colors is pretty easy to solve so in this article we'll add new levels of difficulty. We can do this by simply adding more colors.

Menu Options

Adding a new menu option is done through the resource view. You can bring up the resource view from the View Menu in Visual Studio and then, Other Menus->Resource View or the keyboard shortcut of Ctrl+Shift+E. This will bring up a window similar to what you see below.

Open up the menu in the menu editor by double-clicking on the IDR_MAINFRAME option under Menu. The menu editor will allow you to add, remove and edit menu options. Take a look at all of the menu options that are already included. Most of these options aren't useful to our particular application so we'll delete a few. Click on the file menu and it will drop down the actual menu. Delete all of the options except for "New" and "Exit" as shown in the image below. Do this by clicking on the option and hitting the delete key.

Next we'll examine the Edit menu. In the very last article we'll discuss how to create an Undo/Redo stack for the game so let's add the redo option to the edit menu and delete all of the others except for undo. Once you've deleted the unwanted options, click on the "Type Here" area in the Edit menu and type in what you see in the graphic below.

I'll explain what each part of the string "&Redo\tCtrl+Y" means. First of all the ampersand signals to the menu what the hot-key is for that menu option. The hot-key is the character immediately following the ampersand, R in this case. This will allow you to press Alt+E to open the Edit menu and then press R to select the Redo option. The \t should look familiar; it is the escape sequence for the tab character. The tab character in a menu puts a tab between the name of the menu option and the accelerator key. Look at the Undo option to see the tab in action. The accelerator key is a set of key strokes that will accomplish the same task as clicking on the menu option. We'll use the Windows standard Ctrl+Y for redo. We'll add the event handler and supporting code in a later article.

Adding a new menu is as simple as clicking on the "Type Here" at the top in the menu bar. Type in "&Level" to add the Level menu. After hitting Enter you can move the menu to the left of the Help menu by just clicking on the menu and dragging it to the left. Now let's add in the menu options. We want to allow the user to select different levels from three to seven colors. Add in the following five menu options seen below. The first one contains the text "&3 Colors" and so on.

Let's go ahead and add the menu for the next article too. So add the "&Setup" menu and move it to the left of the Help menu. Then add the "Block &Size..." and "Block &Count..." options. Notice the three periods after the option name, that is a standard for menu systems to indicate that there will be another window to popup. We'll work on that in the next article.

Continue to page 2: Implementing Difficulty Levels