0

I am coding this very simple game to learn C on UNIX OSs. The 'C' character (the chicken), which is controlled by the player in the window, is followed by one or two predecessors. How should erase them?

#include <time.h>
#include <ncurses.h>
WINDOW *create_newwin(int h, int l, int y, int x)
{   WINDOW *local_win;

    local_win = newwin(h, l, y, x);
    box(local_win, 0 , 0);      
    wrefresh(local_win);        

    return local_win;
}
int main(int argc, char **argv)
{
    WINDOW *win;
    int x, y, h, l;                    // window
    int px, py;                       // chicken
    int v1x;                  // car 1
    int ch, top=0;                           
    initscr();  
    cbreak();                   
    keypad(stdscr, TRUE);       
    nodelay(stdscr, TRUE);
    curs_set(0);                              

    h = 30;
    l = 60 ;
    y = (LINES - h) / 2;        
    x = (COLS - l) / 2; 
    py = 1;
    px = 30;
    v1x = 1;        

    while ((ch = getch()) != 'q' && (py != 10 || px != v1x)){                                

        if (clock() > top + 100  ){        
            top = clock();
            win = create_newwin(h, l, y, x);  // moving car 
            mvwhline(win, 10, 1, '.', 58);
            mvwaddch(win, 10, v1x, 'V');
            v1x++;
            if (v1x > 58)
                v1x = 1;
        }


        switch(ch){                            // moving chicken 
            case KEY_LEFT:
                px--;
                break;
            case KEY_RIGHT:
                px++;

                break;
            case KEY_UP:
                py--;
                break;
            case KEY_DOWN:
                py++;
                break;  
        }
        mvwaddch(win, py, px,'C');                  
        wrefresh(win);
    }


    endwin();
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
Fabien
  • 3
  • 2
  • 1
    Not sure what the question is, but "deleting" character can be done by writing a whitespace over it. – Eugene Sh. Feb 13 '19 at 19:22
  • Sorry my english is not very good, i wasn't sure about how to ask this question. The thing is the character that the player moves is followed by an older same character, i was wondering how to erase it. – Fabien Feb 13 '19 at 19:25
  • **before** you update `px` or `py` output a space. Alternatively, copy `px` and `py`, write the 'C', then write a space at the saved old `px` and `py` – pmg Feb 13 '19 at 19:45
  • SOLVED, I added mvwaddch(win, py, px,' '); right after wrefresh(win); and it works perfectly. Thank both of you! – Fabien Feb 13 '19 at 19:59

0 Answers0