我的窗口在ncurses dosn中显示任何内容

时间:2016-06-18 19:40:43

标签: c debian ncurses

我最近为我的项目使用ncurses,我是新手。 我在一个窗口中编写了一个代码来打印我的宇宙飞船(3个字符串行),但是它显示了我用来在新窗口中打印船只以及其他用于删除窗口()的任何东西。这是我的代码:

#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);
}

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

  while((ch = getch()) != 'q')

stdscr读取,而您想在space_ship上撰写文字。如果您将其更改为

  while((ch = wgetch(space_ship)) != 'q')

然后space_ship的刷新将作为wgetch电话的副作用完成。