如何检查由类函数创建的所有变量并使用它们?

时间:2016-12-12 13:53:15

标签: python-3.x

总之;我几天前开始编码,并且想要尝试进行一个简单的基于文本的冒险将让我面对很多问题,我将在其他更难的项目中遇到这些问题。我的类 init 函数生成带有一些变量的项目,其中一个是设备槽位[0-6]。我想解开一个插槽,但是我现在设置它的方式要求我知道哪个项目在那个特定的插槽中。

英文:unequip(“mainhand”),mainhand有slotnumber 0.获取所有装备物品的信息并检查哪一个具有相应的槽号,然后删除该特定物品。 (有些项目有2个槽号(“althand”),这意味着我必须找到一种方法来确保从列表中删除正确的项目,但这是我以后可以做的事情)。现在,我似乎无法弄清楚如何动态调用项目并用它们做些什么。

PS。我很确定我能以更多植物学的方式做到这一点,欢迎任何建议,但无论如何,我仍然想知道如何动态调用该函数。

我尝试这个的代码:

def Unequip(Slotname): #Slotname is just a string, so I could say: unequip("Mainhand")
        for count,i in enumerate(Item.slotname): #Item.slotname is a list of strings for slots which corresponds with Item.Slot which are the values determining the which slot is occupied.
            if Slotname == Item.slotname[count]: #so when the name I put into the function equals the Item.slotname, I know which number in Item.Slot I need.
                for items in Item: #Here we enter the problem (again, I understand that this code could be a lot better and would love some suggestions).
                #Item is a object, but it's __init__ functions produces items, such as item1, item2 etc. I would like to check if any of these items is currently in my Item.Equipped and has the Item.Slot value I want to remove. 
                #I tried global(), locals() and dir(Item) but none give me what I need. I really hope this makes it clear.
                    if Item.Slot[count]  == items.slot and items.name == Item.Equipped: #I need a susbtitute for the items variable which will represent item1, item2 or item3 etc. So I can do Item.Slot.remove(item2.slot).
                        Slot = Item.Slot.remove(items.slot)
                        Equipped = Item.Equipped.remove(items.name)
                        Player.stats = list(map(operator.sub,list(Player.stats),self.itemstats))
                    elif Item.Slot[i]  == items.altslot and Items.name == items.Equipped:
                        pass

完整代码(我尝试使用自我等,但它可能不是超级可读,我的道歉),它包含一个item.unequip函数,但这需要我选择特定的项目,而不仅仅是我希望我的插槽要删除的项目

Edit1:根据请求删除了所有不需要的内容:

import random
import operator
class Item:
    Equipped = []
    Slot = []
    Inventory = []
    num_of_items = 0
    statnames = ["Strength", "Agility", "Dexterity", "Wisdom", "Constitution", "Intelligence"]
    slotname = ["MainHand","Offhand","Head", "Necklace","Chest","Legs", "Cape" ]
    def __init__(self, name, itemstats, slot, altslot = None, atk = None, Def = None): 
        self.itemstats = itemstats #itemstats in order: S, A, D, W, C, I
        self.name = name
        Item.num_of_items += 1
        self.slot = slot
        self.altslot = altslot
        if atk != None and atk != 0:
            self.atk = atk
        if Def != None and Def != 0:
            self.Def = Def

    def Unequip(Slotname):
        for count,i in enumerate(Item.slotname):
            if Slotname == Item.slotname[count]:
                for items in dir(Item):
                    if Item.Slot[count]  == items.slot and items.name == Item.Equipped:
                        Slot = Item.Slot.remove(items.slot)
                        Equipped = Item.Eqiupped.remove(items.name)
                        Player.stats = list(map(operator.sub,list(Player.stats),self.itemstats))
                    elif Item.Slot[i]  == items.altslot and Items.name == items.Equipped:
                        pass

class Player:
    stats= [8,8,8,8,8,8]

    item1 = Item("Sword of damaocles",[5, 1, 0,1,2,-2],0,1,20)
    item2 = Item("Helmet of steel",[9,9,9,9,9,9],2,None,0,20)

0 个答案:

没有答案