Take Me

Now that I have my scenery objects in play, the next big thing to tackle is Items. As you may expect, I consider items to be game objects that you can pick up, carry around and put to use in other places. Naturally, creating items will also include having an inventory.

When you think about it, items are actually not all that different from scenery items. The key difference is really that they can be picked up and, perhaps, manipulated in a different kind of way. Structurally, however, they share the same implementation.

↳ Token
↳ Name
↳ Description

As a result, the implementation of the class looks similar to the one I used for Scenery.

Naturally, the Evaluate() method will have to look a bit different, allowing the player to pick up the item and add it to the player’s inventory.

There are a couple of new things in this small bit of code that I want to explain. First is the InInventory() function.

The implementation of the player’s inventory is really very simple and straight forward. All it takes is a variable called theInventory in globals.py that contains an empty list at first. Whenever the player picks up an item, I will simply append the item to the list and whenever an item is taken out of the inventory, I will remove it from the list. Couldn’t be simpler.

The function InInventory(), simply runs through that list and checks if the desired item is in it, by comparing the token. If it finds the item, it returns the item so my program can use it to process it further without having to run through the loop again at some later point. It looks like this.

The other function I should explain here is GetName(). It is part of the Item class and returns the name of the item itself, but it can also add an article to it, turning “fork” into “a fork” or “the fork,” depending on how I call the function. By default, it will return the name without any article.

The article definitions are part of my globals.py file where I define all my variables that need to be accessible throughout the project.

Seeing this now, also reminds me that I should explain the auto() call as part of the definition. You may recall that when creating the vocabulary, I numbered my words by hand. As the list got longer, I got tired of doing that because I wanted to be able to move words around so I can keep logical groups of words together and doing so would have required me to constantly renumber the definitions.

That’s when I found out about the auto function in Enums. All it does is really creating the next consecutive number for me. This way, “No” is 0, “Undef” is 1 and “Def” is 2. If I change their order, they will be simply renumbered accordingly, which makes things a lot easier and cleaner. I am using auto() on all my enums at this point. No need locking down numbers when I can have Python do it for me.

Going back to my Evaluate() method and the picking up of objects, after making sure the item in question is not yet in the inventory, I print out the message that tells the player that he’s picking up the item, and then I simply add the item to the inventory. The last step to do is to remove the item from the room because, evidently, now that the player picked it up, it should no longer remain there.

This requires a new method in the Room class.

It’s really quite simple, in that it simply runs through all items in the room and compares the token, then removes it from the list. It is, in fact exactly the same process the inventory function will run through when I remove an item from the player’s inventory.

All that is left now, is to throw some item into a room. That, however, is also pretty standard stuff by now, simply adding it to the room definition.

The problem now is that if I pick up the key, there’s no way for me to see that I have it. I need a function to list the player’s inventory.

This may, once again, look vaguely familiar to you, because I’ve done something similar when listing exits. I am simply running through the inventory list, stringing up the names of all items with “and” and then, at the end, replacing all but the last one with commas, to create a printed list that will look something like this.

As you rummage through your pockets, you find a key, a green rock and a sonic screwdriver.

Naturally, the function also catches cases where the player carries no items with its own text message.

That’s pretty much what it takes to manage standard items in the game, but naturally, I will have to create specialized, derived classes as I go forward to allow for special behavior of certain items. But I’ll cross the bridge when I get there.

One Reply to “Take Me”

Leave a Reply

Your email address will not be published. Required fields are marked *