首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >全球范围不变

全球范围不变
EN

Stack Overflow用户
提问于 2022-02-16 20:32:33
回答 1查看 32关注 0票数 0

我有一个使用python的基于文本的游戏。我的问题是,我不能在房间之间移动,因为一个变量没有按需要改变。

我使用了global,这导致了另一个错误。守则如下:

代码语言:javascript
复制
#The dictionary links a room to other rooms and items.
rooms = {
        'Holding Cell': {'South': 'Training room','North': 'Cupboard', 'West': 'Study' },
        'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
        'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
        'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
        'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
        'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
        'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
        'Dungeon Exit': {}
    }
starting_room = 'Holding Cell'
current_room = starting_room

inventory = []
inventory1 = ['Health Potion', 'Sword and shield', 'Mind key', 'Soul key', 'Armor set', 'Body key']
inventory1.sort()
health = 100



def status():
    inventory.sort()
    print('-----------------------------')
    print("Inventory: ", inventory)
    print("Health: ", str(health))
    print("Current room: ", current_room)

def main():
    rooms = {
        'Holding Cell': {'South': 'Training room', 'North': 'Cupboard', 'West': 'Study'},
        'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
        'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
        'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
        'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
        'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
        'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
        'Dungeon Exit': {}
    }
    status()


    current_room = starting_room

    direction = input("Enter 'North/South/East/West' to move or 'Exit': ")

        # user to exit
    if direction == 'Exit':
        print("Thanks for playing!")
        exit(0)


        # a valid move
    elif direction in rooms[current_room]:
        current_room = rooms[current_room][direction]

        # invalid move
    else:
        print("Invalid Move. There's no room to the {}".format(direction))



def show_instructions():
    print("Type 'North', 'South', 'East', 'North' to go in a direction. Type 'Exit' to leave the game!")
    print("To get an item type 'Get (item).")
#provides instructions to player
show_instructions()

while 1:
    main()
EN

回答 1

Stack Overflow用户

发布于 2022-02-16 22:53:14

第一个问题:在global函数中正确使用main

代码语言:javascript
复制
def main():   
    status()

    global current_room  # <--- here
    current_room = starting_room  # this is already defined outside the function, so really isn't needed

    direction = input("Enter 'North/South/East/West' to move or 'Exit': ")

    if direction == 'Exit':
        print("Thanks for playing!")
        exit(0)
    elif direction in rooms[current_room]:
        current_room = rooms[current_room][direction]
    else:
        print("Invalid Move. There's no room to the {}".format(direction))

第二个问题:您每次都会通过current_room = starting_room函数重新设置main,而不是正确地维护您的状态。

修复似乎是删除这一行,只是提示方向输入,并让流传递到elif块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71148922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档