• Advertisement
Sign in to follow this  

RPG Inventory Help

This topic is 1092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey. I could use some help with my RPG. I've got a functioning inventory but what I would really like to do is make it intractable. Right now I want to focus on equipping and using items. Anyone got any ideas? 

STR = 5
EquippedW = ""

def prompt():
    x = input()
    return x

class Item(object):
    def __init__(self, name, value, quantity = 1):
        self.name = name
        self.raw = name.strip().lower()
        self.value = value
        self.quantity = quantity

class Weapon(object):
    def __init__(self, name, value, damage, quantity = 1):
        self.name = name
        self.raw = name.strip().lower()
        self.value = value
        self.quantity = quantity
        self.damage = damage

    def equip(self):
        for name, weapon in weapons:
            EquippedW=weapon.name
            

class Armor(object):
    def __init__(self, name, value, defense, quantity = 1):
        self.name = name
        self.raw = name.strip().lower()
        self.value = value
        self.quantity = quantity
        self.defense = defense

class Container(object):
    def __init__(self, name):
        self.name = name
        self.inside = {}
 
    def __iter__(self):
        return iter(self.inside.items())
 
    def __len__(self):
        return len(self.inside)
 
    def __contains__(self, item):
        return item.raw in self.inside
 
    def __getitem__(self, item):
        return self.inside[item.raw]
 
    def __setitem__(self, item, value):
        self.inside[item.raw] = value
        return self[item]
 
    def add(self, item, quantity=1):
        if quantity < 0:
            raise ValueError("Negative quantity. Use remove() instead")
 
        if item in self:
            self[item].quantity += quantity
            self[item].recalc()
        else:
            self[item] = item
 
    def remove(self, item, quantity=1):
        if item not in self:
            raise KeyError("Item not in container")
        if quantity < 0:
            raise ValueError("Negative quantity. Use add() instead")
 
        if self[item].quantity <= quantity:
            del self.inside[item.raw]
        else:
            self[item].quantity -= quantity
            self[item].recalc()

items = Container("Items")
weapons = Container("Weapons")
armor = Container("Armor")

IronBroadsword = Weapon("Iron Broadsword", 25, 5)
weapons.add(IronBroadsword)


def room():
    print("Equip Iron Broadsword?")
    Weapon.equip(IronBroadsword)
    print(EquippedW)
    print(STR)

room()

Share this post


Link to post
Share on other sites
Advertisement

have a character class and an equipped class so that you can do something like the following:

class Character:
   
    Inventory = new Inventory()


    def checkWeapon():
           Inventory.getWeapon()

    def checkHelmet();
          Inventory.getHelmet()

    def getAllStats():
          For item in Inventory:
                 this.health += item.healthBonus

etc. 

 

Hopefully that helps you out!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement