Question:
So I am making a City Builder game that will be using an arrow select menu. My current one isn’t working.
Repl link:
https://replit.com/@SalladShooter/City-Builder#main.py
arrow_select.py
import keyboard
def arrow_select(options):
selection = 0
while True:
for i in range(len(options)):
if selection == i:
print(f"> {options[i]}")
else:
print(options[i])
key_event = keyboard.read_event()
if key_event.name == 'down':
selection = (selection + 1) % len(options)
elif key_event.name == 'up':
selection = (selection - 1) % len(options)
elif key_event.name == 'enter':
return options[selection]
main.py
import pyfiglet
from arrow_select import arrow_select
# Colors ->
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"
# Background Colors ->
BACK_BLACK = "\u001b[40m"
BACK_RED = "\u001b[41m"
BACK_GREEN = "\u001b[42m"
BACK_YELLOW = "\u001b[43m"
BACK_BLUE = "\u001b[44m"
BACK_MAGENTA = "\u001b[45m"
BACK_CYAN = "\u001b[46m"
BACK_WHITE = "\u001b[47m"
def print_title():
# ^ print a title
PPC = "City Builder"
ASCII = pyfiglet.figlet_format(PPC, font='big')
print(GREEN + BOLD + ASCII + END)
print_title()
options = ["Build", "Bank"]
arrow_select(options)
1 Like
SalladShooter:
keyboard.read_event()
umm I’ve never used that but you can see this post it has I think exactly what you want but also a couple extra features (clears the screen every time, when you press a number it makes that selected, and it hides the cursor) you can remove those parts if you want
Simpler version I made:
from typing import Iterable, Tuple
import cursor
from getkey import getkey, keys
def clear() -> None:
"""Clear the terminal."""
print("\033[H\033[2J", end="", 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]}"
pri…
oh and to get the selected option as a string use menu()[1]
to get option index use menu()[0]
@QwertyQwerty88 when I try to run it, it starts installing all of the things but it never finished. I let it run for 20+ minutes and it hasn’t worked, either Replit is being buggy or something else is happening.
are you on a crome book because that happens all the time right now im grounded so i don’t have my windows but when i was on windows it would always work
@AndrewMorris14 no I use an iPad
@QwertyQwerty88 I get an error of, ModuleNotFoundError: No module named cursor
Try installing it with pip?
@QwertyQwerty88 Could I do it where you type it in and it automatically goes there without pressing enter? I don’t think the arrow menu will work because I have waited a long time and when it tries to automatically add things it hasn’t.
Sky
September 24, 2023, 7:55am
9
Hello @SalladShooter , I’ve reviewed your code and made the necessary adjustments. Here’s the corrected version:
from typing import Iterable, Tuple
import curses
def menu(prompt: str, options: list) -> Tuple[int, str]:
selected_option = 0
curses.wrapper(draw_menu, prompt, options, selected_option)
return selected_option, options[selected_option]
def draw_menu(stdscr, prompt: str, options: list, selected_option: int):
stdscr.clear()
stdscr.addstr(0, 0, prompt)
while True:
for i, option in enumerate(options):
stdscr.addstr(2 + i, 2, f"{i + 1}. {option}", curses.A_BOLD if i == selected_option else curses.A_NORMAL)
stdscr.refresh()
key = stdscr.getch()
if key == curses.KEY_ENTER or key == 10:
return
elif key == curses.KEY_UP and selected_option > 0:
selected_option -= 1
elif key == curses.KEY_DOWN and selected_option < len(options) - 1:
selected_option += 1
prompt_text = "Choose an option:"
menu_options = ["Option 1", "Option 2", "Option 3"]
selected_index, selected_value = menu(prompt_text, menu_options)
print(f"You selected: {selected_value}")
Hey @Sky !
I haven’t been able to test Qwerty or your code because of the automatic installation when I click run. I have waited 20+ minutes but it never installs all the way therefore I can’t test anything.
1 Like
Sky
September 24, 2023, 10:49pm
11
Strange, try and create a new Repl.
@Sky I have tried but they don’t work either.
1 Like
Sky
September 24, 2023, 10:54pm
13
What exactly is happening, the Repl’s just won’t install the packages or?
@Sky it takes forever and doesn’t install them.
1 Like
Sky
September 24, 2023, 10:58pm
15
You might have to make a bug report, I don’t think that is normal.