Python and Interactive Fiction Games

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 similar to Zork, Arthur, Hitch Hikers Guide 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.

Game Developers List Back Up

Hello everyone,

We just would like everyone to know that the game developers list is now back up and running. We spent a little time this morning fixing some issues with the USA Games mailing lists and list subscription pages and everything seems to be back in working order. So if you want to talk about programming games in BGT, C#, C++, Visual Basic, Python, etc please go to our list page

http://www.usagamesinteractive.com/lists.php

and subscribe to the game developers list. Everyone is welcome regardless of if you are a new accessible game developer or an old hand. In addition, we have plans to build and design a new game developers website

http://developers.usagamesinteractive.com

so if you have programming tutorials, guides, tips and tricks, etc you want to share/donate to the project let us know and we’ll get them posted to the new developers site as soon as it is back up. Hope to see you there.

Running Older Games on Windows 7

Running Older Games on Windows 7

Many gamers such as myself have recently purchased a new computer in the last year with state-of-the-art hardware, equipped with the new 64-bit processors from Intel and AMD, and a 64-bit version of Windows 7 preinstalled. For the most part this is fine for the majority of computer applications, and computer users, but for some of us who wish to hang onto our older Dos and Windows games it is getting to be quite an issue to maintain backwards compatibility.

One of the problems is that the 64-bit versions of Windows 7 will not run any 16-bit applications. As a result many Dos games including Dos based interpreters like Scare, Frotz, and so on are simply incompatible with newer releases of Windows. Sure, there are MS Dos emulators like DosBox, but those aren’t screen reader accessible. For text adventures there are other interpreters like Adrift, Winfrotz, Wintads, etc but those are not a perfect solution either. While they run fine on Windows for anyone using a screen reader it requires a fair amount of reviewing the screen with the screen reader’s review cursor which drastically slows down game play.

Another issue we are being confronted with is backwards compatibility for older Windows technologies. For example, Visual Basic 6–which was developed for Win 98–is now deprecated and officially unsupported on Windows 7. Although, these runtime libraries can be installed, and most Visual Basic apps and games still work nobody knows how long this compatibility will continue to last. It might work on the next five to ten versions of Windows, or be totally incompatible with the next version that is released. We just don’t know. This means that we need to find a solution that will help maintain compatibility with our newer systems and platforms for years to come regardless of what we might be running at the time.

Believe it or not there is a fairly straight forward solution for these and other compatibility issues. Its called virtual machines. A virtual machine is simply a third-party application such as VMWare Player or VirtualBox that allows the end user to install a guest operating system inside a host operating system. This creates a virtual environment that runs on top of the existing OS/platform allowing the end user to run software and applications in their native environment.

For example, I’m running a Toshiba notebook with an AMD 64-bit processor, have 3 GB of ram, 325 GB of hard drive space, and Windows 7 Home Premium installed. That’s plenty of power to run Windows 7, but I’m actually now running two operating systems side-by-side on the same machine. I downloaded VMWare Player 4.4 from

http://www.vmware.com

and installed it. Then, I created a new virtual drive, and installed Windows XP with service pack 3 into that virtual drive. As a result Windows XP is running side-by-side with Windows 7 Home Premium. The advantages here are huge.

With Windows XP running virtually ontop of Windows 7 I can now install any games written in Visual Basic, older 32-bit Sapi voices, any Dos games I want, as well as 16-bit interpreters like Frotz, Scare, Tads, etc and run them on a modern PC inside Windows XP. It really is the only surefire solution to bridge the gap between running a fully up to date PC and hanging on to software from yesterday.

Another advantage of using a virtual machine is being able to easily back up your installed software and games. VMWare Player installs your guest operating systems and software into an image which can be backed up to an external hard drive. To restore it on a new PC all you have to do is restore the image into a fresh copy of VMWare Player, browse for the image containing your software, and open it. So instead of having to reinstall your older games and applications from scratch you can keep a master image somewhere for new PC installs making backing up and restoring guest operating systems a breeze.

