上节代码示例:
#include <curses.h>void initNcurse()
{initscr();keypad(stdscr,1);
}void gamePic()
{int hang;int lie;for(hang = 0;hang < 20;hang ++){if(hang == 0){for(lie = 0;lie < 20;lie ++){printw("--");}printw("\n");for(lie = 0;lie <= 20;lie ++){if(lie == 0 || lie == 20) printw("|");else printw(" ");}printw("\n");}if(hang > 0 && hang <= 18){for(lie = 0;lie <= 20;lie ++){if(lie == 0 || lie == 20) printw("|");else printw(" ");}printw("\n");}if(hang == 19){for(lie = 0;lie < 20;lie ++){printw("--");}printw("\n");printw("by beiweiqiuAC");}}}int main()
{initNcurse();gamePic();getch();//防止程序退出endwin();return 0;
}
代码可以优化更简:
#include <curses.h>void initNcurse()
{initscr();keypad(stdscr,1);
}void gamePic()
{int hang;int lie;for(hang = 0;hang < 20;hang ++){if(hang == 0){for(lie = 0;lie < 20;lie ++){printw("--");}printw("\n");}if(hang >= 0 && hang <= 19){for(lie = 0;lie <= 20;lie ++){if(lie == 0 || lie == 20) printw("|");else printw(" ");}printw("\n");}if(hang == 19){for(lie = 0;lie < 20;lie ++){printw("--");}printw("\n");printw("by beiweiqiuAC");}}}int main()
{initNcurse();gamePic();getch();//防止程序退出endwin();return 0;
}
那我们为什么需要这样做呢?
因为我们到时候不仅仅显示的是地图,还有贪吃蛇的身体和食物,相当于我们在这块空间不停扫描,如果用printw写死有些地方就不能显示空格了。