C语言贪吃蛇
运行效果
代码注释
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
void Pos(int x, int y);
void muban();
void initSnake();
void creatFood();
char reDirection();
int snakeMove();
int crossWall();
int eatSelf();
typedef struct Snake
{
int x;
int y;
struct Snake *next;
}snake;
snake *head;
snake *p;
snake *food1;
char status='L';
int score=0;
int add=10;
int leap=0;
int endleap=0;
int sleepTime=500;
void initSnake()
{
int i;
snake *tail;
tail=(snake*)malloc(sizeof(snake));
tail->x=30;
tail->y=10;
tail->next=NULL;
for(i=1;i<=4;i++)
{
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=30-2*i;
head->y=10;
tail=head;
}
while(tail!=NULL)
{
Pos(tail->x,tail->y);
printf("▇");
tail=tail->next;
}
}
char reDirection()
{
if(GetAsyncKeyState(VK_F7))
{
if(sleepTime>300)
{
sleepTime-=50;
add++;
}
}
if(GetAsyncKeyState(VK_F8))
{
if(sleepTime<800)
{
sleepTime+=50;
add--;
}
}
if(GetAsyncKeyState(VK_UP)&&status!='D')
status='U';
if(GetAsyncKeyState(VK_DOWN)&&status!='U')
status='D';
if(GetAsyncKeyState(VK_LEFT)&&status!='R')
status='L';
if(GetAsyncKeyState(VK_RIGHT)&&status!='L')
status='R';
return status;
}
void Pos(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
void creatFood()
{
snake *food;
food=(snake*)malloc(sizeof(snake));
srand((unsigned int)time(NULL));
while(food->x%2!=0)
{
food->x=rand()%56+2;
}
food->y=rand()%23+1;
p=head;
while(p!=NULL)
{
if(food->x==p->x&&food->y==p->y)
{
free(food);
creatFood();
}
p=p->next;
}
Pos(food->x,food->y);
food1=food;
printf("▇");
Pos(70,20);
printf("您的分数是:%d",score);
}
void muban()
{
int i;
for(i=0;i<=60;i+=2)
{
Pos(i,0);
printf("▇");
Pos(i,26);
printf("▇");
}
for(i=0;i<=25;i+=1)
{
Pos(0,i);
printf("▇");
Pos(60,i);
printf("▇");
}
}
int snakeMove()
{
snake *nexthead;
nexthead=(snake*)malloc(sizeof(snake));
if(status=='R')
{
nexthead->x=head->x+2;
nexthead->y=head->y;
if(nexthead->x==food1->x&&nexthead->y==food1->y)
{
nexthead->next=head;
head=nexthead;
p=head;
while(p!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
score=score+add;
creatFood();
}
else
{
nexthead->next=head;
head=nexthead;
p=head;
while(p->next->next!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
Pos(p->next->x,p->next->y);
printf(" ");
Pos(70,20);
printf("您的分数是:%d",score);
free(p->next);
p->next=NULL;
}
}
if(status=='L')
{
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food1->x&&nexthead->y==food1->y)
{
nexthead->next=head;
head=nexthead;
p=head;
while(p!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
score=score+add;
creatFood();
}
else
{
nexthead->next=head;
head=nexthead;
p=head;
while(p->next->next!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
Pos(p->next->x,p->next->y);
printf(" ");
Pos(70,20);
printf("您的分数是:%d",score);
free(p->next);
p->next=NULL;
}
}
if(status=='U')
{
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food1->x&&nexthead->y==food1->y)
{
nexthead->next=head;
head=nexthead;
p=head;
while(p!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
score=score+add;
creatFood();
}
else
{
nexthead->next=head;
head=nexthead;
p=head;
while(p->next->next!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
Pos(p->next->x,p->next->y);
printf(" ");
Pos(70,20);
printf("您的分数是:%d",score);
free(p->next);
p->next=NULL;
}
}
if(status=='D')
{
nexthead->x=head->x;
nexthead->y=head->y+1;
if(nexthead->x==food1->x&&nexthead->y==food1->y)
{
nexthead->next=head;
head=nexthead;
p=head;
while(p!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
score=score+add;
creatFood();
}
else
{
nexthead->next=head;
head=nexthead;
p=head;
while(p->next->next!=NULL)
{
Pos(p->x,p->y);
printf("▇");
p=p->next;
}
Pos(p->next->x,p->next->y);
printf(" ");
Pos(70,20);
printf("您的分数是:%d",score);
free(p->next);
p->next=NULL;
}
}
Sleep(sleepTime);
status=reDirection();
if(crossWall()==1||eatSelf()==1)
endleap=1;
return endleap;
}
int crossWall()
{
if(head->x==0||head->y==0||head->x==60||head->y==25)
leap=1;
return leap;
}
int eatSelf()
{
snake *q;
q=head->next;
while(q!=NULL)
{
if(q->x==head->x&&head->y==q->y)
leap=1;
q=q->next;
}
return leap;
}
int main()
{
muban();
initSnake();
creatFood();
while(1)
{
if(snakeMove()==1)
{
Pos(70,23);
printf("蛇死了 游戏结束");
system("pause");
Pos(70,24);
break;
}
}
printf("是否继续游戏,y or n:");
if(getch()=='y')
{
status='L';
score=0;
add=10;
leap=0;
endleap=0;
sleepTime=500;
system("cls");
main();
}
if(getch()=='n')
{
Pos(70,25);
exit(0);
}
return 0;
}