#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<windows.h>
int a[2000]={0};
int food;
char key='d';
typedef struct snakes
{
int x;
int y;
struct snakes*next;
}snake;
/*蛇内部数据操作*/
snake*create_head() //建头
{
snake*head=(snake*)malloc(sizeof(snake));
head->next=NULL;
head->x=head->y=0;
return head;
}
snake*create_node() //建节点
{
snake*newnode=(snake*)malloc(sizeof(snake));
newnode->next=NULL;
newnode->x=newnode->y=0;
return newnode;
}
void add_node(snake*head,int x,int y) //插入节点
{
snake*newnode=create_node();
newnode->x=x;
newnode->y=y;
newnode->next=head->next;
head->next=newnode;
}
void print(snake*head) //输出链表
{
int i=change(head); //获取链表长度
while (i)
{
printf("%d\t",head->x);
printf("%d\n",head->y);
head=head->next;
i--;
}
}
void Get_Key(snake*head,char key) //根据用户输入键,在位置添加节点
{ //记得加入大小写判别
switch (key)
{
case 'w':add_node(head,head->next->x+1,head->next->y);break;
case 's':add_node(head,head->next->x-1,head->next->y);break;
case 'a':add_node(head,head->next->x,head->next->y-1);break;
case 'd':add_node(head,head->next->x,head->next->y+1);break;
}
}
void Delet(snake*head) //尾删
{
snake*head_left=head;
head=head->next;
while (head->next!=NULL)
{
head_left=head_left->next;
head=head->next;
}
head->x=0;
head->y=0;
head_left->next=NULL;
}
void move(snake*head) //蛇的移动,检测用户是否输入
{
if(GetAsyncKeyState(VK_UP))//如果输入键是sadw
{
Get_Key(head,'s');
key='s';
}
else if(GetAsyncKeyState(VK_DOWN))
{
Get_Key(head,'w');
key='w';
}
else if(GetAsyncKeyState(VK_LEFT))
{
Get_Key(head,'a');
key='a';
}
else if(GetAsyncKeyState(VK_RIGHT))
{
Get_Key(head,'d');
key='d';
}
else
{
Get_Key(head,key);
}
}
/*地图部分*/
void Print_Map() //使得地图周变为1
{
int i,j,k=0;
for ( i = 0; i < 25; i++)
{
for ( j = 0; j <80; j++)
{
if(i==0||j==0||i==24||j==79)
{
a[k]=1;
}
if(a[k]==1)
{
printf("0");//█■♥
}
else
{
printf(" ");
}
k++;
}
printf("\n");
}
}
int change(snake*list) //链表中的xy带入数组
{
int leng;
snake*left;
left=list;
list=list->next;
while (left->next!= NULL)
{
a[list->x*80+list->y]=1;
list=list->next;
left=left->next;
leng++;
}
return leng;
}
void cler() //清空数组
{
int i;
for(i=0;i<2000;i++)
{
a[i]=0;
a[food]=1;
}
}
int death(snake*head) //死亡机制
{
snake*right;
right=head->next->next;
while (right->next!=NULL) //判断是否咬到自己
{
right=right->next;
if(head->next->x==right->x&&head->next->y==right->y)
{
return 0;break;
}
}
if(head->next->x==0||head->next->y==0||head->next->x==24||head->next->y==79)//撞墙
{
return 0;
}
else
{
return 1;
}
}
void create_food(snake*head)//生成食物
{
int x,y,i=0;
x=rand()%24+1;
y=rand()%79+1;
if(x!=0&&x!=24&&y!=0&&y!=79)
{
while(head->x!=x&&head->y!=y&&head->x!=NULL)
{
head=head->next;break;
i++;
}
food=x*79+y;
a[food]=1;
}
}
int eat_food(snake*head)
{
printf("%d\t",head->next->x*80+head->next->y);
printf("%d\n",food);
if(food==(head->next->x*80)+head->next->y)//如果头节点吃到了食物
{
food=0;
create_food(head);//创建新食物
printf("\n%d",food);
return 1;
}
}
int main()
{
snake*head=create_head();
add_node(head,22,5);
Get_Key(head,'d');
Get_Key(head,'d');
Get_Key(head,'d');
Get_Key(head,'d'); //蛇初始长度5
create_food(head);
while(death(head))
{
cler();
move(head);
if(eat_food(head)!=1)
{
Delet(head);
}
change(head); //要对数组重新赋值
Sleep(100);
system("cls");
Print_Map();
}
}
02-21
10-29
3751
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-30
2917
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)