Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / [Tutorial] A Simple Structure for a Text-Adventure

Author
Message
BMacZero
16
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 18th May 2008 05:54 Edited at: 28th May 2009 16:50
Tutorial: Writing a Text Adventure
(Edited 4/28/09)

I decided to write this tutorial in anticipation of the annual text-adventure competition, which I have become a great fan of. Text adventures are a good start for a new programmer to learn about strings, arrays, and structures; but they can also provide very good experience for the knowledgable programmer in story writing and text manipulation.

What's in this tutorial? It will guide you step-by-step through the process of creating your very own text adventure, outline a good program structure for it, and hopefully teach you something new about string manipulation or program structure.

To better understand this tutorial, you should have at least a basic knowledge of arrays and variables, as well as basic program structures like if statements and subroutines. Please check out the Tutorials thread at the top of the Newcomers Board if you don’t know what I’m talking about.


What is a Text Adventure?

A text adventure is a game made almost entirely of text (duh). Usually a good text adventure uses a static HUD or some kind of background for show, but the real meat of the game is in the text. As the player moves to different locations, the game displays a paragraph or two describing the player’s surroundings. Then, depending on what type of text adventure it is, it will do one of two things:

1.) It will ask the player to type what they want to do. Then it will search that input for keywords to determine what the player wanted to do, and send them to the appropriate area.
2.) It will ask the player to choose an action from a short list, and send the player to the appropriate area.

Both types have advantages and disadvantages that will be discussed later.


Step 1: The Game Idea

Before you even think about writing any code, it’s a good idea to have a storyline and at least a basic plot summary (this is a good idea for any kind of game you're making). You can use a sheet of paper to make out the story in a sort of upside-down tree, like this:



Or, if you are more of a visual person, it might be a better idea to make a map like this:



Obviously you would be much better off doing this on a sheet of paper than in Notepad.
It doesn't really matter how you do this, but DO IT. Know where your character is going to go, what he needs to do, if there will be NPCs, or battles. Oh, and don't do a medieval fantasy game unless you're going to add something really creative to it .


Step 2: Program Structures Part 1

Ok, now is when we get into the code portion of the text adventure. First, you have some choices.

The first choice you need to make is whether to make a "parsing" text adventure or an "options" text adventure (these were discussed briefly above):

1.) A parsing text adventure lets the player input a word or a sentence about what they want to do, and then tries to determine what they wanted by searching for keywords.
Advantages:
- Easier to add more options for the player without having to take up lots of screen listing them.
- Allows you to more easily hide secrets. For example, if the key is under a rug, the player would have to actually think of that and type "Look under rug" rather than just seeing the "Look under rug" option and choosing it.
- Gives the player more of a sense of freedom, even if half the commands they enter return "Your command was not understood."
Disadvantages:
- Harder to program effectively
- Takes more insight into the player's mind - you have to anticipate many of the things they might type in or they may get frustrated that the program doesn't understand them.

2.) An options text adventure gives the player a list of options to choose from.
Advantages:
- Easier to program
- Requires less time then entering long lists of keywords into your program
Disadvantages:
- Player may feel restricted
- Can't hide secrets from the player

You should take several factors into account when making this decision.
1.) How much time are you willing to spend on this? If you're just doing it to have some fun and/or you aren't going to have a lot of time to dedicate to it, you should probably go with an options adventure.
2.) Take the number of years you've owned DarkBASIC and divide that by the number of projects you have completed. If the answer is greater than 0.8, do an options adventure . Parsers can be kind of boring and you'll probably give up if you try to make one.
3.) If you had to open the Windows calculator or fetch your own to complete step 2, leave now (j/k)
4.) Think about what style best suits your game. If you want to make the player discover things on their own and spend time uncovering clues and such, I recommend a parser.
These are just some guidelines to get you on the right track, if you have other, better, reasons to go for one method or the other then go for it.

Step 3: Program Structures Part 2

Now you must decide how to structure the text adventure. As with any program, there are many, many different ways to go about the program's structure and the ways it accomplishes things. This is the best method I've managed to come up with so far:


*SEE FOOTNOTE

