首页 > 解决方案 > 为什么我的窗口每次移动时都不会覆盖它自己?

问题描述

所以我试图在城市防御游戏中拥有一个“阻挡者”

void *defense( void *maxBuildingHeight ){
    // the height to put the defense; 2 above the highest building
    int *defense_row = ((int *)maxBuildingHeight);
    int MAXX, MAXY;
    getmaxyx(stdscr,MAXY,MAXX); // screen rows and cols
    WINDOW *inputwin = newwin(1,5,(MAXY - (*defense_row + 2)),MAXX/2); //create the window for the defense
    char blocker[] = "#####";
    wprintw(inputwin, blocker); //the defense
    wrefresh(inputwin);
    keypad(inputwin, true); // collect the user input
    int newX = MAXX/2-2; // the left most position of the defense
    BlockerYSpot = (MAXY - (*defense_row + 2));

    while ( 1 ){
        int input = wgetch(inputwin);
        if (input == KEY_LEFT && (newX - 1) >= 0){
            newX--;
            pthread_mutex_lock(&mutex);
            mvwin(inputwin, BlockerYSpot, newX);
            wrefresh(inputwin);
            pthread_mutex_unlock(&mutex);
        }
        else if (input == KEY_RIGHT && (newX + 5) < MAXX){ // its +5 due to the length of the blocker
            newX++;
            pthread_mutex_lock(&mutex);
            mvwin(inputwin, BlockerYSpot, newX);
            wrefresh(inputwin);
            pthread_mutex_unlock(&mutex);
        } else if (input == 'q'){
            delwin(inputwin);
            endwin();
            exit( EXIT_FAILURE );
        }
        CurrentBlockerXSpot = newX;
    }//while
}

我当前的输出没有左右移动 在此处输入图像描述

当我将阻塞器向左移动时,我的输出,您可以看到它不会擦除前一个阻塞器。 在此处输入图像描述

标签: cprintingc99ncurses

解决方案


您正在明确刷新inputwin,而不是下面的窗口。在没有看到您的其余代码的情况下,我假设它是stdscr. refresh()(或wrefresh())“防御”下方的窗口以重新绘制inputwin曾经存在的区域。


推荐阅读