#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int food_x,food_y;//食物的坐标
int speed=0,changeSpeed=0;//当前速度
int esc=0;//0为不退出,1为退出
struct sneak
{
int x,y;
struct sneak *next;
};//蛇的结构体
void Setpos(int i,int j)
{
COORD pos={i-1,j-1};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}//定位在什么地方输出
void beginsneak(struct sneak *head)
{
struct sneak *temp1,*temp2;
temp1=(struct sneak*)malloc(sizeof(struct sneak));
temp2=(struct sneak*)malloc(sizeof(struct sneak));
temp1->x=6;
temp1->y=5;
temp2->x=6;
temp2->y=4;
head->next=temp1;
temp1->next=temp2;
temp2->next=NULL;
}//初始化蛇的身体
void map()
{
int i;
for(i=0;i<50;i++)
{
Setpos(i,1);
printf("-");
Setpos(i,25);
printf("-");
}
for(i=1;i<25;i++)
{
Setpos(1,i);
printf("|");
Setpos(50,i);
printf("|");
}
}//画地图
void food(struct sneak *head)
{
struct sneak *p;
p=head;
int foodexist=0;//食物标记
srand((int)time(0));//初始化随机数
while(foodexist==0)
{
food_x=rand()%48+2;
food_y=rand()%23+2;
while(p!=NULL)//判断食物生成是否在蛇的身体里
{
if(p->x==food_x&&p->y==food_y)
{
foodexist=1;//食物在蛇的身体里生成
break;
}
p=p->next;
}
if(foodexist==0)//如果没有,则生成食物
{
Setpos(food_x,food_y);
printf("$");
break;
}
}
}
void paint(struct sneak *head)//画出蛇的身体
{
struct sneak *p;
int ifhead=0; //判断是否是蛇头
p=head;
while(p!=NULL)
{
if(ifhead==0)//如果是,画出蛇头
{
Setpos(p->x,p->y);
printf("#");
ifhead=1;
}
else//其他部分
{
Setpos(p->x,p->y);
printf("*");
}
p=p->next;
}
}
struct sneak *moveresult(struct sneak *head)//蛇的移动
{
struct sneak *new1;
new1=(struct sneak*)malloc(sizeof(struct sneak));
static int direction=4;//初始化蛇的方向
char x,y;
if(_kbhit())
{
x=_getch();
if((x=='w'||x=='W')&&direction!=2)
direction=1;
if((x=='s'||x=='S')&&direction!=1)
direction=2;
if((x=='a'||x=='A')&&direction!=4)
direction=3;
if((x=='d'||x=='D')&&direction!=3)
direction=4;
if(x=='j'||x=='J')//j暂停
{
while(1)
{
if(_kbhit())
{
y=
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int food_x,food_y;//食物的坐标
int speed=0,changeSpeed=0;//当前速度
int esc=0;//0为不退出,1为退出
struct sneak
{
int x,y;
struct sneak *next;
};//蛇的结构体
void Setpos(int i,int j)
{
COORD pos={i-1,j-1};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}//定位在什么地方输出
void beginsneak(struct sneak *head)
{
struct sneak *temp1,*temp2;
temp1=(struct sneak*)malloc(sizeof(struct sneak));
temp2=(struct sneak*)malloc(sizeof(struct sneak));
temp1->x=6;
temp1->y=5;
temp2->x=6;
temp2->y=4;
head->next=temp1;
temp1->next=temp2;
temp2->next=NULL;
}//初始化蛇的身体
void map()
{
int i;
for(i=0;i<50;i++)
{
Setpos(i,1);
printf("-");
Setpos(i,25);
printf("-");
}
for(i=1;i<25;i++)
{
Setpos(1,i);
printf("|");
Setpos(50,i);
printf("|");
}
}//画地图
void food(struct sneak *head)
{
struct sneak *p;
p=head;
int foodexist=0;//食物标记
srand((int)time(0));//初始化随机数
while(foodexist==0)
{
food_x=rand()%48+2;
food_y=rand()%23+2;
while(p!=NULL)//判断食物生成是否在蛇的身体里
{
if(p->x==food_x&&p->y==food_y)
{
foodexist=1;//食物在蛇的身体里生成
break;
}
p=p->next;
}
if(foodexist==0)//如果没有,则生成食物
{
Setpos(food_x,food_y);
printf("$");
break;
}
}
}
void paint(struct sneak *head)//画出蛇的身体
{
struct sneak *p;
int ifhead=0; //判断是否是蛇头
p=head;
while(p!=NULL)
{
if(ifhead==0)//如果是,画出蛇头
{
Setpos(p->x,p->y);
printf("#");
ifhead=1;
}
else//其他部分
{
Setpos(p->x,p->y);
printf("*");
}
p=p->next;
}
}
struct sneak *moveresult(struct sneak *head)//蛇的移动
{
struct sneak *new1;
new1=(struct sneak*)malloc(sizeof(struct sneak));
static int direction=4;//初始化蛇的方向
char x,y;
if(_kbhit())
{
x=_getch();
if((x=='w'||x=='W')&&direction!=2)
direction=1;
if((x=='s'||x=='S')&&direction!=1)
direction=2;
if((x=='a'||x=='A')&&direction!=4)
direction=3;
if((x=='d'||x=='D')&&direction!=3)
direction=4;
if(x=='j'||x=='J')//j暂停
{
while(1)
{
if(_kbhit())
{
y=