问题描述
RPG库存无法正常运行。 对不起,如果我的代码太糟糕了,我才开始学习Python。
的Python 3.7.0
闲置3.6。 (64位)
崇高文字3.1.1。(3176)
#firstly I created classes for items and items_in_body_parts.
#for example, one of them:
class item_for_put_on:
def __init__(self):
self.name = ''
self.body_part = '' #for what body part? like in Diablo, for example, you can take a weapon in your hand and put on a helmet on your head
#there is a lot of other staff, i deleted it to make it simple, here are the main things:
right_hand_item_1 = right_hand_item()
on_head_item_1 = on_head_item()
item_for_put_on_1 = item_for_put_on()
item_check_on = item_check_on_class()
right_hand_item_1.name = ''
right_hand_item_1.body_part = ''
on_head_item_1.name = ''
on_head_item_1.body_part = ''
item_for_put_on_1.name = ''
item_check_on.name = ''
i_stick = ['Stick', 'in_hands']
i_helmet = ['Helmet', 'on_head']
all_item_names = {'Stick' : i_stick, 'Helmet' : i_helmet}
def item_put_on():
global item_for_put_on_1, right_hand_item_1, on_head_item_1
if item_for_put_on_1.body_part == 'on_head' and on_head_item_1.name == '':
on_head_item_1.name = item_for_put_on_1.name
#go back to function inventory_menu
if item_for_put_on_1.body_part == 'in_hands'and right_hand_item_1.name == '':
right_hand_item_1.name = item_for_put_on_1.name
#go back to function inventory_menu
#def inventory_menu(): --- this and a lot of other functions i don't decribe you, it's not necessary
def inventory_bag():
#we come here from inventory menu to see what we have in the bag
#global item_check_on, item_for_put_on_1 ---- should be globals here?
#here was some code - to describe to player all content of the bag - it works good
#NOW PLAYER DECIDES WHAT ITEM TO CHOOSE TO READ INFO ABOUT IT AND AFTER THAT HE CAN PUT IT ON HIS HERO
#command4 - it is the list of items.names in bag - it works good; player should type full name or to choose item's number
com4 = input ("> ")
while com4 not in commands4 and com4.lower() != 'back':
print ("Unknown command, try again.\n")
com4 = input ("> ")
if com4 in commands4:
ppp = com4
for i in all_item_names: #i - str!!!
if ppp == all_item_names[i][0]:
item_check_on.name = all_item_names[i][0]
item_check_on.body_part = all_item_names[i][1]
# remember ---- i_stick = ['Stick', 'in_hands']
# remember ---- i_helmet = ['Helmet', 'on_head']
# remember ---- all_item_names = {'Stick' : i_stick, 'Helmet' : i_helmet}
print("You are looking to the item " + item_check_on.name) #this is just info to display for player
print("item body part: " + item_check_on.body_part) #this is just info to display for player
ppp = ''
com4=''
print ("Would you like to 'equip'('eq') it or 'back'?\n")
com5 = input ("> ")
commands5 = ['back', 'equip', 'eq']
while com5.lower() not in commands5:
print ("Unknown command, try again.\n")
com5 = input ("> ")
if com5.lower() == 'back':
inventory_bag()
break
elif com5.lower() in ['equip', 'eq']:
item_for_put_on_1.name = all_item_names[i][0]
item_for_put_on_1.body_part = all_item_names[i][1]
item_put_on()
break
break
else:
print('test looping') #loop while going to this again and again, searching for the correct name of item in dictionary all_item_names
if ppp == '' and action4 == '':
break
elif action4.lower() == 'back':
#inventory_menu()
def inventory_eq():
#we come here from inventory menu to see what is equipped on our hero
#global right_hand_item_1, on_head_item_1 ---- should be globals here?
print("right arm - " + str(right_hand_item_1.name) + "\n")
print("head - " + str(on_head_item_1.name) + "\n")
#SO HERE WE CAN SEE WRONG INFO --- if you put on only one thing, everything is right, but if you put on the second thing - there will be two sticks (one on head, one in hand) or two helmets (one on head, one in hand)
所以在这里我们可以看到错误的信息:如果只穿一件东西,那一切都是对的,但是如果穿第二件东西,那将有两根棍棒(一个在头上,一个在手)或两个头盔(一个在头上) ,一个在手); 由于项目名称存在问题。 而且,如果我为其添加更多的物品和插槽,最后一个将是例如Shield-英雄的身体上将会有很多盾牌,而不是正确的物品。 当然,库存中没有以前的物品。
在我看来,问题出在全局变量的某个地方,或者while循环在某个地方效果不好(也许我永远不知道每秒分配一次)。
看来,当我们更改item_for_put_on_1时,它会更改所有已分配的内容-函数item_put_on()中的on_head_item_1和right_hand_item_1。
如果我在装备完物品后在item_put_on()中使用了item_for_put_on_1的清零功能(使其成为=''),它将以某种方式从设备中删除物品的名称
因此,主要问题与item_for_put_on_1有关,但我需要帮助以找到解决问题的方法。
1楼
我解决了这个问题,删除了object和object.names。 现在,我使用简单的变量-使它们遍及全球。
现在可以了! 但是,即使我们用字典“ all_item_names [i] [0]”中的直接名称替换了愚蠢的不必要的“ item_for_put_on_1”,我也不明白是什么问题。 用这种方法不能解决问题。 下面的代码仍然无法正常工作(更改名称也存在同样的问题)
# item_left_hand, item_on_head - these are objects, they got .name firstly
# below code is in item_put_on(): #I also saved global i from inventory_bag() for that
if all_item_names[i][1] == 'in hands':
item_left_hand.name = all_item_names[i][0]
elif all_item_names[i][1] == 'on head':
item_on_head.name = all_item_names[i][0]
但是通常的全局变量(不是对象)可以很好地工作:
global item_left_hand, item_on_head #in all functions
# below code is in item_put_on(): #I also saved global i from inventory_bag() for that
if all_item_names[i][1] == 'in hands':
item_left_hand = all_item_names[i][0]
elif all_item_names[i][1] == 'on head':
item_on_head = all_item_names[i][0]