首先在Linux 安装ncurses库,输入命令
$ sudo apt-get install libncurses5-dev
示例代码
#include <curses.h> //curses头文件
#include <stdlib.h>
#include <pthread.h>//线程头文件
#include <unistd.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake //创建蛇的结构体
{
int hang;
int lie;
struct Snake *next;
};
struct Snake *head = NULL;
struct Snake *tail = NULL;
int key;
int dir;
struct Snake food;
void initfood() //创建食物位置
{
int x = rand()%20;
int y = rand()%20;
food.hang = x;
food.lie = y;
}
void initNcurses() //初始化ncurese
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasSnakeNode(int i,int j) //判断是否打印蛇身[]
{
struct Snake *p;
p = head;
while(p != NULL){
if(p->hang == i && p->lie == j){
return 1;
}
p = p->next;
}
return 0;
}
int hasFood(int i,int j) //吃食物
{
if(food.hang == i && food.lie == j){
return 1;
}
return 0;
}
void gamePic() //创建地图
{
int hang;
int 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<=19){
for(lie=0;lie<=20;lie++){
if(lie == 0 || lie==20){
printw("|");
}else if(hasSnakeNode(hang,lie)){
printw("[]");
}else if(hasFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
}
printw("by cuichao ,key (%d,%d) %d\n",food.hang,food.lie,key);
}
void addNode() //根据按键方向调整蛇的节点 添加节点
{
struct Snake *new = (struct Snake*)malloc(sizeof(struct Snake));
new->next = NULL;
switch(dir){
case UP:
new->hang = tail->hang-1;
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1;
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->lie+1;
break;
}
// new->hang = tail->hang;
// new->lie = tail->lie + 1;
new->next = NULL;
tail->next = new;
tail = new;
}
void initSnake() //初始化蛇
{
struct Snake *p;
dir = RIGHT;
while(head != NULL){
p = head;
head = head->next;
free(p);
}
initfood();
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 2;
head->lie = 2;
head->next = NULL;
tail = head;
addNode();
addNode();
addNode();
}
void deletNode() //删除蛇的节点
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
int ifSnakeDie() //判断蛇是否相撞 从新游戏
{
struct Snake *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(hasFood(tail->hang,tail->lie)){
initfood();
}else{
deletNode();
}
if(ifSnakeDie()){
initSnake();
}
}
void* refreshJieMian() //刷新界面
{
while(1){
// con = getch();
// if(con == KEY_RIGHT){
moveSnake();
gamePic();
refresh();
usleep(100000);
// }
}
}
void turn(int direction) //判断蛇的方向是否与按键方向是否一致
{
if(abs(dir) != abs(direction)){
dir = direction;
}
}
void* changeDir() //方向健
{
while(1){
key = getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
int con;
pthread_t t1;//线程1
pthread_t t2;//线程2
initNcurses();//初始化nucrses界面
initSnake();//创建蛇身
gamePic();//构建地图
pthread_create(&t1,NULL,refreshJieMian,NULL); //线程1 刷新界面
pthread_create(&t2,NULL,changeDir,NULL); //线程2 输入方向键
while(1);
getch();
endwin();
return 0;
}
编译命令:
gcc 文件.c -lcurses -lpthread
效果展示