I often speak and write about text-based gaming. The topic is relevant to the publishing audience that I usually address, but I do it mostly because I love the genre. I’ve enjoyed playing text games since I was young, but they were also instrumental in teaching me to program.
At Safari we think a lot about effective ways to learn technology. What worked for me was simply having a program I wanted to write, one that felt achievable given my complete ignorance of the subject. When I was a kid, writing a “real” video game seemed impossible: it required math skills I didn’t have, and the use of very low-level programming languages like assembly that I didn’t think I could learn. Writing a text game is completely achievable in any language, including very simple ones like BASIC. In doing so I learned many of the fundamentals of programming: taking user input, storing data in variables, and simple control structures like loops and — I’ll admit it — GOTO.
People still write text games today. The annual interactive fiction contest (in its 19th year!) wrapped up recently. Although interactive fiction games can be narratively and technologically complex, a simple game is easy to write, and it’s still a more engaging introduction to programming than “output 100 numbers in the Fibonacci sequence” or many other Computer Science 101 problems.
And so, text-based games remain a good choice for an introduction to programming, one that is realizable in any number of programming languages.
Lisp, the granddaddy of interactive fiction
I was delighted to find that Land of Lisp by Conrad Barski teaches Lisp by implementing a simple adventure game. This is particularly charming because the earliest and most famous text games were written using a Lisp-like language (the original manual for ZIL is available as a PDF).
In Land of Lisp, the reader is taught how to write the game in Common Lisp rather than any domain-specific language:
> (describe-location 'living-room *nodes*)
(YOU ARE IN THE LIVING-ROOM. A WIZARD IS SNORING LOUDLY ON THE COUCH.)
and is taken all the way through writing a simple English parser to an actual playable game:
This is exceedingly cool, not only as a gentle introduction to Lisp, but as a lesson in why a Lisp variant was suited for this kind of game in the first place.
The Web Game Developer’s Cookbook by Evan Burchard takes a different approach. In the chapter on writing interactive fiction, the reader is walked through developing a simple choose-your-own-adventure game using the impress.js presentation library. I’ve used impress.js for slides and I like it a great deal. It wouldn’t have been my first choice for an IF engine, but I can see the value in starting with something that already models a page-like approach.
Again, Twine would be a better choice for implementing a game like this for real, but as a teaching tool, it’s fun and effective:
One advantage of using a browser engine is that multimedia effects are trivial to implement.
Last but not least, Python
Python is my language of choice, so I’d have been remiss to not include at least one example of using Python to write a text-based game. Luckily, the example comes from one of the best books on learning the language that we have in Safari Flow: Learn Python the Hard Way, by Zed Shaw.
Zed’s example won’t take you through writing an entire game, but it does teach you the basics of accepting user input in Python. The book is a very gentle introduction to programming, and a complete interactive fiction implementation would be a lot for a total newbie to take on.
Even if interactive fiction isn’t your thing, I strongly encourage anyone thinking about learning to code to come up with a simple project that you’ll find motivating. It should be achievable, just a little out of your comfort zone, and fun.