简体   繁体   中英

Cannot add color to character on multiple lines using Ncurses mvchgat

I'm trying to color a video game map in Ncurses using a loop to have a specific color for multiple characters, it works fine on a single line but whenever I try to apply color to multiple lines either it doesn't apply any color, or it only applies color on the last line.

Here's my code:

initscr();
start_color();
char *map =  strdup("OOOOOOOOOXOOOOOOOOO\nBXXXOXXXOXOXXXXOXXB\nOXXXOXXXOXOXXXXOXXO\nOOOOOOOOOOOOOOOOOOO\nOXXXOXOXXXXXOXXOXXO\nOOOOOXOOOXOOOXXOOOO\nXXXXOXXXOXOXXXXOXXX\nXXXXOXOOOOOOOXXOXXX\nXXXXOXOMMMMMOXXOXXX\nXXXXOOOMMMMMOOOOXXX\nXXXXOXOMMMMMOXXOXXX\nXXXXOXOOOOOOOXXOXXX\nXXXXOXOXXXXXOXXOXXX\nOOOOOOOOOXOOOOOOOOO\nOXXXOXXXOXOXXXXOXXO\nBOOXOOOOOOOOOOOOXOB\nXXOXOXOXXXXXOXXOXOX\nOOOOOXOOOXOOOXXOOOO\nOXXXXXXXOXOXXXXXXXO\nOOOOOOOOOOOOOOOOOOO");
init_pair(1, COLOR_CYAN, COLOR_BLACK);
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK);
int y = 0;
int x = 0;
while (true) {
    mvprintw(0, 0, map);
    for (int i = 0; y < 19; i++, x++) {
        if (x == 19) {
            y++;
            x = 0;
        }
        if (map[i] == 'O') {
            mvchgat(map[i], y, x, 1, A_BLINK, 2, NULL);
        }
        else if (map[i] == 'X') {
            mvchgat(y, x, 1, A_BLINK, 1, NULL);
        }
        else if (map[i] == 'B'){
            mvchgat(y, x, 1, A_BLINK, 3, NULL);
        }
        refresh();
    }

The \n in the string erases the next line(s), wiping out the video attributes.

mvprintw (and printw , etc), ultimately call waddch , which documents the behavior:

  • Newline does a clrtoeol , then moves the cursor to the window left margin on the next line, scrolling the window if on the last line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM