Interactive Fiction in Python Part I
In 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.
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:
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 #Goals: # #Part 1: #Initialize the game #Set up the player #Print the header #Create the main loop # #Initialize import os #Character defaults #Start character in Room 1 player_location = 1 def print_header(): os.system("clear") print """ __ __ _ _ _ _ \ \ / /___ | | __| | ___ _ __ ___ _ _ | |_ ( )___ \ V // _ \| |/ _` |/ -_)| ' \ / _ \| '_|| _||/(_-< _\_/ \___/|_|\__,_|\___||_|_|_|\___/|_| \__| /__/ | _ \ ___ __ __ ___ _ _ __ _ ___ | // -_)\ V // -_)| ' \ / _` |/ -_) |_|_\\___| \_/ \___||_||_|\__, |\___| |___/ """ #Show start screen print_header() 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 print_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.")
Recent blog posts
- Simple Python Game Tutorial Using Classes
- Video Tutorial: Python Spacewar Game
- Video Tutorial: Python To-Do-List (GUI Version)
- Video Tutorial: Simple Space Invaders Clone
- Video Tutorial: Python Functions with Turtle Graphics
- Video Tutorial: Python Guessing Game (GUI Version)
- Global Codeathon Scratch Tutorials
- Video Tutorial: A Simple Python Turtle Graphics Game
- Video Tutorial: Tic-Tac-Toe in Python
- In Favor of Standardization in Ed Tech