by Ruud Helderman <firstname.lastname@example.org>
Licensed under MIT License
This is not a C tutorial; there are plenty of those on the web. The reader is assumed to have some basic knowledge of programming in general, and preferrably the C programming language in particular. Maybe you just finished reading a tutorial somewhere, and you would like to learn some more about programming by studying other people’s source code. Or could it be that you really are genuinely interested in the ancient art of writing text adventure games from scratch? Maybe you are just having a sense of nostalgia about the old days, when life was simple, and so was software. In either case, you have come to the right place.
In the 1980s, text adventures were a respectable genre of computer games. But times have changed: in the 21st century, they pale in comparison with modern MMORPGs with 3D engines. Where books survived the rise of film, text-based games quickly lost the battle against their graphical counterparts. ‘Interactive fiction’ is kept alive by an active community, but its commercial value is long gone.
There is a bright side to this: authoring tools of professional quality are now available free of charge. This is also the single biggest advice I can give you if you intend to write your own adventure: use one of the many dedicated development systems.
But then, why this tutorial? Why write an adventure game using a general-purpose programming language? Because doing so can be entertaining, challenging and educational. The programming language C may not be the most obvious choice for writing a text adventure; it’s nothing like LISP-based languages like MDL and ZIL. But while writing my own humble text adventure, I learned that C fits the bill quite well.
The ideas you find here apply not only to C but to other programming languages as well. If you prefer Java or Python over C, it should not be too difficult to translate my code samples.
I tried to keep this tutorial readable for a broad audience, without too much off-topic elaboration. For terms that might warrant further explanation, please follow the hyperlinks to relevant Wikipedia articles.
Throughout this tutorial, we will be developing a fully functional text adventure game. This will be done in an incremental fashion. With every chapter, some code is added to our program. Each increment, however small, adds some value to the game. And every time, the result is a working program, ready to run once built with a proper C compiler.
We start off with just 9 lines of code; it is little more than a trivial “Hello World” program. If you can’t even get this simple example to work, you should find some help with that first, before you are ready to move on to the next chapter.
Welcome to Little Cave Adventure.
It is very dark in here.
Some explanation for those not quite familiar with C:
Trivial as it may be, the program does demonstrate the most important aspect of any text adventure: descriptive text. A good story is one of the things that makes a good adventure game. I deliberately kept it brief in my code sample. I will focus on the programming aspect of the game, and leave the rest up to your imagination.
|⭳ Download source code 🌀 Run on Repl.it|
Next chapter: 2. The main loop