jsp怎样做网站,网站建设的一些背景图片,企业起名大全,福州seo推广服务ncurse的引入
ncurse的概念
ncurse(new curses)是一套编程库#xff0c;它提供了一系列的函数#xff0c;以便使用者调用它们去生成基于文本的用户界面。 ncurses是一个能提供功能键定义(快捷键),屏幕绘制以及基于文本终端的图形互动功能的动态库。ncurses用得最多的地方是…ncurse的引入
ncurse的概念
ncurse(new curses)是一套编程库它提供了一系列的函数以便使用者调用它们去生成基于文本的用户界面。 ncurses是一个能提供功能键定义(快捷键),屏幕绘制以及基于文本终端的图形互动功能的动态库。ncurses用得最多的地方是linux内核编译之前的内核配置ncurses早已淡出舞台甚至体验感完爆ncurses的C图形库GTK、C图形库QT也区趋于落伍嵌入式设备上的Android 系统。这个游戏只是使用ncurses并不以学习它为目的主要还是通过这个游戏锻炼我们C语言的能力。 ncurse的格式
#include curses.h//记得包含头文件int main()
{initscr();//ncurse界面的初始化函数printw(this is a test:n);//在ncurse模式下的printfgetch();//等待用户输入如果没有这句话程序就退出来了看不到运行结果也就是看不到上面的那句话endwin();//程序退出调用改函数来恢复shell终端的显示如果没有这句话shell终端字乱码会坏掉return 0;
}ncurse的编译
ncurse的编译需要在文件名.c后加-lcurses 上述格式代码编译结果如下 nurses的上下左右键值获取及演示
如果不用nurses那么每次输入上下左右得输入回车小蛇才会有反应而运用nurses只需输入上下左右小蛇便会做出反应提高灵敏度因为该项目键盘中只需用到上下左右所以我们只需获得上下左右键值即可。
进入nurses头文件方式
vi /usr/include/curses.h
上下左右键值宏定义
#define KEY_DOWN 0402
#define KEY_UP 0403
#define KEY_LEFT 0404
#define KEY_RIGHT 0405代码演示
#include curses.hint main()
{int key;initscr();keypad(stdscr,1);//函数调用→接受功能键,是否接收(1是接收)while(1){key getch();switch(key){case 0402:printw(DOWN\n);break;case 0403:printw(UP\n);break;case 0404:printw(LEFT\n);break;case 0405:printw(RIGHT\n);break;}}endwin();return 0;
}
编译结果 地图规划 地图边界
代码演示
#include curses.hvoid gameMap()
{int hang,lie;for(hang 0;hang 20;hang){if(hang 0){for(lie 0;lie 20;lie){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);} }
}int main()
{gameMap();getch();endwin();return 0;
}代码运行结果 贪吃蛇节点身子 代码演示
#include curses.h
#include stdlib.hstruct snakebody
{int hang;int lie;struct snakebody *next;
};struct snakebody *head;
struct snakebody *tail;int snakeNode(int i,int j)
{struct snakebody *p;p head;//指向链表头while(p ! NULL){if(p-hang i p-lie j){return 1;}p p-next;}return 0;
}void gameMap(){int hang,lie;for(hang 0;hang 20;hang){if(hang 0){for(lie 0;lie 20;lie){printw(--);}printw(\n);}if(hang 0 hang 18){ for(lie 0;lie 20;lie){if(lie 0 || lie 20){ printw(|);}else if(snakeNode(hang,lie)){printw([]);//蛇结点身子的出现}else{printw( );}}printw(\n);}if(hang 19){for(lie 0;lie 20;lie){ printw(--);}printw(\n);} }
}void addNode()
{struct snakebody *new (struct snakebody *)malloc(sizeof(struct snakebody));new-hang tail-hang;new-lie tail-lie1;new-next NULL;tail-next new;//新的节点赋给尾的下一个tail new;//当尾后面没有节点时新节点就是尾
}void initSnake()//身子初始化
{head (struct snakebody *)malloc(sizeof(struct snakebody));head-hang 2;head-lie 2;head-next NULL;tail head;//一开始头就是尾addNode();//头已经定义 调用一次该函数就是多一节addNode();addNode();
}int main()
{initcurses();initSnake();gameMap();getch();endwin();return 0;
}代码运行结果 贪吃蛇的移动和“复活”
完成了地图规划就应该对蛇进行操控使得更加有体验感 接下来一步一步让代码使得体验更加逼真。
实现贪吃蛇向右移动 向右移动主要分为两个步骤第一个是头结点删除使得第二个结点成为头结点第二个是新结点从尾部加入。代码如下
#include curses.h#include stdlib.hvoid gameMap(){int hang,lie;move(0,0);//每次偏移时在地图里展现出来for(hang 0;hang 20;hang){if(hang 0){for(lie 0;lie 20;lie){printw(--);}printw(\n);}if(hang 0 hang 18){ for(lie 0;lie 20;lie){if(lie 0 || lie 20){ printw(|);}else if(snakeNode(hang,lie)){printw([]);}else{printw( );}}printw(\n);}if(hang 19){for(lie 0;lie 20;lie){ printw(--);}printw(\n);} }
}void addNode()
{struct snakebody *new (struct snakebody *)malloc(sizeof(struct snakebody));new-hang tail-hang;new-lie tail-lie1;new-next NULL;tail-next new;tail new;
}void initSnake()
{head (struct snakebody *)malloc(sizeof(struct snakebody));head-hang 2;head-lie 2;head-next NULL;tail head;addNode();addNode();addNode();
}void deletNode()
{struct snakebody *p;p head;head head-next;//删除头结点free(p);//对删除的头结点进行空间内存释放
}void movesnake()
{addNode();//在尾部增加结点deletNode();/调用删除头结点函数
}int main()
{int control;initcurses();initSnake(); gameMap();while(1){ control getch();if(control KEY_RIGHT){movesnake();gameMap();//偏移后刷新地图}}getch();endwin();return 0;
} 贪吃蛇撞墙回到起始位置
这里先实现蛇在水平方向的偏移如果蛇的尾结点碰到地图边界则让其初始化。代码如下
#include curses.h
#include stdlib.hvoid initcurses()
{initscr();keypad(stdscr,1);
}struct snakebody
{int hang;int lie;struct snakebody *next;
};struct snakebody *head NULL;
struct snakebody *tail NULL;int snakeNode(int i,int j)
{struct snakebody *p;p head;while(p ! NULL){if(p-hang i p-lie j){return 1;}p p-next;}return 0;
}void gameMap(){int hang,lie;move(0,0);for(hang 0;hang 20;hang){if(hang 0){for(lie 0;lie 20;lie){printw(--);}printw(\n);}if(hang 0 hang 18){ for(lie 0;lie 20;lie){if(lie 0 || lie 20){ printw(|);}else if(snakeNode(hang,lie)){printw([]);}else{printw( );}}printw(\n);}if(hang 19){for(lie 0;lie 20;lie){ printw(--);}printw(\n);} }
}void addNode()
{struct snakebody *new (struct snakebody *)malloc(sizeof(struct snakebody));new-hang tail-hang;new-lie tail-lie1;new-next NULL;tail-next new;tail new;
}void initSnake()
{struct snakebody *p;while(head ! NULL)//蛇撞墙后初始化时原来的身子还占内存空间需对其进行释放{p head;//指向链表头head head-next;//遍历整个链表free(p);//释放整个链表内存空间}head (struct snakebody *)malloc(sizeof(struct snakebody));head-hang 1;head-lie 1;head-next NULL;tail head;addNode();addNode();addNode();
}void deletNode()
{struct snakebody *p;p head;head head-next;free(p);
}void movesnake()
{ addNode();deletNode();if(tail-hang 0 || tail-lie 0 || tail-hang 20 || tail-lie 20){initSnake();//蛇撞到边界时初始化}
}int main()
{int control;initcurses();initSnake(); gameMap();while(1){ control getch();if(control KEY_RIGHT){movesnake();gameMap();}}getch();endwin();return 0;
}贪吃蛇的自由移动
线程的引入
若要蛇的移动和按键检测同时进行用两个while循环是无法实现的其只会执行第一个循环中的内容此时就得需要用到线程。线程是Linux中常见的函数库在此项目中的作用是能够使两个循环同时进行。线程的定义格式是pthread_t任意名字创建线程格式是pthread_create(前面的任意名字取地址NULL,循环的名字NULL)同时线程有独自的头文件和链接库如下
void* changeDir()
{}pthread_t t1;
pthread_create(t1,NULL,changeDir,NULL);
#include pthread.h
gcc a.c -lcurses -lpthread
自由移动代码演示
#include curses.h
#include stdlib.h
#include pthread.h#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2void initcurses()
{initscr();keypad(stdscr,1);noecho();//ncurses自带的函数作用是不让无关信息出现在显示屏上
}struct snakebody
{int hang;int lie;struct snakebody *next;
};struct snakebody *head NULL;
struct snakebody *tail NULL;
int key;
int dir;int snakeNode(int i,int j)
{struct snakebody *p;p head;while(p ! NULL){if(p-hang i p-lie j){return 1;}p p-next;}return 0;
}void gameMap(){int hang,lie;move(0,0);for(hang 0;hang 20;hang){if(hang 0){for(lie 0;lie 20;lie){printw(--);}printw(\n);}if(hang 0 hang 18){ for(lie 0;lie 20;lie){if(lie 0 || lie 20){ printw(|);}else if(snakeNode(hang,lie)){printw([]);}else{printw( );}}printw(\n);}if(hang 19){for(lie 0;lie 20;lie){ printw(--);}printw(\n);printw(key%d\n,key);} }
}void addNode()
{struct snakebody *new (struct snakebody *)malloc(sizeof(struct snakebody));new-next NULL;switch(dir){case UP:new-hang tail-hang-1;//蛇向上行减一即可new-lie tail-lie;break;case DOWN:new-hang tail-hang1;//蛇向下行加一即可new-lie tail-lie;break;case LEFT:new-hang tail-hang;//蛇向左列减一即可new-lie tail-lie-1;break;case RIGHT:new-hang tail-hang;//蛇向右行加一即可new-lie tail-lie1;break;}tail-next new;tail new;
}void initSnake()
{struct snakebody *p;dir RIGHT;while(head ! NULL){p head;head head-next;free(p);}head (struct snakebody *)malloc(sizeof(struct snakebody));head-hang 1;head-lie 1;head-next NULL;tail head;addNode();addNode();addNode();
}void deletNode()
{struct snakebody *p;p head;head head-next;free(p);
}void movesnake()
{ addNode();deletNode();if(tail-hang 0 || tail-lie 0 || tail-hang 20 || tail-lie 20){initSnake();}
}void* refreshJiemian()
{while(1){movesnake();gameMap();refresh();//ncurses自带刷新函数usleep(100000);//每个100000us向右移动一次}}void turn(int direction)
{if(abs(dir) ! abs(direction))//abs是一个运算符 表示的是参数的绝对值//如果输入方向的绝对值等于蛇前进方向的绝对值则蛇的路径不发生改变//如果输入方向的绝对值不等于蛇前进方向的绝对值则原有方向改变成输入方向{dir direction;}
}void* changeDir()
{while(1){key getch();switch(key){case 0402:turn(DOWN);//调用turn函数并将方向传过去判断输入方向与原有方向绝对值是否一致break;case 0403:turn(UP);break;case 0404:turn(LEFT);break;case 0405:turn(RIGHT);break;}}}int main()
{pthread_t t1;pthread_t t2;initcurses();initSnake(); gameMap();pthread_create(t1,NULL,refreshJiemian,NULL);pthread_create(t2,NULL,changeDir,NULL);while(1);getch();endwin();return 0;
}贪吃蛇的食物及食物(结尾)
贪吃蛇食物关心的是其位置和符号我们这里把符号定义为##位置可以在蛇的身体结构体定义一个食物结构体让食物的行、列在地图中被扫描从而出现食物。食物的出现是随机的这里需要调用一个函数是rand()其作用是生成一个随机数定义格式在下面代码中展示同时我们的地图是20x20只需要使其范围在19以内(0开始)即可这里运用取余的方法行列都是20让生成的随机数对20取余即%20那么出现的数的范围就在0到19(刚好为20倍数是为0)。食物的根本是判断蛇的尾部的行列值是否与食物行列值一致如果一致则在吃到那一瞬间新增一个身体结点不执行删除函数如果不一致则按原来一样自由移动。代码如下
#include stdlib.h
#include pthread.h
#include ncurses.h#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2void initcurses()
{initscr();keypad(stdscr,1);noecho();
}struct snakebody
{int hang;int lie;struct snakebody *next;
};struct snakebody *head NULL;
struct snakebody *tail NULL;
int key;
int dir;
struct snakebody food;void initfood()
{int x rand()%20;int y rand()%20;food.hang x;food.lie y;
}int snakeNode(int i,int j)
{struct snakebody *p;p head;while(p ! NULL){if(p-hang i p-lie j){return 1;}p p-next;}return 0;
}int creatfood(int i,int j)
{if(food.hang i food.lie j)//如果食物与传进来的参数行列值一样 就运行该函数 该函数主要用于后面地图出现食物符号{return 1;}return 0;
}void gameMap(){int hang,lie;move(0,0);for(hang 0;hang 20;hang){if(hang 0){for(lie 0;lie 20;lie){printw(--);}printw(\n);}if(hang 0 hang 18){ for(lie 0;lie 20;lie){if(lie 0 || lie 20){ printw(|);}else if(snakeNode(hang,lie)){printw([]);}else if(creatfood(hang,lie)){printw(##);//设置食物在地图出现}else{printw( );}}printw(\n);}if(hang 19){for(lie 0;lie 20;lie){ printw(--);}printw(\n);} }
}void addNode()
{struct snakebody *new (struct snakebody *)malloc(sizeof(struct snakebody));new-next NULL; switch(dir){case UP:new-hang tail-hang-1;new-lie tail-lie;break;case DOWN:new-hang tail-hang1;new-lie tail-lie;break;case LEFT:new-hang tail-hang;new-lie tail-lie-1;break;case RIGHT:new-hang tail-hang;new-lie tail-lie1;break;}tail-next new;tail new;
}void initSnake()
{struct snakebody *p;dir RIGHT;while(head ! NULL){p head;head head-next;free(p);}initfood();head (struct snakebody *)malloc(sizeof(struct snakebody));head-hang 1;head-lie 1;head-next NULL;tail head;addNode();addNode();addNode();
}void deletNode()
{struct snakebody *p;p head;head head-next;free(p);
}int ifSnakeDie()
{struct snakebody *p;p head;if(tail-hang 0 || tail-lie 0 || tail-hang 20 || tail-lie 20){return 1;//碰到边界则运行该函数该函数主要用于后面进行碰壁后蛇的初始化}while(p-next ! NULL){if(p-hang tail-hang p-lie tail-lie){return 1;}p p-next;}return 0;
}void movesnake()
{ addNode();if(creatfood(tail-hang,tail-lie))//将蛇的尾巴参数传到该函数 相当于蛇吃到食物 则初始化食物{initfood();}else{deletNode();}if(ifSnakeDie()){initSnake();}
}void* refreshJiemian()
{while(1){movesnake();gameMap();refresh();usleep(100000);}}void turn(int direction)
{if(abs(dir) ! abs(direction)){dir direction;}
}void* changeDir()
{while(1){key getch();switch(key){case 0402:turn(DOWN);break;case 0403:turn(UP);break;case 0404:turn(LEFT);break;case 0405:turn(RIGHT);break;}}}int main()
{pthread_t t1;pthread_t t2;initcurses();initSnake(); gameMap();pthread_create(t1,NULL,refreshJiemian,NULL);pthread_create(t2,NULL,changeDir,NULL);while(1);getch();endwin();return 0;
} 文章转载自: http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.hngmg.cn.gov.cn.hngmg.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.gzttoyp.com.gov.cn.gzttoyp.com http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.ltpmy.cn.gov.cn.ltpmy.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.whpsl.cn.gov.cn.whpsl.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.rzjfn.cn.gov.cn.rzjfn.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.bfgbz.cn.gov.cn.bfgbz.cn http://www.morning.gydth.cn.gov.cn.gydth.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn