简体   繁体   English

无法使用 Ncurses mvchgat 为多行字符添加颜色

[英]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.我正在尝试使用循环为 Ncurses 中的视频游戏 map 着色,为多个字符设置特定颜色,它在单行上工作正常,但每当我尝试将颜色应用于多行时,它都不应用任何颜色,或者它只在最后一行应用颜色。

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.字符串中的\n会擦除下一行,从而清除视频属性。

mvprintw (and printw , etc), ultimately call waddch , which documents the behavior: mvprintw (和printw等),最终调用waddch ,它记录了行为:

  • 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. clrtoeol 然后将 cursor 移动到下一行的 window 左边距,如果在最后一行则滚动 window。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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