我正在制作一个基于文本的游戏,其中有一个选项可以为他们的角色选择一个类。目前,玩家输入他们的选项,要么输入数字,要么输入班级名称。它运作良好。
但是,我想让玩家使用箭头键导航菜单并使用“输入”键选择一个选项。为了明确他们将选择哪个选项,我还希望突出显示所选选项的文本。如果你曾经玩过 ASCII roguelike,你就会知道它是什么样的。
这是我目前拥有的类代码:
def character():
print "What is your class?"
print "1. The sneaky thief."
print "2. The smarty wizard."
print "3. The proletariat."
charclass = raw_input("> ")
if charclass == "1" or "thief":
charclass = thief
print "You are a thief!"
elif charclass == "2" or "wizard":
charclass = wizard
print "You are a wizard!"
elif charclass == "3" or "prole":
charclass = prole
print "You are a prole!"
else:
print "I'm sorry, I didn't get that"
Run Code Online (Sandbox Code Playgroud)
谢谢!
正如评论中已经提到的那样,您可以使用curses。这是一个小的工作菜单来实现你想要的
import curses
classes = ["The sneaky thief", "The smarty wizard", "The proletariat"]
def character(stdscr):
attributes = {}
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
attributes['normal'] = curses.color_pair(1)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
attributes['highlighted'] = curses.color_pair(2)
c = 0 # last character read
option = 0 # the current option that is marked
while c != 10: # Enter in ascii
stdscr.erase()
stdscr.addstr("What is your class?\n", curses.A_UNDERLINE)
for i in range(len(classes)):
if i == option:
attr = attributes['highlighted']
else:
attr = attributes['normal']
stdscr.addstr("{0}. ".format(i + 1))
stdscr.addstr(classes[i] + '\n', attr)
c = stdscr.getch()
if c == curses.KEY_UP and option > 0:
option -= 1
elif c == curses.KEY_DOWN and option < len(classes) - 1:
option += 1
stdscr.addstr("You chose {0}".format(classes[option]))
stdscr.getch()
curses.wrapper(character)
Run Code Online (Sandbox Code Playgroud)
最后一次调用getch只是为了让您可以在程序终止之前看到结果