我正在用C语言制作一个带有护士库的终端游戏。字符@可以通过WASD键在屏幕上移动。然而,我希望它不会留下任何痕迹,因为现在它会留下一个线索,如@@@@@@@。
有谁知道这个方法吗?谢谢!
下面是移动char的代码:
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();
您必须自己删除跟踪,即将其替换为@
传递之前的字符。
你必须跟踪@
之前的位置,然后打印被@
删除的字符,假设它是一个空格:
if (x == -1 && y == -1) { y += 1;x += 1;} //keep in grid
mvwprintw(stdscr,previous_pos_y,previous_pos_x," ");
mvwprintw(stdscr,y,x,"@");
}
不用说,您还必须跟踪这些字符,以便您可以还原它。