When we speak of items in interactive fiction, it could refer to something like a magic potion that you pick up, and at some point in the game drink. An item can also refer to something that cannot be picked up, for example another person, or a large object such as a Magic Mirror.

Just like with our Room class, our Item class will have attributes. For our simple game, our Item class will have the following attributes: name, description, location (room number), movable (True/False), and state. The name is the word that the user will type to refer to the item, for example, refrigerator. The refrigerator will have a location in the game; for items that are not available until the user does something, we will put them in location 0. We need to indicate if an item is movable or not (meaning the user can get the item and carry it). Finally, our items will have a state which can be used to describe what the item looks like or how it is functioning. For example, when my game starts, the refrigerator might be closed, however, during the game, the user will open it. So, it's beginning state might be "closed", while later in the game it might be "open".

As with the Room class, we will use the class keyword in Python to create our item class definition. As before, we need to capitalize the first letter of the class name. If you are not sure about this class definition, please refer to the previous post in this series which explains class definitions in more detail. Our class definition will look like this.

Once we have defined the class, we can then create instances of that class. As with the Room class, we are going to use a dictionary. For those of you with more Python experience, you may wish to use a list instead, but for our purposes we will use a dictionary. As you can see below, we first create the empty dictionary called items. Then we create our test items. In this case there are three: mirror (unmovable), amulet (movable), and Dumbledore (unmovable). Each of these items is located in room #2.

Having the items in the game is not very useful if the player doesn't know they are there. So, we need to go to the part in our code where the computer displays the player's location. For unmovable objects, I recommend putting a reference to the object in the room description itself as the item will always be there. In our example, the Magic Mirror is always in room 2. However, for items that we can pick up, move around, and drop, we will need to display them dynamically.

This is not too terribly difficult. Right after we describe the room, we can list the items. To do so, we simply need to create a loop that checks each item on the items list to see if it is in the same location as the player. The code to do so is listed below. Notice the use of iid (item id) in the loop below. Don't forget that we need to make sure the item is movable as well.

#Tell what is in the room (Movable items only)
print "You see the following: "
for iid in range(1, len(items)+1):
if items[iid].location == player_location and items[iid].movable == True:
print items[iid].name

Go ahead and try the code and see if you can get it to work. As I've mentioned before, it is valuable practice to actually type in the code yourself to get the hang of exactly how the code needs to by typed and to get used to indenting. But, for those of you who can't wait, you can download the code here.