set of items and clothes. And at some points I want them to be able to exchange items. So I figured classes where a good way to represent each character and their inventory. However, I want each inventory to hold an x amount of the same item, something like what you see in some RPGs like Suikoden, where you have for example up to Medicinex6 using one space, and if you add another Medicine, it becomes Medicinex1 in a different space and so on.
However I'm really stuck on how to apply this idea on actual code. I thought of using lists to store each player's amount of each item, however using a group of lists inside another list seems awfully unelegant and I have a feeling things will get messy and unnecesarily complicated when I start adding methods like remove or exchange item.
This is what I have so far:
Code: Select all
init python:
class Item(object):
def __init__(self, name= "", desc= ""):
self.name = name
self.desc = desc
class Player(object):
def __init__(self, name= "", inv = []):
self.name = name
self.inv = inv
def add_item(self, item, amount): #This method probably won't work properly
for l in self.inv:
if item not in l:
self.inv.append([item, amount])
default dave = Player('Dave')
default white_flower = Item('Flower', 'A pure white flower.')