0

I'm working on an RPG style game, and I'm currently working on the inventory. I am not using any engines such as PyGame, but am using keyboard, os, and time. The way the inventory works it that it has a different function for each place the cursor can be, and if you press the up and down arrow keys, it will run the function corresponding to the place it would be, so that the cursor will be there. Please note that this is written for Windows 10.

import time
import os
import keyboard
def screenclear():
    os.system('cls')
def inventory():
    def menu():
        def weaponschoice():
            global choice
            global realchoice
            choice = 'weapons'
            screenclear()
            print('    >WEAPONS<')
            print('     ARMOUR')
            print('     ITEMS')
            print('     BACK')
            time.sleep(0.1)
            while True:
                if keyboard.is_pressed('up'):
                    backchoice()
                if keyboard.is_pressed('down'):
                    armourchoice()
                elif keyboard.is_pressed('z'):
                    realchoice = choice
                elif keyboard.is_pressed('x'):
                    realchoice = 'back'
        def armourchoice():
            global choice
            global realchoice
            choice = 'armour'
            screenclear()
            print('     WEAPONS')
            print('    >ARMOUR<')
            print('     ITEMS')
            print('     BACK')
            time.sleep(0.1)
            while True:
                if keyboard.is_pressed('up'):
                    weaponschoice()
                if keyboard.is_pressed('down'):
                    itemschoice()
                elif keyboard.is_pressed('z'):
                    realchoice = choice
                elif keyboard.is_pressed('x'):
                    realchoice = 'back'
        def itemschoice():
            global choice
            global realchoice
            choice = 'items'
            screenclear()
            print('     WEAPONS')
            print('     ARMOUR')
            print('    >ITEMS<')
            print('     BACK')
            time.sleep(0.1)
            while True:
                if keyboard.is_pressed('up'):
                    armourchoice()
                if keyboard.is_pressed('down'):
                    backchoice()
                elif keyboard.is_pressed('z'):
                    realchoice = choice
                elif keyboard.is_pressed('x'):
                    realchoice = 'back'
        def backchoice():
            global choice
            global realchoice
            choice = 'back'
            screenclear()
            print('     WEAPONS')
            print('     ARMOUR')
            print('     ITEMS')
            print('    >BACK<')
            time.sleep(0.1)
            while True:
                if keyboard.is_pressed('up'):
                    itemschoice()
                if keyboard.is_pressed('down'):
                    weaponschoice()
                elif keyboard.is_pressed('z'):
                    realchoice = choice
                elif keyboard.is_pressed('x'):
                    realchoice = 'back'
        weaponschoice()
        exec(realchoice + '()')

In future I'll add functions such as weapons(), armour(), items(), and back(), but I realised that I'll have to return from a random amount of recurring functions. How do I do that?

2
  • 1
    It's not clear why you are nesting functions like this. Say you call inventory: when does menu get called? Also, minimize your use of globals; either pass arguments to functions (and have the functions return values), or encapsulate your data in a class with methods that operate on instance data. You may want to start with the Python tutorial if you aren't familiar with these concepts. Commented Aug 6, 2019 at 20:28
  • 1
    Recursion is not the right tool for this job, or any job where the next choice is based on user input. Imagine I'm a user that is endlessly looping between functions. Eventually, the amount of memory I'm using will exceed the system's available memory and my game will crash. Instead, use loops. A good design would be to have one event handling loop that gets the users input and delegates the task to the specific function. Once that's been performed, get the next user input. Also, there is no need to nest your functions like that. Use a class to avoid global variables. Commented Aug 6, 2019 at 20:39

1 Answer 1

0

You should add a base condition to stop the function when the condition is met...or you can set the recursion limit by calling the inbuilt module (sys.setrecursionlimit(5).)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.