Python(RenPy):执行函数的文本按钮,它们不会在每次按下时显式调用该函数

2024-10-02 20:39:53 发布

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

在我正在进行的游戏中,我使用一个数组来跟踪玩家所在公司的当前统计数据,并使用以下函数来编辑数组

init python:

#The following store item objects, which include an array of their own stats
#Stores currently owned equipment
   Equipment = []
#Stores items available to buy
   Items = []
#Stores currently equipped equipment
   Equipped = []
#The company's current stats   
   SecArray = [0, 0, 0, 0, 0, 0]

#Called whenever moving something in or out of the currently equipped items.
#Pass the current item's stat array, the stat array, and a + or - symbol
   def arrayedit(array, SecArray, symbol):
        Notify("The stats have changed")
        if symbol == "+":
            SecArray[0] += array[0]
            SecArray[1] += array[1]
            SecArray[2] += array[2]
            SecArray[3] += array[3]
            SecArray[4] += array[4]
            SecArray[5] += array[5]
        if symbol == "-":
            SecArray[0] -= array[0]
            SecArray[1] -= array[1]
            SecArray[2] -= array[2]
            SecArray[3] -= array[3]
            SecArray[4] -= array[4]
            SecArray[5] -= array[5]
        return()

但是,如果“装备”数组中有任何物品,则每次单击文本按钮时,它们的统计信息都会添加到当前统计信息中(例如,如果一个物品的统计信息中有3个,那么玩家的当前统计信息将在每次单击任何按钮时增加3个,无限向上计数)。类似地,如果“装备”数组中有任何物品,则每次单击文本按钮时,都会从玩家的当前属性中减去它们的当前属性。“Items”数组中的项没有任何效果

以下代码适用于windows商店和装备/拆卸设备

screen shopping1():

    frame:
        xpos (config.screen_width*25/64) ypos (config.screen_height*11/64)
        ysize (config.screen_height*31/64)
        xsize (config.screen_width*36/64)

        has side "c r b"

        viewport:
            yadjustment tutorials_adjustment
            mousewheel True

            vbox:
              xpos (config.screen_width*2/5) ypos (config.screen_height*3/16)
              ysize (config.screen_height/2)
              xsize (config.screen_width/2)
              for i in Items:
                    if i.kind == "Item":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(Equipment, i), RemoveFromSet(Items, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

              for i in Policies:
                    if i.kind == "Policy":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(OwnedPolicies, i), RemoveFromSet(Policies, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)                           

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

              for i in Trainings:
                    if i.kind == "Training":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(OwnedTrainings, i), RemoveFromSet(Trainings, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

        bar adjustment tutorials_adjustment style "vscrollbar"

        textbutton _("Return"):
            xfill True
            action [Hide("shopping1")]
            top_margin 10

screen equipmentedit():

    frame:
        xpos (config.screen_width*5/128)  ypos (config.screen_height*2/64)
        ysize (config.screen_height*47/64)
        xsize (config.screen_width*19/64)

        has side "c r b"

        viewport:
            yadjustment tutorials_adjustment
            mousewheel True

            vbox:
              null height 10
              text "Unequipped Items" alt ""
              null height 5
              for i in Equipment:

                    if i.kind == "Item":

                        textbutton "[i.title]   $[i.cost]":
                            action [arrayedit(i.stats, SecArray, "+"), AddToSet(Equipped, i), RemoveFromSet(Equipment, i), Hide("equipmentedit"), Return(i.cost)]
                            left_padding 20
                            xfill True

                    else:

                        null height 10
                        text i.title alt ""
                        null height 5
              null height 10
              text "Equipped Items" alt ""
              null height 5
              for i in Equipped:

                    if i.kind == "Item":

                        textbutton "[i.title]   $[i.cost]":
                            action [arrayedit(i.stats, SecArray, "-"), AddToSet(Equipment, i), RemoveFromSet(Equipped, i), Hide("equipmentedit"), Return(i.cost)]
                            left_padding 20
                            xfill True

                    else:

                        null height 10
                        text i.title alt ""
                        null height 5




        bar adjustment tutorials_adjustment style "vscrollbar"

        textbutton _("Return"):
            xfill True
            action [Hide("equipmentedit")]
            top_margin 10

除此之外,所使用的数组和函数不会在程序的其他地方调用或引用。我相信每次单击按钮(包括返回按钮)时,都会为装备和设备阵列中的项目调用“arrayedit”功能,但我不确定原因。如有任何见解,将不胜感激


Tags: configtruereturniftitleactionarrayscreen