HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Game inventory system

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
inventorygamesystem

Problem

I've been trying to figure out the best way to create a inventory system in Python. I like the way this one works but what I was wondering was if there was a better way to write it.

inv = ['Sword','Armor']
item_values = {'Sword':['Sword',5,1,15,2],
               'Armor':['Armor',0,10,25,5]}
print('Name\tAtk\tArm\tLb\tVal')
print(item_values[inv[0]][0],'\t',item_values[inv[0]][1],'\t',item_values[inv[0]][2],'\t',item_values[inv[0]][3],'\t',item_values[inv[0]][4])
print(item_values[inv[1]][0],'\t',item_values[inv[1]][1],'\t',item_values[inv[1]][2],'\t',item_values[inv[1]][3],'\t',item_values[inv[1]][4])

Solution

The items and the inventory can be modelled intuitively with classes:

  • You can define an Item class that has attributes like name, weight, attack



  • You can define an Inventory class that has a collection of items, and knows how to print them



Something like this:

class Item(object):
    def __init__(self, name, attack, armor, weight, price):
        self.name = name
        self.attack = attack
        self.armor = armor
        self.weight = weight
        self.price = price

class Inventory(object):    
    def __init__(self):
        self.items = {}

    def add_item(self, item):
        self.items[item.name] = item

    def print_items(self):
        print('\t'.join(['Name', 'Atk', 'Arm', 'Lb', 'Val']))
        for item in self.items.values():
            print('\t'.join([str(x) for x in [item.name, item.attack, item.armor, item.weight, item.price]]))

inventory = Inventory()
inventory.add_item(Item('Sword', 5, 1, 15, 2))
inventory.add_item(Item('Armor', 0, 10, 25, 5))
inventory.print_items()


UPDATE

Based on a suggestion by @jonrsharpe you could move the printing logic from print_items to the __str__ method:

def __str__(self):
    out = '\t'.join(['Name', 'Atk', 'Arm', 'Lb', 'Val'])
    for item in self.items.values():
        out += '\n' + '\t'.join([str(x) for x in [item.name, item.attack, item.armor, item.weight, item.price]])
    return out


This way, instead of inventory.print_items(), you can print the inventory more intuitively with print(inventory).

Code Snippets

class Item(object):
    def __init__(self, name, attack, armor, weight, price):
        self.name = name
        self.attack = attack
        self.armor = armor
        self.weight = weight
        self.price = price


class Inventory(object):    
    def __init__(self):
        self.items = {}

    def add_item(self, item):
        self.items[item.name] = item

    def print_items(self):
        print('\t'.join(['Name', 'Atk', 'Arm', 'Lb', 'Val']))
        for item in self.items.values():
            print('\t'.join([str(x) for x in [item.name, item.attack, item.armor, item.weight, item.price]]))


inventory = Inventory()
inventory.add_item(Item('Sword', 5, 1, 15, 2))
inventory.add_item(Item('Armor', 0, 10, 25, 5))
inventory.print_items()
def __str__(self):
    out = '\t'.join(['Name', 'Atk', 'Arm', 'Lb', 'Val'])
    for item in self.items.values():
        out += '\n' + '\t'.join([str(x) for x in [item.name, item.attack, item.armor, item.weight, item.price]])
    return out

Context

StackExchange Code Review Q#57438, answer score: 8

Revisions (0)

No revisions yet.