可堆叠项目以及如何更改其数量以使用插值数据显示?

2024-04-25 21:08:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我不是很有经验,我用renpy/python构建了一个可爱的工作清单。我在尝试使我的物品可堆叠时遇到了一些困难。我一直在尝试让我的类项对象有一个可编辑的变量/整数,我不确定它是否是我正在寻找的语法,或者我是否需要以不同的方式排列我的类,如果是,如何排列?以前,清单只会在我的显示屏上添加一个相同的项目(和图标)。这是我希望在指定文本中显示的内容:

class item(object):
        def __init__(self, name, img, himg, THIS THING):
            self.name = name
            self.img = img
            self.himg = himg
            self.THIS THING = THIS THING

尽管名称已明确定义或出现某种混乱的“%item.amountd”文本,但我在没有得到“name is not defined”错误的情况下得到的最接近的结果是:

label start:
     show screen inventory
     pause
     "adding another item"
     $ backpack.add_item(popcorn)
     jump start

return



default popcorn = item("popcorn", "inventory/ragepop_idle.png", "inventory/ragepop_hover.png", 0)
default money = 50
default backpack = container()
default inv = []



######screen
screen inventory():
    zorder 2
    add "images/inventory/inventorybg.png"
    modal True
    text "Money: [money]0" xpos 310 ypos 60 color("#FFFFFF")

    hbox:
        xpos 298 ypos 113
        spacing 7
        xmaximum 695
        box_wrap True
        for thing in backpack.inv:
            imagebutton idle thing.item.img hover thing.item.himg action Null
            text "{b}[thing.item.amt]{/b}" size 20 color("#FFFFFF") xoffset -30 yoffset 5


    imagebutton:
        idle "inventory/invpois.png"
        hover "inventory/invpois2.png"
        focus_mask True
        action Hide("inventory")

##amt is what I settled on for the THIS THING
init python:
    class item(object):
        def __init__(self, name, img, himg, amt):
            self.name = name
            self.img = img
            self.himg = himg
            self.amt = amt

    class invitem(object):
        def __init__(self, item, amount):
            self.item = item
            self.amount = amount


    class container(object):
        def __init__(self):
            self.inv = []


## Disregard these add/remove methods as I have to change these once I figure out how to $ amt+=1
##As they stand now, they serve to put the icon in the inventory the old unstackable way.

        def add_item(self, item, amount=1):
            self.inv.append(invitem(item, amount))
            return("success")

        def has_item(self, item, amount=1):
            if item in [i.item for i in self.inv]:
                if self.finditem(item).amount >= amount:
                    return(self.finditem(item).amount)
                else: return(False)
            else:
                return(False)

        def finditem(self, item):
            return(self.inv[[i.item for i in self.inv].index(item)])

        def remove_item(self, item, amount=1):
            if self.has_item(item):
                self.finditem(item).amount -= amount
                if self.finditem(item).amount <= 0:
                    self.inv.pop(self.inv.index(self.finditem(item)))
                    return ("gone")
                else:
                    return ("more left")
            else:
                return ("not found")

当我运行此操作时,库存显示屏幕首先为空,然后项目显示在其中,如下所示: example run

不幸的是,0将是我放在那里的任何东西,我无法通过调用项目或尝试让项目调用一个易于编辑的变量并显示为“amt”来在游戏中计算如何添加或减去“amt”

这是我应该告诉你我所做的一切的部分。这对我来说很难,因为我尝试了太多不同的东西,以至于我无法确切地了解输入的内容的细节。基本上,我通过文档和其他人的论坛结果找到了各种各样的amt插值方法,并试图使其适用于我的项目,但大多数情况下,我得到了一堆“类项目没有属性amt”和“名称amt没有定义”。失败后,我试图说服我的物品从一个完全独立的变量中获取其金额。我已经成功且轻松地使用插值文本在库存屏幕上显示了资金。它只需要“文本”货币:[money].0”,并且在游戏过程中使用$money 1+10等很容易更改。如果我试图从中获取的插值信息在项目类中,我无法说服它正常工作

提前感谢您对我做错了什么有任何想法


Tags: 项目nameselfimgreturnpnginitdef
1条回答
网友
1楼 · 发布于 2024-04-25 21:08:53

我知道出了什么问题!我需要给我的项目类属性!这就是最终的结果

        def __init__(self, name, img, himg, amt):
            self.name = name
            self.img = img
            self.himg = himg
            self.amt = amt

        @property
        def output(self):
            return str(self.amt)

        def add_stack(self, amt):
            self. amt += 1

    class invitem(object):
        def __init__(self, item, amount):
            self.item = item
            self.amount = amount```

相关问题 更多 >

    热门问题