Python and Interactive Fiction Games

by Thomas Ward

Feb. 14, 2012

For about a year I have been searching for the best language to
develop classic interactive text adventures like Zork, Arthur, Hitch
Hikers to the Galaxy, and various other famous games that have been
developed by interactive fiction companies and authors over the years.
When it comes to writing interactive fiction there is a large variety
of tools, languages, and interpreters available for an author to work
with. We have specific text adventure authoring systems such as
Adrift, Inform 7, and Tads as well as all the  other traditional
programming languages like C or C++ to choose from. The only problem
was I couldn't find a language that had the right balance of ease of
use but the ability to build extremely complex worlds and handle a lot
of stats. So while there were many options I felt there was no middle
ground which I'll explain in more detail below. However, Python seems
to fit all of my requirements because its easy to use, is
cross-platform, and is able to build extremely complex worlds and
handle a lot of stat checks and so on. None of the other choices
seemed to fit all of the requirements I was looking for.

For example, I started my quest with the Adrift 4.0 Generator and
Runner. To be fair it is a nice adventure system, and getting up and
running with a basic text adventure is fairly straight forward and
easy. The Generator allows the developer to bring up a simple dialog
box and then he or she can enter the required information into various
edit fields as well as check and uncheck attributes using standard
checkboxes. This makes creating adventures very easy as there is no
programming involved and it is a tool clearly designed for the layman.
Unfortunately, its this simplicity and ease of use that I also found
problematic and undesirable for my own work.

Having used full blown programming languages for roughly a decade I
found that Adrift didn't give me nearly the flexibility and control
over the game that I wanted. I couldn't integrate a d20 stats system,
nor could I write custom AI for various enemies in the game. It was
simply too generic for what I had in mind for my text adventures.

Another problem I found with Adrift is while the Generator and Runner
are designed for the Windows operating system environment it wasn't
fully supported on non-Windows platforms. The Scare interpreter is a
cross-platform Adrift Runner for Mac, Linux, and Windows, but lacked
many features available in the Windows Adrift Runner such as a fully
operational combat system. As a result only some Adrift adventures
were fully compatible with Scare, and there was no Linux or Mac Adrift
creation tools that I could find. It made Adrift adventures less than
fully cross-platform compatible which was another of my requirements.

To compound the issue Adrift Generator is not freeware. As a result I
would be paying for software that didn't really fit all of my personal
requirements where other text adventure systems are open source or
freeware. So it wasn't a good financial investment for me.

So I turned to Inform 7 which has a huge base of supporters and
developers, and seemed like a good alternative to Adrift. I have to
say my experience with Inform was also mixed. It definitely was more
cross-platform because the Inform development tools are available for
Windows, Linux, and Mac and the free Frotz interpreter is available
for just about any platform I could name. Plus interpreters like
Winfrotz TTS is an added bonus because all of the text messages are
automatically read aloud by Sapi compatible voices on Windows.
However, in the end Inform wasn't so golden either.

Although, Inform is a programming language I found it is too abstract.
In attempting to make the language for non-programmers to understand I
found  it too verbose, and over simplistic in cases. To create a
vampire you might type
The vampire is a Man
which creates a vampire object using the Man class in the body of your
script. The problem here is that Inform only has about 16 default
classes to choose from, and I'd prefer to do something like
Man vampire
which is the traditional way programmers declare new objects in code.
In short, while I found Inform much better supported, better designed
than something like Adrift, I simply did not like the language itself.

Over the next few months I spent time looking at Tads, Hugo, etc. One
by one I tried them, didn't like them for one reason or another, and
then chose not to use them. After a while I was convinced if I wanted
something more advanced I was going to have to basically write it
myself in C or C++ which wouldn't be ideal given the time and
complexity involved in writing my custom adventure system from
scratch. However, before I got that far I decided what I really needed
was a scripting language which falls somewhere between a full blown
programming language and the text adventure languages.

Scripting languages such as TCL, Perl, Python, etc were specifically
created to address precisely this kind of issue. I wouldn't want to
write a text adventure in C++ because that is simply overkill for that
kind of project.  Plus there is a whole lot more complexity involved
in using a language like C or C++ which would take longer to program a
game of equal length and complexity that I could write in a scripting
language with less time and effort involved.

Something like Python is ideal in this situation because it is both
simple and powerful. A program can be one line that prints "hello
world" to the screen or be thousands of lines of code broken up into
classes and modules. Although, there are modules like PyGame for
sounds, music, networking, and input all I really needed for a simple
text adventure game was a way to randomly select numbers, print text
to the console, and allow me to enter a string of text for input.
Python is especially suited for this because it fulfills all of these
basic requirements with the random module, the print statement, and
the raw_input() function. The fact it supports advanced programming
techniques like object oriented programming with classes, objects, etc
is just icing on the cake.

