#include<stdlib.h> #include<ncurses,h> #define plane '*' char plane(int x,int y); int main() { int x = 5; int y = 10; char ch; initscr(); cbreak(); noecho(); clear(); do { clear(); picture(x,y); ch = getch(); switch(ch) { case 'w': picture(x,y); y = y - 1; break; case 's': picture(x,y); y = y - 1; break; case 'a': picture(x,y); x = x - 1; break; case 'd': picture(x,y); x = x + 1; break; } }while(ch != 'q'); endwin(); exit(0); } char plane(int x,int y) { int i; for(i = 0;i<y;i++) { mvprintw(i,x,"%c",'|'); refresh(); } mvaddch(y,x,PLANE); mvaddch(y+1,x-2,PLANE); mvaddch(y+1,x-1,PLANE); mvaddch(y+1,x,PLANE); mvaddch(y+1,x+1,PLANE); mvaddch(y+1,x+2,PLANE); mvaddch(y+2,x-1,PLANE); mvaddch(y+2,x+1,PLANE); }
目前只实现了一个移动的飞机,还不算一个游戏,后续会对代码进行改进。
参考博客:https://www.imooc.com/article/24381