在Python3x中创建一个始终可用的文本菜单

时间:2014-04-26 21:06:59

标签: python python-3.x menu

我是python的新手并且很快学习。谢谢大家的帮助。

我正在尝试创建一个始终在讲故事文本RPG的背景中运行的文本菜单。我搜索过,无法找到如何创建“永远在线”菜单或者如何工作的解释。

我希望玩家能够在游戏中随时点击“m”并显示菜单提示。

到目前为止,我已经创建了一个“userinput”函数以及一个“菜单”函数,每当游戏提示用户/玩家输入时,它都会被部署。

def menu():
    print('Press "1" for map >>> "2" for stats >>> "3" for exit')
    choice = input()
    if choice == '1':
        print('map needs to be made and shown')
    elif choice == '2':
        print('stats need to be made and assinged to choice 2 in def menu')
    elif choice == '3':
        print('You are exiting the menu. Press "M" at any time to return to the menu')
        return
    else:
        print('I did not recognize your command')
        menu()

def userinput():
    print('Press 1 to attack an enemy >>> 2 to search a room >>> 3 to exit game')
    print('Press "M" for menu at any time')
    inputvalue = input()
    if inputvalue == 'm':
        menu()
    elif inputvalue == '1':
        print('attack function here')
    elif inputvalue == '2':
        print('search function here')
    elif inputvalue == '3':
        exit
    else:
        userinput()

这似乎不是一个理想的解决方案,因为用户无法随时选择查看地图或退出游戏。

有没有办法让菜单始终在后台运行?

我想过使用一个永远不会关闭的while循环,并且所有游戏都会在while循环中保持,但这看起来并不经济。

任何想法或帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我抓了一把。这可能不是做你正在寻找的最好的结构,但我不希望我的回复太复杂。

任何具有UI的“标准”方法都是将模型,视图和控件分开。在线查看MVC架构。虽然它在开始时增加了复杂性,但从长远来看,它可以让生活变得更加简单。

其他注意事项是:

一些代码

# flag we set when we're done
finished = False

def finish():
    # ask the user for confirmation?
    global finished
    finished = True
    return


def handle_menu_input(choice):
    handled = True

    if choice == '1':
        print('map needs to be made and shown')
    elif choice == '2':
        print('stats need to be made and assinged to choice 2 in def menu')
    else:
        handled = False

    return handled

def menu():

    finished_menu = False
    while not finished_menu:

        print('Press "1" for map >>> "2" for stats >>> "3" for exit')
        choice = raw_input() # NOTE: changes behaviour in Python 3!

        if handle_menu_input(choice):
            # done
            pass
        elif choice == '3':
            print('You are exiting the menu. Press "M" at any time to return to the menu')
            finished_menu = True
        else:
            print('I did not recognize your command')
            menu()
    return


def userinput():
    print('Press 1 to attack an enemy >>> 2 to search a room >>> 3 to exit game')
    print('Press "M" for menu at any time')
    choice = raw_input() # NOTE: changes behaviour in Python 3!

    if choice == 'm':
        menu()
    elif choice == '1':
        print('attack function here')
    elif choice == '2':
        print('search function here')
    elif choice == '3':
        finish()

    # elif handle_menu_input(choice):
    #     # delegate menu functions?? ..
    #     # do this if you want to see maps anytime without going through the menu?
    #     # otherwise comment this elif block out.
    #     # (Problem is 1, 2 etc are overloaded)
    #     pass
    else:
        print('I did not recognize your command')
    return


def main():
    # main loop
    while not finished:
        userinput()
    return

if __name__ == "__main__":
    main()