The reason something like Python is so simple to use is that the
Python developers intentionally trimmed out all of the unnecessary
conventions and syntax found in most programming languages such as
terminating a statement with a semi-colon, surrounding blocks of code
with braces,declaring variable data types, and so on. What we are left
with is a very basic language that has the ability to rapidly develop
fairly complex scripts and applications with the absolute minimum
amount of programming necessary. A simple Guess the Number game that
might require 25 lines in C++ can be done in 10 or less in Python.
Thus making Python a great language for writing text adventures in
because I can get most of the advantages a language like C or C++
provides, but with the same kind of ease of use something like Inform
offers. In short, Python has the best of both worlds for an
interactive fiction developer who wants greater complexity in his or
her text adventures.

One of the things I personally found useful is comparing strings in
Python. In python its possible to compare a string consisting of one
or more words where in C and C++ comparing strings is down right
complex. Part of the complexity in C and C++ is different types of
character strings and they have to be converted from one type to
another before being compared, and then you sometimes have to use a
function like strcmp() to find out if the strings match. In Python
writing an if statement like this
if mystring == "examine room":
    examine_room()
 will perform the same level of string comparison without all that
extra baggage. As someone who has been programming in C and C++ since
the late 90's I don't think I understood how much extra work is
involved in handling strings in those languages until it was no longer
necessary to do that level of work to process basic command strings.

In short, the primary advantage of Python is that Python is designed
to freely code without having to worry much about coding conventions.
It, like most scripting languages, is written to resemble English, and
allows for a greater degree of free writing in the language itself,
For example, consider the code below.

print "The Legends of Arcania"
print "by Thomas Ward"
print "Version 1.0"
print "Copyright 2012 USA Games Interactive"

selection = raw_input("Do you want to load a saved game?")

if selection == 'y':
    load_game()

if selection == 'n':
    new-game()

In this simple code example I have created a very simple game intro
screen in 9 lines of code. Because I didn't have to worry about
braces, semi-colons, parentheses, etc I am able to free write in IDL
or a simple text editor like Notepad without a lot of unnecessary
programming and complexity. Anyone who has never programmed before
should be able to look at the code example above and realize what it
does at a single glance. Traditional full blown programming languages
usually aren't this simple, and it takes longer to program the same
basic message as well. As a result of its simplicity and ease of use
Python allows a developer like me to write very long and complex
interactive fiction type adventures with a greater degree of power and
ease.

Next, we have the  issue of cross-platform support. These days
developing for a single platform like Windows isn't always ideal.
Today everyone seems to be using a cell phone or similar handheld
device. Plus Mac OS and Linux have slowly become more popular over the
last decade. As a result gone are the days when a developer can create
a game specifically for a target environment like Windows and ignore
all the alternatives. Python resolves this issue nicely because unlike
a language like C or C++ Python isn't compiled to a operating system
specific binary, and can be run as uncompiled source code, just a text
file with a *.py extension, or compile it to a  *.pyc file that gets
run by the Python interpreter on the target environment. In either
case most games I write have the possibility of being redistributed on
computers and devices other than the one I am using and creating the
game with. As a result the ability to develop games for Mac, Windows,
and Linux using a single language and tools greatly increases the
number of games available for players using a variety of different
devices and operating systems.

Last, but certainly not least, is all of the modules available for
Python these days. Thanks to a great number of open source developers
there are a number of options to go above and beyond the simple text
adventure. There are modules for adding text to speech support,
modules that wrap sound APIs, modules to add networked game play, etc.
In other words with Python we have all of the tools to create large
multiuser networked based games complete with sound, input, and
speech. This alone has more to offer than the standard interactive
fiction languages because it has so much more potential.

Bottom line, I can't think of a language any better than Python for
developing the traditional interactive fiction text adventure than
Python. After a year of exploration, experimentation, and many failed
attempts Python has proved itself to meet all of my design goals. Plus
for new users, people who have never programmed before Python, is
truly a good introduction to programming without having to take on all
the complexities of more advanced full blown programming languages
like C or C++. That isn't to say I do not have reservations about
Python, don't have certain complaints, but those are acceptable
concessions as far as the type of games I want to create using the
language. Perhaps if you are interested in writing your own text
adventures you'll find Python to be a great option as well.

---
Gamers mailing list __ Gamers@audyssey.org
If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org.
You can make changes or update your subscription via the web, at
http://mail.audyssey.org/mailman/listinfo/gamers_audyssey.org.
All messages are archived and can be searched and read at
http://www.mail-archive.com/gamers@audyssey.org.
If you have any questions or concerns regarding the management of the list,
please send E-mail to gamers-ow...@audyssey.org.

Reply via email to