Jump to content
  • Advertisement
Sign in to follow this  
kzar

How do text games work?

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I was wondering how text based games work? I have been making some basic text based menus for a program using printf and things and its not too pratical. The thing is I have definately played text based games which couldn't have been made using just printf. So could anyone tell me how they are made? Games like nethack, ZZT to name a couple I have played. Thanks

Share this post


Link to post
Share on other sites
Advertisement
I never got there (making console-based games, just only command line apps...), but AFAIK, a good choice is to use win32 console API or whatever its called. There used to be some great articles at Gametutorials, but most tuts there are no longer free.
Just my 5 cents though

Ohh, and also, as far as I remember, you can use all those console graphics API from 1000000 years ago. Im not sure if conio for example provides some methods for the purpose

Share this post


Link to post
Share on other sites
There are several techniques to making text based games like MUDs.

One would be to just capture characters as each key is pressed (GLUT, SDL, and (I assume) Win32 all have easy ways of doing this) and storing these keys into a buffer that gets processed when the user presses enter. You'll basically just parse the sentence, first by checking each token for validity ("move" is a valid token, "asdf" is not) and then checking the sentence grammar ("move south" works, "move get sword" does not). I'll go deeper into how you would do this (basically just make functions for each command that call other functions based on the grammar) if you need me to, but you probably get the idea.

You could also use multi-threading, so that instead of checking all of the keys each second you only capture characters as keys are pressed (interrupting the main loop). This is a little more advanced than the other technique, though both work in pretty much the same way.

MUD Pies is a good article for you to check out, if it is a MUD you're interested in making.

I'm not sure what you mean by these games could not have been made using printf, though in a project like this I think I'd opt for a language like Python which has all sorts of nice string handling techniques. Python is also a scripting language, so it'd probably be very nice for a MUD anyway.

Good luck and if you have any other questions, please do not hesitate to ask!

Share this post


Link to post
Share on other sites
Well I have been using SDL to make my little games and things already so I can see how keyboard input would be easier to use via that. The thing is though when you use SDL it makes printf's go into a file instead of to the screen. If theres a nifty way to display text using SDL it would be perfect though. I would rather not use the windows api for my project because I don't really like learning windows specific things when I can avoid them.

By the games not working with printf what I mean is that it doesn't handle colours and things. I suppose you could have some functions to assemble each line of text and then display them to the screen, but I'm not sure how well it would work.

Share this post


Link to post
Share on other sites
Quote:
Original post by kzar
The thing is though when you use SDL it makes printf's go into a file instead of to the screen.


Um...are you sure you're not using fprintf? I don't have much experience with SDL, but I know it ate the console for the one I'm working on now. That's not the end of the world, though, because...

Quote:
If theres a nifty way to display text using SDL it would be perfect though.


Yup! There is indeed. Google it, I'm sure you'll find it, but I know for a fact that SDL has really easy-to-use text handling (the guy on my team doing the GUI did it in like 20 minutes).

Quote:

I would rather not use the windows api for my project because I don't really like learning windows specific things when I can avoid them.


Amen, brother. Damn the man! Go cross platform! And while Win32/MFC are super powerful, man are they a pain to use (at least at first, it gets better as you go along but I'd just as soon chuck the whole thing to begin with).

Hope this helps!

Share this post


Link to post
Share on other sites
I'm not completely certain how a text-based game works, but I have been coding my own for a while, and it can get mind numbing depending on how you go about doing things. If you want to check it out, take a look in my "text-based demo code" thread.

Share this post


Link to post
Share on other sites
In the good old days of ZZT simply writing to the screenbuffer sufficed quite well :) in real mode it was located somewhere near addres 0xB800:0000 or something similar.

Under windows you need to use the console api that gives you much added flexibility over printf or you could look into using something like Curses or nCurses also depening on terminal type there's a bunch of special sequences that can be used to perform diffrent tricks.

Basicly it depends on your platform how you do it but yeah vanilla printf isn't going to cut it in most cases.

Share this post


Link to post
Share on other sites
Right, I googled like you said and I can't find any mention of a way to do console output. The closest I found was some addon for a quake like console in your games :(

If SDL isn't an option can anyone recommend a good console api, preferable one that I can use on a few different OS's and is fairly simple :)

Thanks for the pointers :)

Share this post


Link to post
Share on other sites
I'm not sure if you're still trying to find a new API but I switched from SDL to Allegro because it was much easier for me to understand. Both are documented pretty well, except for font info concerning Allegro. But it's easily doable using the grabber, and I've found it to be easier to get a project up and running quicker using Allegro.
That's just two pennies.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!