#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define WIDETH 40
#define HEIGHT 20
#define MOVETO(x,y) printf("\033[%d;%dH",(y),(x)) //很重要!可以让光标停在想要的位置。打印地图以及蛇身等全靠他。
typedef enum MOV_DIR{UP = -1,DOWN = 1,LEFT = 2,RIGHT = 4,stop}DIR; //将方向赋值并定义成一个枚举,就可以将方向与蛇的前进建立起关系
DIR cur_dir = RIGHT; //初始游戏,默认蛇的方向为向右
int snake_length = 3; //蛇初始长度为3
char ch;
int score = 0; //记录成绩
typedef struct snakenote //蛇身数据的结构体
{
int x,y;
DIR dir;
}snake_data;
typedef struct link_snake //蛇节点
{
snake_data data;
struct link_snake *next;
}*psnake;
psnake link_head,tail; //link_head为链头,tail是蛇头
typedef struct snakefood //食物
{
int x;
int y;
}Food,*pfood;
pfood food;
void MAP() //打印游戏区域
{
int i;
MOVETO(10,5);
for(i=0;i<WIDETH/2;i++)
{
printf("* ");
}
for(i=1;i<HEIGHT;i++)
{
MOVETO(10,5+i);
printf("*");
}
for(i=1;i<=HEIGHT+1;i++)
{
MOVETO(10+WIDETH,4+i);
printf("*");
}
MOVETO(10,5+HEIGHT);
for(i=0;i<WIDETH/2;i++)
{
printf("* ");