Classic Computer Magazine Archive ANTIC VOL. 7, NO. 12 / APRIL 1989

Adventure Works

Easy Way to Prgram Text Adventures

By David Woolley

Adventure Maker is a clear tutorial that shows new programmers how to create their own text adventures. It includes a short demonstration adventure program which can be easily adapted to your own original adventures. This BASIC program works on all Atari computers with at least 32K memory, disk or cassette.

For a novice programmer, getting adventures from your Atari can be difficult. But with a short overview of the necessary elements, text adventure games can really be written with ease.

This article includes Escape From Barnaby's Isle, a simple demonstration adventure designed to illustrate how the pieces of the puzzle fit together--and to provide programming routines and structures that you can adapt for your own adventure creations.

GETTING STARTED

Type in Listing 1, BARNABY.BAS, check it with TYPO II and remember to SAVE a copy before you RUN it.

Programming a text adventure is straightforward. The program must READ information from a list of DATA, display that information, INPUT the player's action, then perform that action and display the results. To get an idea of how it all comes together, I suggest you first play Escape From Barnaby's Isle.

The object of this game is to escape from the island. The list of commands you can use is very short-GO, GET, EXAMINE, INFO, and USE. You should be able to solve the game easily.

After you complete the game, LIST it on screen and look at it again. The most frequently used variables, strings, and arrays are N$(nouns),V$(verbs), O$(objects), O(objects carried), F(flags), D$(descriptions), C$, X and Y.

WRITE AN ADVENTURE

Writing your own text adventure program will require a lot of thought. First think of a setting and a final objective. The setting might be a haunted house, the English countryside or an alien planet.

The objective is something that the player must do to win the game (just "adventuring" or exploring can get pretty boring). The setting for Escape From Barnaby's Isle is a deserted island, and the objective is to leave the island.

Next, imagine items and locations that fit in with the setting. If the setting is a forest, you might find an axe in a grove of oak trees, or mushrooms by a dead tree stump.

After you create some interesting designs for your game, draw a map of the area in which the game takes place. Familiarize yourself with the world you just created, and then draw what I call a "plot" map.

A plot map is drawn on a graph so that each location can be described by ordered pairs. For instance, in Barnaby's Isle, the North Beach location is at (3,5).

The plot map has two axes, based on compass points. North and south are on the X-axis. East and west are on the Y-axis. When the program goes through the DATA lines in the routine at lines 15-35, it finds the ordered pairs.

At the start, the player begins at location (3,1), or X= 3 and Y=l (see line 10). If the player goes north, the program adds one to the Y variable, making it 2. The program then finds the data on location (3,2), the Crossroads.

If the player goes west, the value of X increases--eastward, X decreases. These calculations occur in lines 115-140.

Now you must draw three lists, one for verbs, one for nouns and one for flags. These lists may vary in length, depending on the size of your game.

Having a variety of verbs in your program adds interest and challenge. USE is too general, but I put it in Barnaby's Isle just to demonstrate how the subroutine to manipulate objects works. Try being more specific--instead of USE SPADE, you might try DIG HOLE.

INFO is a one-word command. Normally, a one-word command will cause an error in this program, unless you insert a line telling the program to go elsewhere. This is done at line 111.

Your noun list will probably be much longer. Nouns used in Barnaby's Isle are RAT, BOOK, CUTLASS, APPLE, TALISMAN, CHEST, BOAT, TROLL, TREE and SHACK. Some objects can be picked up, some can be used and all can be examined.

Whenever a player GETs an object, a corresponding variable in the 0( ) array is set to 1. You can see how this works in line 170, part of the GET routine :

170 IF N$ = "RAT" AND X=5 AND Y=2 AND O(1)=0 THEN O(1)=1: GOTO 200

Now the progrram can easily determine if the player is carrying the rat by checking the value of O(1). If he has the rat, O(1)=1. Otherwise, O(1)=0.

The Flags List, contained in the F( ) arrray, shows the condition of the flags that might change during the game. These include whether or not a door has been closed or a magic rune has been read. Such things are vitally important to keep track of.

Barnaby's Isle has eight flags. Each may be switched on by placing a '1' into the corresponding slot of the F( ) array. For instance, when you EXAMINE CHEST, flag 6 is switched on (LET F(6)= 1) then the program prints the appropriate response.

Now, whenever you type INFO, the program will check to see if flag 6 is on. If so, the program will display what was described inside the chest.

PROGRAM TAKE-APART

Lines 5-7 contain the title page and determine whether or not to disable the [BREAK] key. Text adventures normally require a lot of typing, so it's always a good idea to disable the [BREAK] key to keep the user from stopping the program with an accidental keypress.

