当前位置: 首页 > 科技观察

Linux下的图形库curses写贪吃蛇,酷

时间:2023-03-13 17:19:55 科技观察

Linux下的图形库curses写的是Snake。最近看到高手在linux下写的snake代码。它使用curses图形库。可能很多人没有用过,我分享给大家。ubuntu下安装curses图形库命令sudoapt-getinstalllibncurses5-dev。双buff是一个很好的机制。之前写贪吃蛇的时候,如果不使用双buff,画面会剧烈跳动。使用双buff后,体验非常好。我们使用curses图形库时也是如此。如果不调用refresh()函数,显示的屏幕将不会更新。例如下面的代码#include#include#includeintmain(){initscr();/*Wemovethecursortothepoint(5,15)onthelogicalscreen,print"HelloWorld"and刷新实际屏幕。最后,我们使用调用sleep(2)来暂停程序两秒钟,因此我们可以在程序结束前看到输出。*/move(5,15);addstr("HelloWorld");refresh();sleep(2);endwin();exit(EXIT_SUCCESS);}编译命令如下运行gcc-otscreen1.c-lncurses&&./t首先初始化一个屏幕,然后移动到屏幕的5,15位置,并输出字符串HelloWorld。休眠2秒后,程序退出。使用curses写贪吃蛇代码//sudoapt-getinstalllibncurses5-dev//gcc-ottanchishe.c-lncurses&&./t#include//Linux下的图形库#include//usleep()#include//rand()#include//time()#defineW40#defineH24intm[W*H],q[W*H],p=H/2*W+(W/2),a,h=0,t=0,d=1,i;intmain(void){initscr();noecho();keypad(stdscr,1);nodelay(stdscr,1);curs_set(0);srand(time(NULL));for(i=0;i//Linux下的图形库#include//usleep()#include//rand()#include//time()#defineW40#defineH24intm[W*H],q[W*H],p=H/2*W+(W/2),a,h=0,t=0,d=1,i,j=3;intmain(void){initscr();noecho();keypad(stdscr,1);nodelay(stdscr,1);curs_set(0);srand(time(NULL));for(i=0;i