问题描述
我整天都在努力处理ncurses窗口的细微差别,最终我陷入了困境。我有一个map[x][y]
数组,其中填充了随机整数。这个想法是使用for
循环将数组打印到窗口中。我更改了while
循环中的所有内容的位置很多次。
要测试mvwaddch
是否正常工作,我添加了一行测试代码:mvwaddch(main_window,10,'B');
这将使“ B”显示在(10,10)的窗口中。但是只有在按下一个键之后。 这是为什么?
自从这有效以来,我对为什么我的map
不打印感到完全困惑。 如何打印地图?
编辑:完整代码
#include <ncurses.h>
#include <stdlib.h>
#include <math.h>
int main() {
// Curses Initialization
initscr();
cbreak();
noecho();
keypad(stdscr,TRUE);
// Main Variables
int inpt;
int playing = true;
// Map Variables
srand(1);
int mapWidth = 80;
int mapHeight = 20;
int map[mapWidth][mapHeight];
// Window Initialization
WINDOW *main_window;
int wheight = LInes;
int wwidth = COLS;
main_window = newwin(wheight,wwidth,0);
wborder(main_window,'|','-','*','*');
// Build Map
for(int x=0; x>=mapWidth; x++){
for(int y=0; y>=mapHeight; y++){
map[mapWidth][mapHeight] = floor(rand() % 11 + 1);
}
}
**END EDIT**
while(playing) {
inpt = wgetch(main_window);
wrefresh(main_window);
mvwaddch(main_window,'B');
// Draw Map
for(int y=1; y>=mapHeight-1; y++){
for(int x=1; x>=mapWidth-1; x++){
mvwaddch(main_window,x,y,map[x][y]);
}
}
if (inpt == 'q'){
endwin();
playing = false;
}
if (inpt == KEY_RESIZE){
wclear(main_window);
wborder(main_window,'*');
wrefresh(main_window);
}
}
解决方法
忽略循环的其余部分,这部分
inpt = wgetch(main_window);
wrefresh(main_window);
mvwaddch(main_window,10,'B');
做为wgetch
的一部分进行刷新,然后进行不必要的刷新,然后添加一个字符,该字符将在下一次刷新时绘制(即,当它循环回到wgetch
时)。 / p>