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?
inventory: when doesmenuget 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.