Interactive Fiction in Python Part II

Move!If you haven't read the introduction to this Interactive Fiction project in Python, now would be a good time to do so! In our last installment, we set up our basic framework for our text adventure including creating a header, setting a location for our character, getting a command from the user, and setting up our main game loop.
 
In this installment, we will look at how to create the actual game world using classes.  A class is defined as:A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.” (Ref: http://www.tutorialspoint.com/python/python_classes_objects.htm)
 
For those of us who speak English, allow me to interpret that. A class is simply an object that has attributes and can do stuff.  Let's take a look at a simple example.
 
Imagine we want to create a class about a person with the attributes of height (in cm.), weight (in kg.), and nationality.  The height and weight should be floats and the nationality should be a string.
 
#Define the Person class.  Note: Class names are capitalized
 
class Person:
 
    def __init__(self, height, weight, nationality):
    self.height = height
    self.weight = weight
    self.nationality = nationality
 
In the example above, we create the class, and then define the attributes.  The keyword self refers to the particular instance.  After self, list the attributes just like you would with a function.  So when you define the instance, you need to put your attributes in the same order as shown below.  Note that you ignore "self" when you create the instance.
 
#Create an instance of the class Person called christian
christian = Person(175.0, 92.0, “American”)
 
To access the values, we use dot notation, which is the instance plus dot plus the attribute.
 
#Show the values for this particular instance.
print christian.height
 
>>>175.0
 
print christian.weight
 
>>>92.0
 
print christian.nationality
 
>>>American
 
Let’s say I go on a diet and lose 5 kilos (God, I hope so!). I can change the weight like so.
 
christian.weight = 87.0
print christian.weight
>>>87.0
 
Creating Rooms in our Text Adventure
 
OK, so how does this help us?  For our simple text adventure, our rooms will have the following attributes:
 
Attribute Description
shortdescription Text to be printed after “You are..."
description  Full description of the room including what you see, exits, unmovable objects.
n, s, e, w, u, d Room number of the room to that direction.  0 is used where no door or exit exists.
 
The class declaration looks like this:
 
class Room:
 
    #Initialize the class
    def __init__(self, shortdescription, description, n, s, e, w, u, d):
        self.shortdescription = shortdescription
        self.description = description
        self.n = n
        self.s = s
        self.e = e
        self.w = w
        self.u = u
        self.d = d
 
So now we need to create some rooms.  For this, I will use a dictionary.  Those of you more familiar with Python will probably want to use a list instead, but for our purposes we will use a dictionary so the room numbers are obvious.  Also, I could use a list for the directions, but again, I want to keep this as simple (I hope!) as possible.
 
#Create dictionary of rooms
rooms = {}
 
#rooms[] = Room("", "", 0, 0, 0, 0, 0, 0)
rooms[1] = Room("in the waiting room outside Dumbledore's Office", "The waiting room is small, but nicely furnished.  There is a door to the north that leads to Dumbledore's Office.", 2, 0, 0, 0, 0, 0)
 
rooms[2] = Room("in Dumbledore's Office", "There are a lot of old books on bookshelves. Dumbledore's desk has various items on it. There is a Magic Mirror in one corner. Dumbledore is sitting behind his desk. There is a door to the south.", 0, 1, 0, 0, 0, 0)
 
The code above will create 2 rooms, room 1, and room 2. If you go north from room 1, you will go to room 2 (Dumbledore’s Office), and if you go south from room 2, you will go to room 1 (the waiting room).
 
 
Moving Around
 
In Part I, we printed the location number only.  Now that we’ve created the rooms, we can now print out the location using our awesome new knowledge of classes.  The code looks like this.
 
#Describe location
print "You are " + rooms[player_location].shortdescription + ".\n"
print rooms[player_location].description
print
 
So now, we have to write the code for the user to move around.  To make things easy, we’ll use n, s, e, w, u, d to move in that direction.
 
Let’s think about how we can move.
 
1) The user enters the direction he or she would like to go.
2) Check to see if an exit exists (ie. The number for that exit is not 0)
3) If so, change the player location to that room
4) If not, inform the user.  No need to update the player location
 
#Move n,s,e,w,u,d
if command == "n":
    if rooms[player_location].n != 0:
        player_location = rooms[player_location].n
    else:
        print "Sorry, you can't go that way.\n"
 
It should be relatively trivial to copy the above code and alter it so the user can go south, east, west, up, or down.
 
That’s it for this installment.  I recommend typing the code in yourself to get used to the structure of classes and to gain experience using the editor, but if you really want to get going, feel free to download the code here.
 
In our next installment where we will use classes to create the items we will use in our game. Stay tuned by following me on Twitter, or subscribing to my RSS Feed.

Comments

Post new comment

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

@TokyoEdTech

Poll

What LMS does your school use?: