Question

I am new to python and learning quickly. Thank you all for the help.

I am attempting to create a text menu that will always run in the background of a storytelling text rpg. I have searched and cannot find an explanation of how to create an "always on" menu or how one would work.

I would like the player to be able to hit "m" at any time in the game and have the menu prompt show up.

So far, I have created a "userinput" function as well as a "menu" function that will be deployed each time the game prompts the user/player for input.

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()

This does not appear to be an ideal solution because the user cannot choose to view a map or exit the game at any time they want.

Is there a way to have a menu always running in the background?

I thought of using a while loop that would never close and all of the game would be held within that while loop but that doesn't seem economical by any means.

Any thoughts or help would be appreciated.

Was it helpful?

Solution

I took a stab at it. This is perhaps not the best structure for doing what you're looking for but I don't want my reply to get too complicated.

The "standard" approach for anything with a UI is to separate the model, the view and the control. Check out MVC architecture online. While it adds complexity at the start it makes life much simpler in the long run for anything with a non trivial UI.

Other points of note are:

Some code

# 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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top