Interactive Fiction in Python Introduction

"You are in a maze of twisty little passages, all alike..." As a middle school student with a love of reading and an active imagination, the words on the screen conjured up visions of monsters, treasure, and danger...all with just a little text. Interactive Fiction was once a hugely popular genre of computer games, but is now relegated mostly to hobbyists. Since I am teaching a new Python course this year, I wanted to learn more about Python myself and make something fun at the same time. So I settled on making my own interactive fiction game.

For those of you unfamiliar with the game mechanics of Interactive Fiction, users enter simple English commands to guide a character through a virtual world. The goals vary, but by the end you've faced numerous challenges and probably died...many, many times.

Here is the opening of Zork I The Great Underground Empire, one of the most famous text adventures of all time. The ">" symbol shows where the user typed a command.:

You are in an open field west of a big white house with a boarded front door.

There is a small mailbox here.

> open mailbox

Opening the mailbox reveals:

A leaflet.

> read leaflet

Taken.
Welcome to Zork (originally Dungeon)!

Dungeon is a game of adventure, danger, and low cunning. In it
you will explore some of the most amazing territory ever seen by mortal
man. Hardened adventurers have run screaming from the terrors contained
within.

As you've likely noticed, you are able to interact in this world in logical ways. First, there's a mailbox...let's open it. A leaflet? Let's read it. And so on... Older Interactive Fiction, such as Zork, accepted two word commands (and a few shorter ones such as inv for inventory. The player can get items in the game, drop them, and use them in various ways. Like reading a book, you use your imagination to visualize the scene. In this game as with most interactive fiction, you move around the world by using north, south, east, west, up, or down. The user needs creativity, perseverance, and logic to solve the puzzles encountered in the game.

In this series of blog posts, I will walk you through how to create a simple text adventure using Python. My goal is to port another text adventure I created using JavaScript to a Python-based version. This text adventure is based around a Harry Potter theme; you play as Harry Potter trying to rescue your friends Ron and Hermione from the Evil Lord Voldemort. You can play the online JavaScript version in your browser by clicking here to see what the game will involve. Please note that our Python version will be text-only, while the JavaScript version has images.

Stay tuned for more installments as I walk you through the coding process and teach some Python concepts along the way! UPDATE: Click here to read Part I.