Answer the question
In order to leave comments, you need to log in
How to properly design a Character's inventory structure?
Hello!
I'm trying to make a main character inventory system for an RPG game.
Interested in the question of how to properly design the inventory of the main character, how to store things (weapons, armor, etc.), how to choose things from the inventory. How it is reasonable to organize all this in an object-oriented approach?
So far I've made this sketch:
class Hero:
def __init__(self, name, hp = 100):
self.name = name
self.hp = hp
self.weapons = [] # инвентарь с оружием
self.armor = [] # инвентарь с броней
# добавить предмет в инвентарь
def take(self, item):
if isinstance(item, Weapon):
self.weapons.append(item.__dict__)
elif isinstance(item, Armor):
self.armor.append(item.__dict__)
class Weapon:
pass
class Sword(Weapon):
def __init__(self, name = 'Меч', damage = 30):
self.name = name
self.damage = damage
class Bow(Weapon):
def __init__(self, name = 'Лук', damage = 20):
self.name = name
self.damage = damage
class Armor:
pass
Answer the question
In order to leave comments, you need to log in
Inventory is inventory. It stores any item based on its size and/or weight. Slots are already specialized cells in which you can place only those types of items for which these slots are intended, for example, weapons or armor.
Thus, it is logical to make the base class "item" (item), describing items in terms of storage in the inventory and general manipulations (buying, selling, etc.) and child classes "armor" (armor), "weapon" (weapon) etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question