如何使用函数

时间:2015-10-21 19:01:55

标签: python python-2.7

我一直在编写基于文本的RPG,熟悉类,函数和模块,以及它们如何相互作用和相互关联。目前,我有一个房间,真的是一个功能,有点像集线器类型的区域,以连接3个不同的房间。我将在这里发布一些代码,并在这段代码之后详细说明我的问题。代码看起来有点像这样(简化以表明我的观点):

def room_hub():
    movement = raw_input("[W] for North, [D] for East, and [A] for West:").lower()
    if movement == 'd':
        if room_E_bool == False:
             print "You have moved East! "
             room_East()

        elif room_E_bool == True:
             print "You have already been to that room! "
             room_hub()

def room_East():
    print "There's treasure here!"
    treasure = raw_input("Press [Y] to collect treasure! ").lower()
    if treasure == 'y':
         room_E_bool = True
         print "You have collected treasure!"
         print "Returning to the hub area!"
         room_hub()
    else:
        print "Not a valid command!"
        room_hub()

我的想法是,当用户运行函数时,我想要更改布尔值。这样,当他们返回到像地区一样的枢纽时,他们将不被允许重新访问某个房间。当然,这可能不是一个非常好的方法来做这样的事情,如果是这样的话,我想向他们提出其他解决方案来解决这个问题,即一旦他们访问某些房间,就会限制他们。

我尝试将布尔值作为全局变量,将其列在Python文件的顶部,以便hub函数可以引用布尔值来确定可用的选项。但是,似乎这个全局布尔值方法和函数内部的局部方法在声明一个新的布尔值方面都不能很好地工作。

有没有办法可以使用布尔来确定一个玩家是否已经通过某个功能,还是有更好的方法来做这个?

编辑: 这是我引用的代码块。在播放器输入[D]后,我移动了布尔值来改变值;然而,它仍然循环并允许他们回到房间。

def area_1_c(self):
    global dungeon_area_1
    dungeon = dungeon_area_1()
    print "You step into a hub like area."
    print "There are three ways to go: "
    print "North, east and west. "
    print ' '
    print "You can now also view your inventory anytime by pressing [I] when prompted for a choice. "
    print ' '
    print "Press [W] for North, [D] for East, [A] for West, [I] to view inventory."
    input = raw_input("What is your next choice?: ").lower()
    while input:
        if input == 'w':
            dungeon.area_1_f()
            print " "

        elif input == 'd':
            bool = False
            if bool == True:
                print "You have already explored this area. "
                print ' '
                input = raw_input("What is your next choice?: ").lower()
                continue
            else:
                bool = True
                print "You walk towards the east. "
                print ' '
                dungeon.area_1_d()

        elif input == 'a':
            dungeon.area_1_e()

        elif input == 'i':
            print inventory
            print ' '
            input = raw_input("What is your choice?: ").lower()
            continue    

        else:
            return

def area_1_d(self):
    print "You push open an iron door, behind it lies a room decorated in vines and insects."
    print "Out of the corner of your eye, you notice a chest.  You walk towards it. "
    input = raw_input("Press [Y] to open chest.: ").lower()
    if input == 'y':
        x = random.randint(1, 20)
        global dungeon_area_1
        if x >= 10:
            print "You open the chest and reveal 20 gold!"
            inventory["Gold"] = (inventory["Gold"] + 20)
            print "You turn around and walk out the door, back to the hub area. "
            dungeon = dungeon_area_1()
            dungeon.area_1_c()

        elif x < 10:
            print "You open the chest and reveal 5 gold!"
            inventory["Gold"] = (inventory["Gold"] + 5)
            print "You turn around and walk out the door, back to the hub area. "
            dungeon = dungeon_area_1()
            dungeon.area_1_c()

很抱歉,如果代码很乱,什么不是。距离完成还很远。

1 个答案:

答案 0 :(得分:0)

我解决了。感谢您的帮助,特别是Tom Karzes。我将布尔变量更改为全局,然后注意global bool,我能够根据用户选择的[D]更改全局变量,并说:

elif input == 'd': global bool bool = True

它现在按照我想要的方式工作。