wordpress站点赏析,万网注册域名做简单网站,网站建站 seo,建设主题网站步骤最近看到大神在Linux下写的贪吃蛇代码#xff0c;用到了curses图形库#xff0c;可能很多人都没有用过#xff0c;分享出来给大家。在ubuntu下安装curses图形库命令sudo apt-get install libncurses5-dev双buff是一个非常优秀的机制#xff0c;之前写贪吃蛇的时候#xff… 最近看到大神在Linux下写的贪吃蛇代码用到了curses图形库可能很多人都没有用过分享出来给大家。在ubuntu下安装curses图形库命令sudo apt-get install libncurses5-dev
双buff是一个非常优秀的机制之前写贪吃蛇的时候如果不使用双buff屏幕跳动会很剧烈使用了双buff后体验就非常好。我们使用curses图形库也是一样如果没有调用refresh()函数之前显示的屏幕是不会进行更新的。比如下面这段代码#include unistd.h
#include stdlib.h
#include curses.h
int main()
{initscr();/* We move the cursor to the point (5,15) on the logical screen,print Hello World and refresh the actual screen.Lastly, we use the call sleep(2) to suspend the program for two seconds,so we can see the output before the program ends. */move(5, 15);addstr(Hello World);refresh();sleep(2);endwin();exit(EXIT_SUCCESS);
}
使用下面的命令编译并运行gcc -o t screen1.c -lncurses ./t
首先初始化一个屏幕然后移动到屏幕的 5,15位置在输出字符串 Hello World。之后休眠 2秒后程序退出。使用curses写贪吃蛇代码//sudo apt-get install libncurses5-dev
//gcc -o t tanchishe.c -lncurses ./t
#include curses.h // Linux 下的图形库
#include unistd.h // usleep()
#include stdlib.h // rand()
#include time.h // time()
#define W 40
#define H 24
int m[W * H], q[W * H], p H / 2 * W (W / 2), a, h 0, t 0, d 1, i;
int main(void) {initscr(); noecho(); keypad(stdscr, 1); nodelay(stdscr, 1); curs_set(0);srand(time(NULL));for (i 0; i W * H; i)m[i] !(i / W % (H - 1) i % W % (W - 1));m[q[t (t 1) % (W * H)] p] 1;do { a rand() % (W * H); } while (m[a]);while ((i getch()) ! 27) {if (i KEY_UP d ! W) d -W;else if (i KEY_DOWN d ! -W) d W;else if (i KEY_LEFT d ! 1) d -1;else if (i KEY_RIGHT d ! -1) d 1;if (m[p d]) break;m[q[t (t 1) % (W * H)] p] 1;if (p a) do { a rand() % (W * H); } while (m[a]);else m[q[h (h 1) % (W * H)]] 0;for (i 0; i W * H; i)mvaddstr(i / W, (i % W) * 2, m[i] ? [] : );mvaddstr(a / W, (a % W) * 2, ());refresh();usleep(100000);}while (getch() ERR);endwin();
}程序运行简单解释下for (i 0; i W * H; i)mvaddstr(i / W, (i % W) * 2, m[i] ? [] : );
构建边框和蛇身的代码边框是用 [] 构建的用这个字符从视觉上看会比较舒服。mvaddstr(a / W, (a % W) * 2, ());
随机生成的食物之前已经用时间srand(time(NULL));作为种子设置了随机数。if (m[p d]) break;
碰撞检测if (p a) do { a rand() % (W * H); } while (m[a]);else m[q[h (h 1) % (W * H)]] 0;
如果碰撞到了食物就增加蛇长度m[]里面同时保存蛇的数据和边框的数据并且蛇移动的时候需要把后面的数值设置为0。p H / 2 * W (W / 2)
蛇的初始位置自己修改的代码可以实现穿墙效果//sudo apt-get install libncurses5-dev
//gcc -o t tanchishe.c -lncurses ./t
#include curses.h // Linux 下的图形库
#include unistd.h // usleep()
#include stdlib.h // rand()
#include time.h // time()
#define W 40
#define H 24
int m[W * H], q[W * H], p H / 2 * W (W / 2), a, h 0, t 0, d 1, i,j3;
int main(void) {initscr(); noecho(); keypad(stdscr, 1); nodelay(stdscr, 1); curs_set(0);srand(time(NULL));for (i 0; i W * H; i) m[i] !(i / W % (H - 1) i % W % (W - 1));m[q[t (t 1) % (W * H)] p] 1;do { a rand() % (W * H); } while (m[a]);while ((i getch()) ! 27) {if (i KEY_UP d ! W) {d -W;j0;}else if (i KEY_DOWN d ! -W) {d W;j1;}else if (i KEY_LEFT d ! 1) {d -1;j2;}else if (i KEY_RIGHT d ! -1) {d 1;j3;}if (m[p d]) {switch(j){case 0:p p(H-2)*W; break;case 1:p p-(H-2)*W; break;case 2:p pW-2; break;case 3:p p-W2; break;default: p H / 2 * W (W / 2); break;}};m[q[t (t 1) % (W * H)] p] 1;if (p a) do { a rand() % (W * H); } while (m[a]);else m[q[h (h 1) % (W * H)]] 0;for (i 0; i W * H; i){mvaddstr(i / W, (i % W) * 2, m[i] ? [] : );}mvaddstr(a / W, (a % W) * 2, ());refresh();usleep(100000);}while (getch() ERR);endwin();
}运行如下代码原文https://www.zhihu.com/question/360814879/answer/1013986215公众号后台回复「curse」获取curse图形库资料推荐阅读专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈关注公众号后台回复「1024」获取学习资料网盘链接。欢迎点赞关注转发在看您的每一次鼓励我都将铭记于心~