C 为什么我的窗户每次移动时都不会写得太多?
所以我想在城市防守比赛中有一个“拦网者”C 为什么我的窗户每次移动时都不会写得太多?,c,printing,c99,ncurses,C,Printing,C99,Ncurses,所以我想在城市防守比赛中有一个“拦网者” 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 an
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
}
void*防御(void*最大建筑高度){
//防御的高度;高于最高建筑物2
int*国防_行=((int*)最大建筑高度);
int-MAXX,MAXY;
getmaxyx(stdscr,MAXY,MAXX);//屏幕行和列
WINDOW*inputwin=newwin(1,5,(MAXY-(*defense_row+2)),MAXX/2);//为防御创建窗口
煤焦阻滞剂[]=“#######”;
wprintw(inputwin,blocker);//防御
wrefresh(inputwin);
键盘(inputwin,true);//收集用户输入
int newX=MAXX/2-2;//防御的最左侧位置
BlockerYSpot=(最大值-(*defense_row+2));
而(1){
int input=wgetch(inputwin);
如果(输入=键左&&(newX-1)>=0){
纽克斯--;
pthread_mutex_lock(&mutex);
mvwin(inputwin、BlockerYSpot、newX);
wrefresh(inputwin);
pthread_mutex_unlock(&mutex);
}
如果(input==KEY_RIGHT&&(newX+5)
我的电流输出没有向左或向右移动
我的输出当我向左移动拦截器时,你可以看到它不会擦除前一个拦截器。
您正在显式刷新
inputwin
,但不是下面的窗口。在没有看到其余代码的情况下,我假设它是stdscr
refresh()
(或wrefresh()
)在“防御”下方的窗口中重新绘制inputwin
所在的区域。为什么函数签名会采用void*
,您会立即将其重铸为int*
?如果可以的话,只需重新声明它。看到这里使用pthread也很奇怪。通常通过一个简单的事件循环来实现这种逻辑。线程化会增加相当大的复杂性,这使得调试这类事情变得不必要的困难。因为这是一个线程化函数,这是一个项目,我无法控制设计。在这种情况下,线程不是问题。在您对该流投入过多之前,值得一看是否可以将其重新编写成一个简单的非阻塞事件循环。在这里使用线程会造成很多控制复杂性。如果您使用的是线程,那么一个线程,并且只有一个线程负责显示。另一个应该是操纵游戏状态的副本,如果/当游戏准备好渲染时,这会被关闭,但同步是非常重要的。