gpt4 book ai didi

python - 如何使用箭头键使 Python 中的菜单可导航

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:11 25 4
gpt4 key购买 nike

我正在制作一款基于文本的游戏,其中包含为角色选择职业的选项。目前,玩家输入他们的选项,要么输入数字,要么输入职业名称。它工作得很好。

但是,我想让玩家使用箭头键导航菜单并使用“回车”键选择一个选项。为了清楚他们将要选择哪个选项,我还希望突出显示所选选项的文本。如果您曾经玩过 ASCII roguelike,您就会知道它的样子。

这是我目前的类(class)代码:

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"

谢谢!

最佳答案

正如评论中已经提到的,您可以使用诅咒。这是一个小的工作菜单来实现你想要的

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)

最后一次调用 getch 只是为了在程序终止前看到结果

关于python - 如何使用箭头键使 Python 中的菜单可导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com