Question

I'm working on a Roguelike right now in ncurses and C++. Right now I'm coding the title screen, it looks something like this:

game name
company name

(n)ew game
(q)uit

But I'd really like for the user to be able to use the arrow keys to highlight their selection and maybe reuse this functionality later for the inventory screen. The problem is I can't figure out how to colorize new game and not quit when its selected and vice versa. So far my code is like this:

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());

Then I have my key handler. I tried to do it like this:

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

But it doesn't work. I'm pretty new to ncurses so it could be something totally obvious that I am overlooking. Thanks!

Was it helpful?

Solution

The best/easiest way to accomplish simple menu is do redraw title screen every time you change your selection( like press rp_arrow down_arrow, usually ). I don't have much time to refresh my ncurses so here's pseudocode. You associate variable holding selection with text that has to be highlighted. And it goes like that.

#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--

I know it's not perfect and you have to work your logic of getting input and drawing fresh screen but that's just a general idea behind simple, highlighted menu.

If you need any help just write here and I'll dig deeper into some of my code/memory for details and tips. Good luck with your game!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top