Text Menu Tutorial I

Since there are no GUIs in text games, text menus are the next best thing. This tutorial will give you the basics on this essential text game programming concept.

Text Menu Tutorial

Let's assume for a moment you are programming an adventure game. You are working on a shop. How would you allow the player to choose what he wants to buy? What about choosing a difficulty level? Character class?

All of these can be accomplished using a highly-complex natural language parser. However, the easiest way to do it is to make a simple text menu. A text menu is simply a list of options that the player can choose from. Let's take that character class selection again as an example.

What would you like to buy?

  1. Torch
  2. Sword
  3. Axe
  4. Zombie Paper

Enter the number of your choice
>

The above example illustrates the basic form of a text menu.

A. Question or description of the choice to make. In the shop example, this is What would you like to buy?. It is very important to let the player know why he is in this text menu in the first place. Here, it's obvious that he's in a shop.

B. A list of options. The player has to know exactly what choices he is making. In the shop example, the player immediately knows that he can choose to buy four items. In a a real shop menu, the price of the item would be listed along with the item, but this is a general purpose menu tutorial, so we'll save that for another tutorial.

C. A simple instruction that lets the player know how he is supposed to choose an item. In this case, he knows he can choose an item to buy by entering its number.

Printing the Menu

printing the menu is the easiest part. Here's an example :

print "What would you like to buy?"
print
print "  1. Torch"
print "  2. Sword"
print "  3. Axe"
print "  4. Zombie Paper"
print
print "Enter the number of your choice."

After this basic display, you also need to include input with a symbolic prompt (we will assumed you dimmed a string variable named answer at the beginning.

input ">", answer

Notice that a comma ( , ) is used rather than a semicolon ( ; ). This eliminates the question mark ( ? ) that would normally appear so only the symbolic prompt is seen.

Input Checking

This code would be fine if you expected everyone to answer perfectly the first time. But, as we are human, we make mistakes. Your program should expect them. Your program should also expect deal with little snots who purposefully enter the wrong choices to see if you programmed your game correctly. Therefore, we have to check the input to make sure that it is the kind of input our game expects. In the case of the menu above, it expects an answer between 1 and 4.

Checking Range of Input

You don't have to check every valid response from the player separately. You can check to see if an answer is valid by checking a range of input. The game needs to look for a response between 1 and 4 so we do exactly that :

if answer < "1" or answer > "4" 'then the answer is not valid

If the player types in anything other than 1, 2, 3 or 4, the game will know that the answer given is not valid. [ 1 ] The question now is what to do when the player gives a non-valid answer.

Good Old Goto

Rather than setting up a more complicated loop, you can use goto and a label to loop back to the beginning of the menu :

dim answer as string

menu:

PRINT "What would you like to buy?"
'lines omitted
Input ">", answer

if answer < "1" or answer > "4" then goto menu

Now the menu will repeat until the player gives a valid answer. Be careful with this approach, however. Make sure you use a different label with every different menu in your game. Otherwise, FreeBASIC could become very confused when you use goto.

Letting the Player Know

As it stands, if the player enters a non-valid answer, the game will repeat the menu, hoping to get a valid answer. The game is happy, but the player might be a bit confused. If he thinks he entered a valid choice, then he would wonder why the menu repeated. After detecting a non-valid answer, you have to let the player know.

So, instead of

if answer < "1" or answer > "4" then goto menu

all we have to do is expand this into a multi-line if.

if answer < "1" or answer > "4" then
    print:print "Please enter a choice between 1 and 4"
    print
    goto menu
end if

Getting on With The Game

You now know how to display a menu presenting the player which a number of choices, get a valid answer, and tell the player when he as entered a non-valid answer. You can be confident that the player will be forced to give the right kind of answer. You can now check his answer and act upon it with multiple ifs, or (recommended), a select case statement.

Notes

[1] You may find it strange that we are checking a range of strings like this. We can do this because strings are compared by their ASCII value. 1 to 4 exist in a straight line in the ASCII standard.

If you are not comfortable with this, you can covert the answer to a number using val() and compare it that way.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.