我在 ncurses 中的 window 没有显示任何内容
my window in ncurses dosn't display anything
我最近在我的项目中使用 ncurses,我是新手。
我写了一个代码在 window 中打印我的宇宙飞船(3 条线),但它没有显示任何我用来在新的 window 中打印飞船和其他删除 [=14] 的功能=]()。这是我的代码:
#include<stdio.h>
#include<ncurses.h>
#include <string.h>
#include<stdlib.h>
WINDOW * create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[]){
WINDOW* space_ship;
int max_y , max_x , startx , starty;
char ch;
initscr();
cbreak();
noecho();
getmaxyx(stdscr,max_y,max_x);
startx=(LINES - 3) / 2;
starty=(COLS - 5) / 2;
space_ship=create_newwin(3,5,starty,starty);
refresh();
while((ch = getch()) != 'q')
{ switch(ch)
{ case KEY_LEFT:
destroy_win(space_ship);
space_ship = create_newwin(3,5, starty,--startx);
break;
case KEY_RIGHT:
destroy_win(space_ship);
space_ship = create_newwin(3, 5, starty,++startx);
break;
case KEY_UP:
destroy_win(space_ship);
space_ship = create_newwin(3, 5,--starty,startx);
break;
case KEY_DOWN:
destroy_win(space_ship);
space_ship = create_newwin(3, 5, ++starty,startx);
break;
}
}
endwin();
return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx){
WINDOW* temp;
temp=newwin(height,width,starty,startx);
refresh();
wprintw(temp," ^\n");
wprintw(temp," (0)\n");
wprintw(temp,"[] []\n");
wrefresh(stdscr);
wrefresh(temp);
return temp;
}
void destroy_win(WINDOW *local_win){
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
wrefresh(local_win);
delwin(local_win);
}
问题出在这一行:
while((ch = getch()) != 'q')
正在从 stdscr
读取,而您想在 space_ship
上写入文本。如果您将其更改为
while((ch = wgetch(space_ship)) != 'q')
然后 space_ship
的刷新将作为 wgetch
调用的副作用完成。
我最近在我的项目中使用 ncurses,我是新手。 我写了一个代码在 window 中打印我的宇宙飞船(3 条线),但它没有显示任何我用来在新的 window 中打印飞船和其他删除 [=14] 的功能=]()。这是我的代码:
#include<stdio.h>
#include<ncurses.h>
#include <string.h>
#include<stdlib.h>
WINDOW * create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[]){
WINDOW* space_ship;
int max_y , max_x , startx , starty;
char ch;
initscr();
cbreak();
noecho();
getmaxyx(stdscr,max_y,max_x);
startx=(LINES - 3) / 2;
starty=(COLS - 5) / 2;
space_ship=create_newwin(3,5,starty,starty);
refresh();
while((ch = getch()) != 'q')
{ switch(ch)
{ case KEY_LEFT:
destroy_win(space_ship);
space_ship = create_newwin(3,5, starty,--startx);
break;
case KEY_RIGHT:
destroy_win(space_ship);
space_ship = create_newwin(3, 5, starty,++startx);
break;
case KEY_UP:
destroy_win(space_ship);
space_ship = create_newwin(3, 5,--starty,startx);
break;
case KEY_DOWN:
destroy_win(space_ship);
space_ship = create_newwin(3, 5, ++starty,startx);
break;
}
}
endwin();
return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx){
WINDOW* temp;
temp=newwin(height,width,starty,startx);
refresh();
wprintw(temp," ^\n");
wprintw(temp," (0)\n");
wprintw(temp,"[] []\n");
wrefresh(stdscr);
wrefresh(temp);
return temp;
}
void destroy_win(WINDOW *local_win){
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
wrefresh(local_win);
delwin(local_win);
}
问题出在这一行:
while((ch = getch()) != 'q')
正在从 stdscr
读取,而您想在 space_ship
上写入文本。如果您将其更改为
while((ch = wgetch(space_ship)) != 'q')
然后 space_ship
的刷新将作为 wgetch
调用的副作用完成。