For game developers–such as myself–it has the added advantage of being able to write games and other software for multiple target platforms and environments. The developer can run multiple versions of Windows, Linux, etc allowing him/her to develop, design, and test his/her products on different operating systems than the one the developer is currently using.

That’s not to say virtual machines are perfect though. For one thing virtual machines may require quite a bit of hard drive space as the end user is running one or more operating systems inside another. It also requires a fairly modern PC with enough ram and CPU power to run two or more operating systems concurrently. For a new PC this isn’t really a problem, but it is advisable to have enough memory to run both operating systems and applications at the same time. I’ve noticed, for instance, if I don’t give XP at least 512 MB of ram of its own the virtual machine runs too slow for running screen readers and games on the virtual machine.

The other downside is cost. Many end users got their copies of Windows as a bundle with their old PC, and do not own or have a full retail copy for general use. They just have the stock OEM versions supplied by their manufacturer. This means there may be a small fee involved in having to purchase an older copy of Windows–such as Windows XP Basic–to run inside a virtual machine. Although, this is not an ideal situation it isn’t a deal breaker. Copies of Windows XP can be purchased from software retailers like

http://www.buycheapsoftware.com

for under $100. Which is fairly reasonable in order to maintain maximum backwards compatibility and still keep your new OS at the same time.

In conclusion while technology is rapidly advancing, technology is changing daily, and older software is being left behind there is still a fairly simple solution for this problem.Virtual machines like VirtualBox, VMWare Player, VMWare Workstation, and VMWare Fusion are the ideal solution for those who want to upgrade to a new PC or switch to a different operating system and still run your older Windows games and software. So if you are having troubles playing that favorite game of yesterday on your new Windows 7 64-bit PC a virtual machine might just be what the doctor ordered.

USA Games and 64-bit Operating Systems

USA Games and 64-bit Operating systems

by Thomas Ward

Oct. 8, 2011

Earlier this week we went out laptop hunting. As mentioned in a previous message on the USA Games news blog USA Games is preparing to create some cutting edge games for newer operating systems and PCs. We are well aware of the trends in the PC market where most desktops and laptops available are all running the latest Intel and AMD 64-bit processors and are preloaded with Windows 7 Home Premium. With Windows 8 now in beta, which is a 64-bit only operating system, this pretty much spells the end of 32-bit applications and hardware. Although, 32-bit applications will still run on a 64-bit platform most developers are actively converting their 32-bit applications and games to 64-bit. This is where USA Games is heading as well. Thus an upgrade of hardware and software is necessary.

So earlier this week we went to our local Wal-Mart and invested in a new Toshiba laptop to replace our aging Compaq Presario. This Toshiba isn’t the top of the line–its more of an e-machine–but what it does have is pretty decent. Here is a review of the system specs.

* AMD duel core 64-bit processor.
* 3 GB of memory.
* A 325 GB hard drive.
* DVD reader/writer.
* Internal card reader.
* Built-in web cam.
* Built-in microphone.
* A 15 inch LED display.
* Full-sized keyboard.
* Windows 7 Home Premium.

Its far from the top of the line, but as far as features and the price its not a bad deal. The advantage of this laptop is that it is 64-bit, has a 64-bit operating environment, and it is also very lite.It is a very decent system for software development, e-mail, web browsing, and anything else we need to use it for.

Now, that we have the new computer we have began the process of upgrading our games as well as the Genesis 3D engine to support 64-bit operating systems directly. We’ve upgraded to Microsoft Visual Studio 2010, and eventually we will be replacing DirectSound support with XAudio2. These and other changes will allow us to produce cutting edge games for new Windows 64-bit users. This, of course, will allow us to have a long term support cycle for our games for many many years to come.

However, we’ve not completely forgotten about our XP customers. We’ll be producing and releasing 32-bit versions of our games for a while as that is still our primary market group. It would be less than ideal to invest completely in the new technologies when the majority of our customers are still running Windows XP and older 32-bit hardware and software. The best way to describe what we are doing right now is we are preparing for the conversion to newer 64-bit hardware and software, but we won’t be immediately switching fully over to 64-bit as there is a lot of 32-bit customers out there who may not upgrade for at least the next three to five years or so. When Microsoft drops XP around 2014 we’ll probably be supporting Windows 7 and Windows 8 more directly, but as it stands now Windows 7 is the minority not the majority. So at the moment we’ll get the ball rolling in that direction, but we will be offering backwards compatible software too.

