0

I want to write a program that let's you move a character on the screen, basically, I will improve on this to hopefully make a sort of game. One problem I'm having trouble deleting the previously printed character, it just leaves a trail behind otherwise.

I've tried making a variable (actually 2) to keep track of the printed character and delete it (put space instead) when a new one is printed, which I couldn't get to work.

#include <ncurses.h>
using namespace std;

int x=10,y=10;
void pro(char dr)
{
    switch (dr)
    {
        case 'u':
            move(--y, x);
            break;
        case 'd':
            move(++y, x);
            break;
        case 'r':
            move(y, ++x);
            break;
        case 'l':
            move(y, --x);
            break;
    }
    addch('#');
}
int main()
{
    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    int in;
    border(0, 0, 0, 0, 0, 0, 0, 0);
    while (true)
    {
        in = getch();
        switch (in)
        {
            case KEY_UP:
                pro('u');
                break;
            case KEY_DOWN:
                pro('d');
                break;
            case KEY_RIGHT:
                pro('r');
                break;
            case KEY_LEFT:
                pro('l');
                break;
        }
    }
    endwin();
}

This code currently leaves a trail behind, and I want it to delete the previous one.

alpkaan35
  • 23
  • 5
  • `pro` stands for protagonist, if anyone's wondering. – alpkaan35 Sep 08 '19 at 12:13
  • Hmmm. My ncurses programs use 'wrefresh' to 'push' the changes (to the window) out to the display. I do not see any here, but then, I have never tried ncurses with NO wrefreshes ... "The refresh and wrefresh routines (or wnoutrefresh and doupdate) must be called to get actual output to the terminal, as other routines merely manipulate data structures. The routine wrefresh copies the named window to the physical terminal screen, taking into account what is already there to do optimizations." – 2785528 Sep 08 '19 at 14:22
  • I saw that too, but it works without them for me, no idea how. – alpkaan35 Sep 08 '19 at 17:32
  • I am unable to get this to compile on Linux ... did you try adding a refresh() to the end of pro()? – 2785528 Sep 08 '19 at 18:59

1 Answers1

0

If I get you right, you want to keep only the present position on the screen denoted with #, not the trail. To achieve this put mvaddch(y, x, ' ') just above switch(dr)

void pro(char dr) {
  mvaddch(y, x, ' ');
  switch (dr) {
    ...
  }
  addch('#');
}

Also consider mvdelch(y, x) if there is a use case for it.

Tom Trebicky
  • 638
  • 1
  • 5
  • 11
  • I'm already using curses (ncurses), and there are no new lines or carriage returns. Using carriage returns the point back to the start of the line, if I'm not mistaken. That means it would ruin this, but if that's what it does, please inform me. – alpkaan35 Sep 08 '19 at 12:21
  • Plus, I already print it out the moment the button's pressed. – alpkaan35 Sep 08 '19 at 12:28
  • @alpkaan35 that should do it. – Tom Trebicky Sep 08 '19 at 12:40
  • Instead of `mvdelch`, `mvaddch(x, y, ' ')` works better because that way, it doesn't delete the frame. – alpkaan35 Sep 08 '19 at 13:11