• Advertisement
Sign in to follow this  

How do text games work?

This topic is 4676 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
Ok, you can either use console output or draw to the SDL window. Pick one and roll with that. If you're going to use the console, you don't need to use SDL at all. You can either use GLUT or some other function set to get a loop going and capture input.

Good luck!

[Edited by - CyberSlag5k on March 31, 2005 11:37:00 AM]

Share this post


Link to post
Share on other sites
I have found this: http://devpaks.org/show.php?devpak=7 which lets you use curses with dev-c++. I'm working through this http://en.tldp.org/HOWTO/NCURSES-Programming-HOWTO/helloworld.html tutorial.

Seems to do the job fine so I'l stick with it :)

Share this post


Link to post
Share on other sites
I've just started games programming and have done 2 console applications in C++. They are far more involved than I first imagined.

Feel free to take a look if you like..:)

Tic-Tac-Toe:
TicTacToe

Textris:
Textris

Personaly I think I may have made the tetris clone a little too complicated for what it needs to do, but I'm still learning.

Share this post


Link to post
Share on other sites
Does anyone have any brain waves of how to do forms / menus in a cleverer way than having a function for each? At the moment I have a loop running which has a switch on a status variable. It calls a function for the page that the user is on. For example if the status is 1 then it calls main_menu() if its 2 it calls a different menu and so on..

I had a look at the link you gave me and he had come up with a clever way of doing it, the thing is I didn't understand it because I am using c not c++.

I thought I could have a function which takes a pointer to a string and displays the string as a menu but it wouldn't know what to do for each menu item.

Share this post


Link to post
Share on other sites
I'm not too sure but the C++ version should almost be the same for C. You woudl have to make a few changes, for example instead of having a string you'd have to use a char[]...that kind of thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by kzar
I had a look at the link you gave me and he had come up with a clever way of doing it, the thing is I didn't understand it because I am using c not c++.


Make structs to replace classes; their functions (including operator overloads) become free functions where the first argument is a pointer to a struct of that type (then you need to fix up variable references to look within the struct when necessary). std::vectors will need to be replaced with plain arrays. (In the class where I extended std::vector, I probably should have been containing one as a data member instead, anyway).

Share this post


Link to post
Share on other sites
The key to victory here is to learn string functions such strlen(), strcat() etc, etc. For a console program, a text game is rather straight forward, but for a game that requires visuals from DirectX you will have to write your own text/font engine. It certainly ain't fun creating individual bitmaps for each character... %_%

So there you go - master strings!

Share this post


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

  • Advertisement