So what's going on here? The array Texts() holds a description each room (PrintStatements), a list of options presented to the user (Option1-3), and where each of those options will take them (Option1-3Trgt) - that is, which index in the array will be displayed next.
This method is great because the ENTIRE game can consist of one array, and all you need is one simple, little loop to actually run through the game:



Very, in my opinion.


Step 3: Shiny!

After completing Step 2, your text adventure will work. It may even be fun and interesting to play. But there are a few more features you may be wanting to add to avoid just having the same simple game as everyone else. Here are some ideas:

1.) Items
Depending on your game, you may want to award the player items, or allow him to buy them. This is easy with the above method. Just add some more to the TextStorage type, and DIM an array for items:



In the code above, the array Items() will have one spot for each different item there is in the game. For example, if spot 1 is for money and spot 2 is for arrows, when the player buys an arrow you would decrease the value in slot 1 by the cost of the arrow and increase the value in slot 2 by 1.
The sky is the limit on things like this. Just expand the TextStorage type and the loop when you think of something else you want your areas to do. For example, what if you wanted the player to only be able to pass through a door if he had the right key?:



Voila!

Another common add-on you may need, depending on your game, is a battle system.

The thing that is unique about a battle system is that it needs to loop around. The player needs to attack, then the enemy, then the player again...

The easiest way I could think of to do this is to have another loop for battles. The basic idea: you add a BattleFlag and EnemyNo to the TextStorage type, and you add a new typed array for each type of enemy. If the program sees that the battle flag is set to 1, it sets the variable CurrentEnemy to the EnemyNo for that area. Then it routes the program down to the battle loop.
If you choose this method, the battle loop would need another array to hold data on the player's attacks, and yet another for data on the enemy's. The player chooses an attack, the program randomly selects one of the enemy's attacks, and health variables for both are decreased appropriately. An if statement then checks to see if either the player or the enemy's health has dropped below 0. If they have, it sends them on to an appropriate area, which would be defined in the Texts array.

Alrighty, now you have enough good info to run with. Start thinking about your plot, and watch for the next compo!


* Footnote: Zotoaster has suggested a few cleaner methods to store the Options data in the Texts() array. Using this method can allow to easily add many more options to each room without having to have a massive TextStorage type.

Basically I combined two of his ideas. Your type will look like this:



What each of the _Start and _Count variables do is define a portion of another array that contains the options or text for this area. The _Start is the index in this other array that the options for this area start on. The _Count is how many options or texts there are. The other array contains ALL the data for the entire game in a massive block, and the Texts() array just defines what parts of the array go to what areas. With only three options, it probably would actually add typing overall, but if you had 5 or 6 your fingers would greatly appriciate this method.



Diggsey: I have a spine and memory, but one memorable guy says he hates me. What am I?
Must Program
15
Years of Service
User Offline
Joined: 22nd Nov 2007
Location:
Posted: 18th May 2008 10:44
Wow thanks for the help im making an options one for my text based game and this was a big help thankyou.


Is it possible to input photos aswell so when you are in the car you can see a picture of a car next to the text?
BMacZero
16
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 18th May 2008 15:58
Glad it helped!

And yes, images are quite easy to add. Add another variable to the TextStorage type that stores an image number. Then, in the main loop, just paste that image:



Unterseeboot - Has you played it?
Los Mineros are on leave...
Zotoaster
17
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 18th May 2008 21:41
This is pretty good. Think I'll add it to the tutorials thread.

Just a little bit of input: It always bugs me to see numbered variables inside types, yet you can't put arrays in them. Try looking at my tutorial on types which gives some neat work-arounds to it.

Don't you just hate that Zotoaster guy?
BMacZero
16
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 19th May 2008 06:59
Thanks, Zotoaster! I'll definitely have a look at your tutorial soon.

Unterseeboot - Has you played it?
Los Mineros are on leave...
BMacZero
16
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 19th May 2008 17:01
Ok, I've added a footnote on that. Thanks!

Unterseeboot - Has you played it?
Los Mineros are on leave...

Login to post a reply

Server time is: 2022-12-01 10:40:14
Your offset time is: 2022-12-01 10:40:14