Skip navigation

I managed to finish everything I set out to do today with Python, even if it might have dipped into more of my time for other things than I would have wished. So it goes.

A lot of this extra time was spent in the debugging phase of today’s work. In particular, I was having trouble getting the game to restart. There were a few minor issues with it that I cleared up easily, but I got seriously stuck on resetting the player’s starting inventory. Copying some code I’ve seen in other programs, I decided to use global variables at the top of the file containing my “runner” (driver) class to initialize some of the game’s starting values, including inventory. It looks like this:

STARTING_INVENTORY = ["dagger", "coins"]

This seems to be an artifact of C++’s “#define” statements, but I suppose it works just fine in Python. I think the idea is to make these variables exceptionally readable and easy to locate so they can be changed conveniently.

The problem I ran into was that, when resetting the game after getting rid of the coins, reassigning this value to the runner object’s self.inventory attribute was only giving the player the dagger. It took me the longest time to figure out what was going on.

After maybe 20 minutes, I finally remembered that I had encountered this problem in the past, but had carelessly forgotten about it: lists are not copied when reassigned. When you make a list and continue using it, one list is created and will be passed along to variable after variable.

So, when I was assigning STARTING_INVENTORY to self.inventory at the beginning, then clipping “coins” off of self.inventory, I was actually clipping coins off of the original list assigned to STARTING_INVENTORY. When I was passing STARTING_INVENTORY back into self.inventory, I was passing back that same, clipped list that was already assigned to self.inventory. In other words, I was not actually resetting the inventory and therefore cheating the player out of his or her hard-earned coins.

What this has taught me (or retaught me) is that lists are more concrete than abstract; the changes you make to them last longer; they are mortal. Of course you can copy them (which was my eventual solution), but it’s something you have to specify; it is not native to the nature of the list to be copied and disseminated; it wants to remain unique.

But enough anthropomorphism of data structures.

I have written out all the traps and dead ends for the game, and tomorrow I make some finishing, aesthetic touches. The traps are really not intricate and the dead-ends are far from thorough. I have realized that 99% of the actions in adventure games are dead ends. It would take a very long time for me to map every mundane, pointless action in detail, so I will leave it as it is. Considering that in the example I’m supposed to follow there are maybe three possible choices, I have gone far past the call of duty.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: