Honestly, you should just use input()
but some games just want to be fancy. I mean, you gotta win the over the player over this game that you made. Every game maker wants their game to become popwular. So obviously you gotta impress them with something not just a dumb little input()
function for your inputs then comes the if statements, you need something more catchy.
from termcolor import colored
from colorama import Fore as F
from colorama import Style as S
from getkey import getkey,keys
from replit import clear
def enter_to_continue():
print(f'{S.BRIGHT}|{F.BLUE}Enter{F.WHITE}|To Continue{S.RESET_ALL}')
input()
def getinput(menu):
while True:
global opt,breaks
bright = S.BRIGHT
normal = S.NORMAL
reset = S.RESET_ALL
blue = F.BLUE
white = F.WHITE
purple = F.MAGENTA
green = F.GREEN
dim = S.DIM
opt = ''
selected = 0
solution_get_input = 0
key = ""
debug = False
breaks = False
if breaks == True:
break
solution_get_input = 0
selected = 0
key = ""
debug = False
while True:
try:
for i, item in enumerate(menu):
if i == selected:
print(colored(f"{bright}> {bright}[{F.GREEN}{i if debug else i+1}{S.RESET_ALL}] {menu[i]}{normal}"))
else:
print(f" {bright}[{F.YELLOW}{i if debug else i+1}{S.RESET_ALL}] {menu[i]}{normal}")
key = getkey()
if key.isdigit():
solution_get_input = int(key)
break
elif key == keys.UP:
clear()
selected = (selected - 1) % len(menu)
if selected == -1:
selected = (selected + len(menu)+1) % len(menu)
elif key == keys.DOWN:
clear()
selected = (selected + 1) % len(menu)
if selected > len(menu):
selected = (selected - len(menu)-1) % len(menu)
elif key == "\n":
solution_get_input = selected
opt = menu[solution_get_input]
clear()
else:
clear()
if opt == 'hello world':
print('hi!')
enter_to_continue()
breaks = True
clear()
break
elif opt == 'Hewwo':
print('Yeah whatever. Hello!')
enter_to_continue()
breaks = True
clear()
break
elif opt == 'I am cat':
print('No you are just imagining that ur a cat lolz')
enter_to_continue()
breaks = True
clear()
break
#add more elif statements as you go!
except Exception as e:
print("An exception occurred.")
# Save state here.
raise e
menu = ['hello world','Hewwo','I am cat']
getinput(menu)
see what happens, its beautiful.
I also added a else statement to clear the screen if the user presses anything else.