If you want to use the [BREAK] key, on the other hand, just hold down the [SELECT] key when you type RUN and [BREAK] will be enabled.

Line 8 sets the graphics mode and changes the background colour to black. Lines 10-12 DIMension the strings and arrays to be used.

Lines 15-35. This routine takes the player's current X and Y coordinates and searches through the DATA statements to find the rest of the information for that location. Then it reads the description (D$), object (O$), and directions in which the player can travel (N$).

The DATA lines (10000-10014) are important, so I will show you how they work using the South Beach location as an example. This information for this location is in line 10000:

10000 DATA 3,1,SOUTH BEACH.A BEATEN TRACK LEADS NORTH. TALL CLIFFS LOOM UP ON EITHER SIDE., (heart),NOOO

Here, 3 and 1 are the respective X and Y coordinates of South Beach. The description follows. Here, the description is: SOUTH BEACH. A BEATEN TRACK LEADS NORTH. TALL CLIFFS LOOM UP ON EITHER SIDE.

Next, there is a list of objects found there. If there are no objects, just use a single "heart" character (a [CONTROL][, ]).

The object's name and the noun don't have to match exactly. Here, the object's name can simply be the phrase used to describe the object. The noun is the word as found in N$, which is the form the player must use. As an example, the object in location (2,3) is described as a MAGIC TALISMAN. To pick it up or use it you must use the specific noun, as in GET TALISMAN.

Finally comes the direction code. This is a short string which shows the directions in which the player can travel. Directions are entered in the order north, south, east, west (NSEW). If the player is not permitted to go in a certain direction, that direction is represented by an O. In the above exmple, the string NOOO means the player may only travel north. In the string NOEO, however, the player can travel only north and east.

Lines 40-54 display all data about your location. Line 42 jumps to a subroutine at 425 which checks the O( ) array to see if there is an object at the player's current location, and whether or not the player has that object. If O$ is one character long, the subroutine renames O$ to NOTHING. The exception is the apple. There will always be fruit on the apple tree.

Lines 51-54 print all the directions in which the player can travel.

Line 55 prompts the player for the next command, then places that command into A$. Line 60 jumps to a subroutine at 95 which will break A$ into two parts--the verb (V$) and the noun (V$). Line 111 checks to see if V$ is INFO, a one-word command. If A$ contains only one word, the routine RETURNs to avoid an error. Otherwise, it extracts the noun, N$, to complete the command.

Lines 65-90 direct the program to the correct subroutine indicated by the verb.

Lines 112-130, the GO routine, calculate the player's new position, and then return to 15 to READ new data.

Lines 170-200 contain the GET routine. A player may pick up an object only if all of the following criteria are met:

--The object exists and can be picked-up.
--The player is in the same place as the object (their X and Y coordinates match).
--The player has not yet picked it up. (If O(x)=0)

Once picked up, the object's corresponding flag in O( ) is set to 1.

Some objects that cannot be picked up have specific responses here, too. If the noun is unknown or incorrect, the location is wrong, or the object is already being carried, the bell sounds and an error message is displayed.

Lines 205-207 contain the USE routine, which checks the objects you can USE to make sure they are being carried or are at the current location.

Then, depending on the object in question, a flag might be switched on (as is the case with RAT, BOOK and TALISMAN), or a part of O( ) is switched off, as in the case of the APPLE. Once the APPLE is USEd (eaten) you no longer have it.

The results are then printed, and the program returns to line 55.

Starting at Line 235, EXAMINE is the biggest subroutine, yet it is one of the simplest. It uses all the nouns (some of them more than once) and is mainly there to give players extra information.

Each object you can pick up is checked twice, once to see if it is being carried, and then to see if the player is in the correct location. Objects that cannot be picked up are checked only once. The exception is TREE, which must be checked three times to determine which of the three trees is being examined.) The results are then displayed, flags set in some cases, and then the program goes to the prompt WHAT NOW? at 55.

The INFO routine is next, in lines 235-317. It displays a list of all the useful verbs, then a list of all objects held. Finally, it prints a list of clues found by the player, and returns to line 55.

The final part is at 475-520, the old WON! routine, often accompanied by a system of scoring. In Escape From Barnaby's Isle the player gains two points for every flag switched on, and four points for every object.

ADVENTURE CONTEST

Now that you have some idea of how text adventure games work and have seen how the elements are coordinated, you can start writing your own using this as a guideline. Your games can be as simple or as complex as you're willing to make them. Remember, adventure games are not limited so much by your computer's memory, but by your own imagination.

Antic will publish as disk bonuses the best short adventures created with this program structure. Adventures must be written in standard Atari BASIC and must be able to RUN on a 48K computer.

David Woolley is a student from New Zealand. This is his first appearance in Antic.

Listing: BARNABY.BAS Download