#include <curses.h>
#include <stdlib.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;
}
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 hang, int lie )
{
if(food.hang == hang && food.lie == lie)
{
return 1;
}
else
{
return 0;
}
}
void initNcurse()
{
initscr(); //打开curses模式 进入你的终端
keypad(stdscr,1);//这个函数允许使用功能键,例如:F1、F2、方向键等功能键。
//几乎所有的交互式程序都需要使用功能键,因为绝大多数用户界面主要用方向键进行操作。使用keypad(stdscr,TURE)就为“标准屏幕”(stdscr)激活了功能键。
noecho(); //关闭回显 让键盘输入字符 不显示在终端上
}
void gamePic()
{
move(0,0);
int hang;
int lie;
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("\nby xuzhijie\n%d",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;
}
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 = 1;
head->lie = 1;
head->next = NULL;
tail = head;
addNode();
addNode();
addNode();
addNode();
}
void deleNode()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
int ifSnakeDie()
{
struct Snake *p;
p = head;
if(tail->hang < 0 || tail->hang == 20 || tail->lie == 0 || 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()
{
struct Snake *p;
p = head;
addNode();
if(hasfood(tail->hang,tail->lie))
{
initfood();
}
else{
deleNode();
}
if(ifSnakeDie())
{
initSnake();
}
}
void refreshjiemian()
{
while(1)
{
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()
{
initNcurse();//初始化Ncurse
initSnake();//初始化Snake
gamePic();
pthread_t t1;
pthread_t t2;
pthread_create(&t1,NULL,refreshjiemian,NULL);
pthread_create(&t2,NULL,changeDir,NULL);
while(1);
getch();
endwin();//关闭窗口stdscr
return 0;
}
贪吃蛇(C语言编写)在linux下采用Ncurse
最新推荐文章于 2024-06-28 09:31:42 发布