Going Laptop Hunting

Everyone who programs for a living or even as a hobby knows its pretty hard to keep up with current PC specifications because the technology changes so fast. No sooner does a developer outfit his/her home or office with the latest computer hardware and software the technology standards change requiring yet another expensive upgrade. For big name game studios like EA Games this is really no big deal as they have millions to spend on hardware and software upgrades. For a small independantly owned game studio like USA Games it is a financial concern, and it is more difficult to make sure that we stay up to date in regards to trends and changes in the wider PC market at large.

For instance, back in early 2008 we purchased a couple of Compaq C500 series notebooks for home and office use that had a 32-bit Intel Pentium 3.6 GHZ processor, 1 GB of ram, 120 GB hard drive, and Windows Vista Home Premium installed. Were we going to buy a new laptop today we might pick something like the Toshiba L755 series notebook that has a 64-bit Intel I3 2.1 duel core GHZ processor, 4 GB of ram, a 640 GB hard drive, and of course Windows 7 Home Premium. Its not just that the Toshiba L755 is newer, has more memory, is faster, but that the changes in technology are so drastic that it effects the kinds of games we can and might choose to develop in the future.

The big concern here for us is 32-bit vs 64-bit operating systems. As I write this article its difficult to even find and purchase a 32-bit desktop or laptop as all the major U.S. stores like Best Buy, Staples, Office Max, and Wal-Mart only carry 64-bit computers preloaded with Windows 7, and Microsoft has already stated that the next version of Windows will strictly be a 64-bit OS. In other words, when we, as developers look ahead of what our customers might be using in the next five years or so, it is clear the industry as a whole is rapidly moving to a 64-bit platform and that developing 32-bit software isn’t going to be in our best interests long term. So at this point USA Games is going to have to be able to create versions for both older 32-bit platforms as well as prepare for newer 64-bit platforms at the same time. That requires a hardware and software upgrade on our part.

So recognizing that this change is coming and that USA Games would be better off getting started on developing 64-bit games and software sooner than later we’ve decided to do a little shopping. At the moment we have not decided on anything, but we are comparing a number of newer 64-bit laptops preloaded with Windows 7 Home Premium, and we hope to purchase a couple of new laptops that will keep us developing state of the art software for the next three to five years. One of the laptops we have under consideration is the previously mentioned Toshiba L755 which seems to have everything we need and is under $600. This will allow us to use a relatively state of the art machine to design or redesign our Genesis 3D engine not only for the hardware/software of today but tomorrow as well.

I might add that its not just the hardware that has changed but software standards and specifications have changed as well. particularly in the area of PC gaming. Back in 2008 most mainstream game developers were still using DirectInput, DirectSound, and various other DirectX 8.1/9.0C core components. However, since then Microsoft’s new XAudio2 API for Windows Vista and Windows 7 has replaced DirectSound as the Windows audio mixer for cutting edge mainstream games for the PC. Microsoft’s XInput library has largely replaced DirectInput for new joysticks, game pads, racing wheels, and various other game controllers. Bottom line, things are very different now than they were 3.5 years ago, and we need to begin thinking of how to upgrade our software to meet these changes in software sooner or later. As with the hardware changes above it seems perfectly logical to prepare for these changes in software specification sooner than later. Which is exactly what we plan to do.

So over the next couple of months we will be out shopping for some new laptops. Once we have acquired our new PCs we’ll be in a better position to look at what needs to be done in order to bring Mysteries of the Ancients, Raceway, and any other projects up to current standards and specifications. Perhaps by Christmas we’ll be in a position to produce 32-bit versions for people still running say Windows XP or 32-bit versions of Windows Vista/7 as well as produced cutting edge versions for brand new laptops and desktops running Windows 64-bit software.