此处用到了链表的知识
并且要仔细考虑一下 蛇转弯的情况 要以转弯的拐点为分界 拐弯前与拐弯后的坐标变化是不一样的
#include
#include
#include
struct snake{
char xingzhuan;
float score;
int x;
int y;
struct snake *next;
};
void tangchishe()
{
char a[200][200]={
"###################",
"# #",
"# * * # ",
"# #",
"# * * #",
"# #",
"# * * * #",
"# #",
"###################",
"w s a d上下左右 o离开" ,
"c 查看分数 "};
//----------------------------------定义层
int i,j;
char ch;
struct snake *head,*pnew,*ptail,*p;
//-------------------- a[x][y](初始布局 画出头和果实)
pnew=(struct snake *)malloc(sizeof(struct snake));
pnew->x=1;
pnew->y=1;
pnew->xingzhuan='O';
head=ptail=pnew;
head->score=0;
a[(head->x)][(head->y)]=head->xingzhuan;
for(i=0;i<=10;i++)
puts(a[i]);
//行动层
while(1)
{
ch=getch();
if(ch=='o')
{
system("cls");
printf("BYE!\n");
Sleep(3000);
ui();
}
if(ch=='c')
{
printf("您现在的得分是 %f",head->score);
Sleep(3000);
}
//-------------------------------------------------------下
if(ch=='s')
{
if(a[(head->x)+1][(head->y)]!='#'&&(a[(head->x)+1][head->y]!='O'))
{
if(a[(head->x)+1][(head->y)]=='*')
{
//创建新的一个蛇节点
a[(head->x)][(head->y)]=' ';
(head->x)++;
a[(head->x)][(head->y)]=' ';
pnew=(struct snake *)malloc(sizeof(struct snake));
(head->score)++;
(pnew->x)=(head->x)-1;
(pnew->y)=(head->y);
ptail->next=pnew;
ptail=pnew;
ptail->next=NULL;
p=head;
while((p->next)!=NULL)
{
a[(p->x)][(p->y)]='O';
p=p->next;
}
a[p->x][p->y]='O';
}
else
{
p=head;
while((p->next)!=NULL)
{
if((p->y)==(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->x)++;
a[(p->x)][(p->y)]='O';
}
if((p->y)<(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)++;
a[(p->x)][(p->y)]='O';
}
if((p->y)>(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)--;
a[(p->x)][(p->y)]='O';
}
p=p->next;
}
if((p->y)==(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->x)++;
a[(p->x)][(p->y)]='O';
}
if((p->y)<(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)++;
a[(p->x)][(p->y)]='O';
}
if((p->y)>(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)--;
a[(p->x)][(p->y)]='O';
}
}
}
}
//-------------------------------------------------------上
if(ch=='w')
{
if(a[(head->x)-1][(head->y)]!='#'&&a[(head->x)-1][(head->y)]!='O')
{
if(a[(head->x)-1][(head->y)]=='*')
{
//创建新的一个蛇节点
a[(head->x)][(head->y)]=' ';
(head->x)--;
a[(head->x)][(head->y)]=' ';
pnew=(struct snake *)malloc(sizeof(struct snake));
(head->score)++;
(pnew->x)=(head->x)+1;
(pnew->y)=(head->y);
ptail->next=pnew;
ptail=pnew;
ptail->next=NULL;
p=head;
while((p->next)!=NULL)
{
a[(p->x)][(p->y)]='O';
p=p->next;
}
a[p->x][p->y]='O';
}
else
{
p=head;
while((p->next)!=NULL)
{
if((p->y)==(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->x)--;
a[(p->x)][(p->y)]='O';
}
if((p->y)>(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)--;
a[(p->x)][(p->y)]='O';
}
if((p->y)<(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)++;
a[(p->x)][(p->y)]='O';
}
p=p->next;
}
if((p->y)==(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->x)--;
a[(p->x)][(p->y)]='O';
}
if((p->y)>(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)--;
a[(p->x)][(p->y)]='O';
}
if((p->y)<(head->y))
{
a[(p->x)][(p->y)]=' ';
(p->y)++;
a[(p->x)][(p->y)]='O';
}
}
}
}
//-------------------------------------------------------左
if(ch=='a')
{
if(a[(head->x)][(head->y)-1]!='#'&&a[(head->x)][(head->y)-1]!='O')
{
if(a[(head->x)][(head->y)-1]=='*')
{
//创建新的一个蛇节点
a[(head->x)][(head->y)]=' ';
(head->y)--;
a[(head->x)][(head->y)]=' ';
pnew=(struct snake *)malloc(sizeof(struct snake));
(head->score)++;
(pnew->x)=(head->x);
(pnew->y)=(head->y)+1;
ptail->next=pnew;
ptail=pnew;
ptail->next=NULL;
p=head;
while((p->next)!=NULL)
{
a[(p->x)][(p->y)]='O';
p=p->next;
}
a[p->x][p->y]='O';
}
else
{
p=head;
while((p->next)!=NULL)
{
if((p->x)==(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->y)--;
a[(p->x)][(p->y)]='O';
}
if((p->x)>(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)--;
a[(p->x)][(p->y)]='O';
}
if((p->x)<(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)++;
a[(p->x)][(p->y)]='O';
}
p=p->next;
}
if((p->x)==(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->y)--;
a[(p->x)][(p->y)]='O';
}
if((p->x)>(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)--;
a[(p->x)][(p->y)]='O';
}
if((p->x)<(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)++;
a[(p->x)][(p->y)]='O';
}
}
}
}
//-----------------------------------------------------右
if(ch=='d')
{
if(a[(head->x)][(head->y)+1]!='#'&&a[(head->x)][(head->y)+1]!='O')
{
if(a[(head->x)][(head->y)+1]=='*')
{
//创建新的一个蛇节点
a[(head->x)][(head->y)]=' ';
(head->y)++;
a[(head->x)][(head->y)]=' ';
pnew=(struct snake *)malloc(sizeof(struct snake));
(head->score)++;
(pnew->x)=(head->x);
(pnew->y)=(head->y)-1;
ptail->next=pnew;
ptail=pnew;
ptail->next=NULL;
p=head;
while((p->next)!=NULL)
{
a[(p->x)][(p->y)]='O';
p=p->next;
}
a[p->x][p->y]='O';
}
else
{
p=head;
while((p->next)!=NULL)
{
if((p->x)==(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->y)++;
a[(p->x)][(p->y)]='O';
}
if((p->x)>(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)--;
a[(p->x)][(p->y)]='O';
}
if((p->x)<(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)++;
a[(p->x)][(p->y)]='O';
}
p=p->next;
}
if((p->x)==(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->y)++;
a[(p->x)][(p->y)]='O';
}
if((p->x)>(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)--;
a[(p->x)][(p->y)]='O';
}
if((p->x)<(head->x))
{
a[(p->x)][(p->y)]=' ';
(p->x)++;
a[(p->x)][(p->y)]='O';
}
}
}
}
system("cls");
for(i=0;i<=10;i++)
puts(a[i]);
}
//-----------------------------------------------------刷新层
printf("输入o离开\n");
ch=getch();
if(ch=='o')
{
system("cls");
printf("BYE!\n");
system("cls");
ui();
}
return 0;
}