|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 就是要努力呀 于 2020-3-9 12:37 编辑
- #include <stdio.h>
- #include <ncurses.h>
- int main(void)
- {
- initscr(); /* */
- cbreak(); /*初始化ncurses(再使用ncurses库的都要先调用这三个函数)*/
- noecho(); /* */
- curs_set(0); //隐藏光标
- move(10, 10); //移动光标
- addch('A'); //打印一个字符
- addstr("ABCD"); //打印一串字符串
- refresh(); //刷新屏幕
- getch(); //类似于getchar() 但getch()会在用户按键后立刻执行 不需要等待回车
- endwin(); //退出ncurses
- return 0;
- }
复制代码 感谢人造人大佬给我提出的ncurse函数库
今天在b站通过学习一位up主写flappybird这个游戏学习了ncurses库的一些使用方法,在这里分享给大家
ncurse库不是linux自带的库,所以需要在命令行执行 sudo apt-get install libncurses5-dev 安装这个库
在编译的时候也需要加上-lncurese选项连接这个库
以下是flappybird的代码 因为是一边跟着up写的 所以就没有注释
- #include <stdio.h>
- #include <ncurses.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <sys/time.h>
- #define BIRD 'o'
- #define PIPE '*'
- #define BLANK ' '
- typedef struct Pipe
- {
- int x;
- int y;
- struct pipe *next;
- }
- Pipe;
- Pipe *HEAD, *TAIL;
- int X, Y; //小鸟坐标
-
- void drop(int sig); 小鸟下坠
- int set_ticker(int n_msec); 设定时间发送信号
- void clear_bird(); 清楚小鸟之前的位置
- void draw_bird(); 打印出小鸟现在的位置
- void clear_pipe(int x, int y); 清除一个柱子之前的位置
- void draw_pipe(int x, int y); 打印一个柱子现在的位置
- void init_pipes(); 初始化柱子的链表
- void draw_pipes(); 打印所有柱子
- void clear_pipes(); 清除所有柱子
- void move_pipes(); 移动所有柱子
- void move_pipes()
- {
- Pipe *p;
- for(p = HEAD->next; p->next != NULL; p = p->next)
- {
- p->x--;
- }
- }
- void draw_pipes()
- {
- Pipe *p;
- for(p = HEAD->next; p->next != NULL; p = p->next)
- {
- draw_pipe(p->x, p->y);
- }
- }
- void clear_pipes()
- {
- Pipe *p;
- for(p = HEAD->next; p->next != NULL; p = p->next)
- {
- clear_pipe(p->x, p->y);
- }
- }
- void init_pipes()
- {
- HEAD = (Pipe *)malloc(sizeof(Pipe));
- if(HEAD == NULL)
- {
- exit(0);
- }
- HEAD->next = NULL;
- Pipe *temp, *p = HEAD;
- int i;
- for(i = 0; i < 5; i++)
- {
- temp = (Pipe *)malloc(sizeof(Pipe));
- if(temp == NULL)
- {
- exit(0);
- }
- temp->x = (i + 1) * 20;
- temp->y = rand()%16 + 10;
- p->next = temp;
- temp->next = NULL;
- p = temp;
- }
- TAIL = p;
- }
- void draw_pipe(int x, int y)
- {
- int i, j;
- for(i = 0; i < 5; i++)
- {
- for(j = 0; j < 20; j++)
- {
- move(y - j - 5, x + i);
- addch(PIPE);
- move(y + j + 5, x + i);
- addch(PIPE);
- }
- }
- }
- void clear_pipe(int x, int y)
- {
- int i, j;
- for(i = 0; i < 5; i++)
- {
- for(j = 0; j < 20; j++)
- {
- move(y - j - 5, x + i);
- addch(BLANK);
- move(y + j + 5, x + i);
- addch(BLANK);
- }
- }
- }
- void draw_bird()
- {
- move(X, Y);
- addch(BIRD);
- refresh();
- if((char)inch() == PIPE)
- {
- set_ticker(0);
- endwin();
- exit(0);
- }
- }
- void clear_bird()
- {
- move(X, Y);
- addch(' ');
- refresh();
- }
- int set_ticker(int n_msec)
- {
- struct itimerval timeset;
- long int n_sec, n_usec;
- n_sec = n_msec / 1000;
- n_usec = (n_msec % 1000) * 1000L;
- timeset.it_interval.tv_sec = n_sec;
- timeset.it_interval.tv_usec = n_usec;
- timeset.it_value.tv_sec = n_sec;
- timeset.it_value.tv_usec = n_usec;
- return setitimer(ITIMER_REAL, ×et, NULL);
- }
- void drop(int sig)
- {
- clear_bird();
- X++;
- draw_bird();
- clear_pipes();
- Pipe *p = HEAD->next;
- if(p->x < 0)
- {
- HEAD->next = p->next;
- free(p);
- Pipe *temp = (Pipe *)malloc(sizeof(Pipe));
- if(temp == NULL)
- {
- exit(0);
- }
- temp->x = 80;
- temp->y = 15;
- TAIL->next = temp;
- temp->next = NULL;
- TAIL = temp;
- }
- move_pipes();
- draw_pipes();
- }
- int main(void)
- {
- int i;
- char ch;
- X = 5;
- Y = 10;
- initscr();
- cbreak();
- noecho();
- curs_set(0);
- srand(time(NULL));
-
- signal(SIGALRM, drop);
- set_ticker(500);
- init_pipes();
- while(true)
- {
- clear_bird();
- draw_bird();
- clear_pipes();
- draw_pipes();
- if((ch = getch()) == ' ')
- {
- clear_bird();
- X--;
- }
- else if(ch == 'q' || ch == 'Q')
- {
- break;
- }
- }
- endwin();
-
- return 0;
- }
复制代码 当然自己也有很多没有弄明白的地方
<sys/time.h>头文件
signal函数 signal(SIGALRM, drop)(我知道这个函数的意思是接受数据就执行drop函数)
还有set_ticker这个函数中定义的 itimerval 结构体
以及 setitimer函数 setitimer(ITIMER_REAL, &TIMESET, NULL) (指定多少时间后就发送信号)
因为视频中对这几个函数都是略过或者根本没讲 所以一点也没搞懂
|
|