Fiach ONeill
Fiach ONeill

Reputation: 155

How to stop trail behind moving char in NCurses?

I'm making a terminal game in C with the ncurses library.The char @ gets moved around the screen with the WASD keys. However, I want it so that it leaves no trail behind it, as of now it leaves a trail such as @@@@@@@.

Does anyone know a way about this? Thanks!

Below is the code for moving the 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();

Upvotes: 0

Views: 138

Answers (1)

anastaciu
anastaciu

Reputation: 23802

You will have to delete the trail yourself, i.e. replace it with the character that was there before @ passed.

You have to keep track of the position were @ was before and there print the character that was deleted by @, let's say it was a 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,"@");
}

Needless to say you must also keep track of such character so you can restore it.

Upvotes: 3

Related Questions