在我正在开发的游戏中,我使用数组来跟踪玩家所在公司的当前统计数据,并使用以下函数来编辑数组。
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”函数,但我不确定为什么。任何见解将不胜感激!