如何停止在 NCurses 中移动字符的踪迹?

How to stop trail behind moving char in NCurses?

我正在使用 ncurses 在 C 中制作一个终端游戏 library.The char @ 使用 WASD 键在屏幕上移动。但是,我希望它不留下任何痕迹,截至目前,它会留下诸如@@@@@@@之类的痕迹。

有人知道这方面的方法吗? 谢谢!

下面是移动字符的代码。

init_Curses(); //start ncurses
mvwprintw(stdscr, y, x, "@");
curs_set(0);

while (1)
{
     refresh();
     direction = getchar();
     switch (direction)
     {
     //proccess movement with WASD controls
     case 'w':
          y -= 1;
          break;
     case 'a':
          x -= 1;
          break;
     case 's':
          y += 1;
          break;
     case 'd':
          x += 1;
          break;
     default:
          continue;
     }
     if (x == -1 && y == -1)
     {
          y += 1;
          x += 1;
     } //keep in grid
     mvwprintw(stdscr, y, x, "@");
}
endwin();

您必须自己删除线索,将其替换为@之前的字符通过.

你必须跟踪 @ 之前的位置,然后打印 删除 的字符 @,假设它是一个 space:

if (x == -1 && y == -1) { y += 1;x += 1;} //keep in grid
    mvwprintw(stdscr,previous_pos_y,previous_pos_x," ");
    mvwprintw(stdscr,y,x,"@");
}

不用说,您还必须跟踪此类角色,以便您可以恢复它。