Interactive Fiction in Python Part I

InventoryIn a recent blog post, I talked about the early days of computer gaming and the then-popular genre of Interactive Fiction, also popularly known as text adventures.  As promised, here is the first part in a tutorial series on how to create a simple text adventure in Python. 

Using Python, we will have the player entering text commands to move around a virtual world and interact with the environment.  The player will be expected to solve puzzles in order to move through the game world and achieve the objective. This tutorial assumes that you have a basic knowledge of Python programming fundamentals such as raw_input, print, loops, and functions.  Later tutorials will cover topics such as conditionals and classes.

Scenario: The user plays the role of Harry Potter.  Harry's friends Ron and Hermione have been captured by the Evil Lord Voldemort and he has to rescue them.  I already have a playable online version of this game written in JavaScript that you can play by clicking here.  Note that the online version has images while the Python version we are working on does not. 

Getting Started

We will need to take care of the following to get our game started.

1) Initialize the game.  This involves importing the necessary modules, and setting variables to their starting values. 

2) We will have to set up the player and any attributes associated with the player.  In our simple game, we only really need to keep track of the player's location, which will be an integer value.  Later, we can add health and a score if we so desire.

3) Print the game title (header) and explain the scenario to the user.

4) Create the main game loop. Inside the main loop we will want to print the header and get the user's commands.

Here is what the screen should look like after the player has made it through the splash screen:

Splash Screen

As you can see, the header is nicely drawn using ASCII Art.  You can create your own ASCII Art text by going to this website (which was discovered by one of my students).

The code is listed below and you should be able to copy and paste it into your favorite code editor.  Be sure to save it with the .py extension.

Here are some key points to look at:

1) The only real variable we have regarding the player at this point is the player location which is stored in the variable player_location.  Later, each location (room) in our virtual world with have an id number.

2) We use a while loop to keep the game going.

3) We have some code to print the player's location.  Since the rooms have not been set up yet, we are simply printing the integer value of the player's location.

4) We use raw_input to get the player's typed command.

In our next installment, we will look at using classes to create rooms in our game and learn how to move the player around the virtual world. Stay tuned by  following me on Twitter, or subscribing to my RSS Feed.

#Voldemort's Revenge Part 1 by Christian Thompson @TokyoEdTech
#Python 2.7
#Part 1:
#Initialize the game
#Set up the player
#Print the header
#Create the main loop

import os

#Character defaults
#Start character in Room 1
player_location = 1

def print_header():
	print """
 __   __     _     _                        _    _    
 \ \ / /___ | | __| | ___  _ __   ___  _ _ | |_ ( )___
  \ V // _ \| |/ _` |/ -_)| '  \ / _ \| '_||  _||/(_-<
  _\_/ \___/|_|\__,_|\___||_|_|_|\___/|_|   \__|  /__/
 | _ \ ___ __ __ ___  _ _   __ _  ___                 
 |   // -_)\ V // -_)| ' \ / _` |/ -_)                
 |_|_\\___| \_/ \___||_||_|\__, |\___|                

#Show start screen
print """
Welcome to Voldemort's Revenge.

You are Harry Potter. Hermione and Ron have been captured by Lord Voldemort! It's up to you to rescue them.


delay = raw_input("Press ENTER to continue.")

#Main Loop

while True:

	#Print the header

	#Describe location
	print "You are in room #%s." % player_location

	#Get the player's command
	command = raw_input("\n> ")

	#After all is said and done and before loop repeats, pause
	delay = raw_input("\nPress ENTER to continue.")


Dynamic ascii text

If you want dynamic ascii text, you can use the pyfiglet module. It gives the same result (although there are no options for vertical spacing).



pip install pyfiglet


Then you can do something like this:


from pyfiglet import figlet_format




print figlet_format(mytext, font=myfont)

Thanks for the info!

Hi Patrick,


Thanks for the info - I will definitely check it out.


I find for beginner tutorials, it is best to go with the stock Python install - that way there is less of a learning curve or hump for those new to programming to get over.


Thanks again!



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.



What LMS does your school use?: