Remember when I said that I was creating a video game in my spare time? Well, if eight months of inactivity has told you anything, that hasn’t exactly gone to plan for a number of reasons, some of which are programming related (see my last post).
I am happy to say that I’ve gotten back into writing a video game, although as usual the story is completely different, as is my approach to writing the game. Instead of working on visual mechanics like smooth scrolling of the screen and animation of the character sprite, I’m worrying about inventory interaction and item alterations, all of which are done through a network of interconnected modules and classes. It’s a much cleaner approach, plus it’s much easier to test sets of class methods when that’s all I’m working with.
I’m taking a very micro-to-macro approach too, starting with single items, moving onto inventory, moving onto a character shell (which will eventually be greatly inflated, but no need to worry about that now!). Yes, a few important things are missing from the small set of code I have written (items having a special effect on the character or other items, for example), but much of that will flow once I figure out how I want the game to progress. Check out the two (partial) modules I have now:
# Superclass for all in-Game Items class Item(object): def __init__(self, name, amount=1): self.name = name self.amount = amount def adjust_amount(self, amount): self.amount += amount # Subclass to handle key items class KeyItem(Item): def adjust_amount(self, amount): pass # Class to store Items for Player within Game class Inventory(object): def __init__(self): self.inventory =  def add_item(self, item): self.inventory.append(item) def remove_item(self, item): self.inventory.remove(item) def adjust_item(self, item, amount): changing_item = self.inventory[self.inventory.index(item)] changing_item.adjust_amount(amount) if changing_item.amount == 0: self.remove_item(item) def order_inventory(self, item, new_position): temp = self.inventory[self.inventory.index(item)] self.remove_item(item) self.inventory.insert(new_position, temp)
# Creates Player class to be controlled by user class Player(object): def __init__(self, name, inventory): self.name = name self.money = 500 self.inventory = inventory def adjust_money(self, amount): self.money += amount # Controls inventory object changes def adjust_inventory(self, item, amount=1): if item in self.inventory.inventory: pass else: self.inventory.add_item(item) self.inventory.adjust_item(item, amount) def change_inventory(self, item, new_position): self.inventory.order_inventory(item, new_position)
Pretty simple, and only 40 lines when you ignore whitespace and comments, but the fact that everything works the way I want it to right now is a big deal. I could throw in a few other necessary variables and function calls right now, but I do have my first exam of the semester in… six hours… so I’ll just leave this here for now.
In bashnews, I fixed a problem with grep where no matter what I did, grep just didn’t work. It ended up being some extraneous commands in my .bash_profile that I must have put in while adding aliases and setting the color scheme for ls -G, since the commands came right after those lines. Now it works, and I’m back to being a happy camper!