Using arrow keys to scroll down through inputs

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.

2 Likes

It breaks for me » https://replit.com/@SalladShooter/AptMediumCharacter#main.py
Line 25

global opt,breaks

Done. I fixed it, it should work fine now.

Simpler version I made:

from typing import Iterable, Tuple

import cursor
from getkey import getkey, keys


def clear() -> None:
    """Clear the terminal."""
    print(end="\033[H\033[2J", flush=True)


def menu(prompt: str, options: Iterable[str]) -> Tuple[int, str]:
    cursor.hide()

    selectedOption = 0
    key = None

    while key != keys.ENTER:
        clear()
        print(prompt)
        for i in range(len(options)):
            option = f" {i + 1}. {options[i]}"
            print(f"> {option}" if i == selectedOption else f"  {option}")

        key = getkey()
        if key in {keys.UP, keys.W}:
            selectedOption = len(options) - 1 if selectedOption == 0 else selectedOption - 1
        elif key in {keys.DOWN, keys.S}:
            selectedOption = 0 if selectedOption == len(options) - 1 else selectedOption + 1
        elif key.isdigit() and int(key) <= len(options) and int(key) > 0:
            selectedOption = int(key) - 1

    for i in range(len(options)):
        if i == selectedOption:
            cursor.show()
            return i, options[i]
5 Likes