Interactive Fiction in Python Part III

You see an amulet!If you haven't read the introduction to this Interactive Fiction project in Python, or Part I, or Part II,  now would be a good time to do so! In our last installment, we learned how to use Python classes to create rooms in our virtual world that we could move around. In this installment, we are going to use the same feature, classes, to create items in our game that we can interact with.

 

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.

class Item:
	#Initialize the class
	def __init__(self, name, description, location, movable, state):
		self.name = name
		self.description = description
		self.location = location
		self.movable = movable
		self.state = state

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.

#Create dictionary of items

items = {}

#item[] = Item("", "", 0, False, "default")
items[1] = Item("mirror", "The mirror is large and has a gold frame.  There is ancient writing on the frame! It says, To Useth this Myrror, Sayeth thou Tralfaz.", 2, False, "default")
items[2] = Item("amulet", "The amulet has a large purple crystal in the center.", 2, True, "default")
items[3] = Item("dumbledore", "Dumbledore looks tired and worred.", 2, False, "default")

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.

In our next installment we will learn how to get the items, and drop the items. Stay tuned by following me on Twitter, or subscribing to my RSS Feed.

Comments

yes!

yes!

Thank you, your explanations are good, your tutorial is easy to follow and understand.

i am eagerly waiting for part 4:)

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.

@TokyoEdTech

Poll

What LMS does your school use?: