【问题标题】:Inventory-system for Text-RPG - global bugText-RPG 的库存系统 - 全局错误
【发布时间】:2018-09-23 13:30:41
【问题描述】:

RPG-inventory 效果不佳。如果我的代码太糟糕,我很抱歉,我刚开始学习 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)

所以在这里我们可以看到错误的信息:如果你只戴上一件东西,一切都很好,但如果你戴上第二件东西 - 会有两根棍子(一根在头上,一根在手上)或两顶头盔(一个在头上,一个在手上);因为物品名称有问题。如果我为他们添加更多物品和身体槽,最后一个将是例如盾牌 - 英雄的身上会有很多盾牌而不是正确的物品。当然,库存中没有以前的物品。

在我看来,问题出在全局变量的某个地方,或者某个地方的 while-loop 无法正常工作(也许永远分配每一秒,我不知道)。

似乎当我们更改 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 有关,但我需要帮助才能找到解决它的方法。

【问题讨论】:

标签: python inventory global


【解决方案1】:

我解决了这个问题,删除了对象和 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]

【讨论】:

    猜你喜欢
    • 2015-09-14
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多