如何着色ncurses中的选择?

时间:2014-04-05 17:44:29

标签: c++ ncurses

我现在在ncurses和C ++中使用Roguelike。现在我正在编写标题屏幕,它看起来像这样:

game name
company name

(n)ew game
(q)uit

但我真的希望用户能够使用箭头键突出显示他们的选择,并可能稍后在库存屏幕中重复使用此功能。问题是我无法弄清楚如何着色新游戏而不是在选择时退出,反之亦然。到目前为止我的代码是这样的:

mvaddstr((height-1)/2, ((width-4)/2)-(newgame_button.length()/2),newgame_button.c_str());
mvaddstr((height+1)/2, ((width-4)/2)-(quit_button.length()/2),quit_button.c_str());
mvaddstr((height-10)/2, ((width-4)/2)-(titlename.length()/2), titlename.c_str());
mvaddstr((height-8)/2, ((width-4)/2)-(companyname.length()/2), companyname.c_str());

然后我有我的密钥处理程序。我试着这样做:

if(ch == KEY_DOWN) {
    start_color();          
init_pair(1, COLOR_BLUE, COLOR_BLACK);
attron(COLOR_PAIR(1));
attroff(COLOR_PAIR(1));
}

但它不起作用。我对ncurses很新,所以它可能是我忽略的一件非常明显的事情。谢谢!

1 个答案:

答案 0 :(得分:1)

完成简单菜单的最佳/最简单的方法是每次更改选择时重绘标题屏幕(例如,通常按rp_arrow down_arrow)。我没有太多时间来刷新我的ncurses,所以这里是伪代码。将变量保持选择与必须突出显示的文本相关联。它就是这样。

#selecion=0;
#while(key_pressed != ENTER)
    #print game title
    #print company name
    #if selection = 0 print highlighted new game
    #else print new game without highlight
    #if selection = 1 print highlighted quit
    #else print quit without highlight
    #if uparrow selection++
    #if downarrow selection--

我知道它并不完美,你必须运用你的输入和绘制新屏幕的逻辑,但这只是简单,突出显示菜单背后的一般概念。

如果您需要任何帮助,请在此处写一下,我会深入了解一些代码/内存以获取详细信息和提示。祝你的游戏好运!