对于传统的贪吃蛇做了一次改进,每当按下向下的键的时候,在蛇尾自动的脱落一块障碍物,向下按的次数越多,障碍物越多。
游戏截图:
代码:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define U 1
#define D 2
#define L 3
#define R 4
void gotoxy(int x,int y);
void createmap();
void createsnake();
void initgame();
void createfood();
void circlegame();
void gamemove();
void acrosswall();
int biteself();
void gameover();
void acrosspoint();
typedef struct SNAKE
{
int x;
int y;
struct SNAKE *next;
}snake;
int score=0;
int status; //显示按键的状态。
snake *head,*food,*p; //定义头结点。
snake ary[1000];
int num=0,flag=0;
void gameover()
{
snake *q;
p=head;
while(p!=NULL)
{
q=p->next ;
free(p);
p=q;
}
system("cls");
gotoxy(50,12);
printf("游戏结束");
gotoxy(50,14);
printf("您的分数是%d\n",score);
gotoxy(50,20);
system("pause");
exit(0);
}
void gotoxy(int x,int y)
{
HANDLE A;
COORD B;
B.X =x;
B.Y =y;
A=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(A,B);
}
void createmap()
{
int i;
for(i=0;i<58;i=i+2)
{
gotoxy(i,0);
printf("■");
gotoxy(i,26);
printf("■");
}
for(i=1;i<26;i++)
{
gotoxy(0,i);
printf("■");
gotoxy(56,i);
printf("■");
}
gotoxy(24,5);
}
void createsnake() //进行初始化蛇身链表的,使用头插法。
{
int i;
snake *tail;
tail=(snake *)malloc(sizeof(snake));
tail->x =24;
tail->y =5;
tail->next =NULL;
for(i=1;i<=4;i++)
{
head=(snake *)malloc(sizeof(snake));
head->next =tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL)
{
gotoxy(tail->x ,tail->y );
printf("■");
tail=tail->next;
}
gotoxy(0,28);
}
void createfood()
{
snake *food_1;
srand((unsigned)time(NULL)); //设置随机种子。
food_1=(snake *)malloc(sizeof(snake));
while(food_1->x %2!=0)
{
food_1->x =rand()%52+2;
}
food_1->y =rand()%24+1;
p=head;
while(p !=NULL)
{
if(food_1->x ==p->x &&food_1->y ==p->y )
{
free(food_1);
createfood();
}
p=p->next ;
}
gotoxy(food_1->x ,food_1->y );
food=food_1;
printf("★");
}
void initgame()
{
createmap();
createsnake();
createfood();
}
void pause()
{
while(1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
void acrosswall() //检验撞墙函数。
{
if(head->x ==56||head->x ==0||head->y ==0||head->y ==26)
gameover();
}
int biteself()
{
snake *self;
self=head->next ;
while(self!=NULL)
{
if (head->x ==self->x &&head->y ==self->y )
return 1;
self=self->next ;
}
return 0;
}
void acrosspoint()
{
int i=0;
while(i<=num)
{
if(head->x ==ary[i].x &&head->y ==ary[i].y )
gameover();
i++;
}
}
void gamemove()
{
snake *nexthead;
acrosswall();
nexthead=(snake *)malloc(sizeof(snake));
if (status==U)
{
nexthead->x =head->x ;
nexthead->y =head->y-1;
if(nexthead->x ==food->x &&nexthead->y ==food->y ) //有食物的情况下
{
nexthead->next =head;
head=nexthead;
p=head;
while(p!=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
score+=10;
createfood();
}
else //没有食物。
{
nexthead->next =head;
head=nexthead;
p=head;
while (p->next ->next !=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next;
}
gotoxy(p->next ->x ,p->next ->y );
printf(" ");
free(p->next );
p->next =NULL;
}
}
if (status==D)
{
nexthead->x =head->x ;
nexthead->y =head->y +1;
if(nexthead->x ==food->x &&nexthead->y ==food->y )
{
nexthead->next =head;
head=nexthead;
p=head;
while(p!=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
score+=10;
createfood();
}
else
{
nexthead->next =head;
head=nexthead;
p=head;
while(p->next->next !=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
gotoxy(p->x ,p->y );
if(flag==0)
{
ary[num].x =p->next ->x ;
ary[num].y =p->next ->y ;
num++;
flag=1;
}
printf(" ");
free(p->next );
p->next =NULL;
}
}
if(status==R)
{
nexthead->x =head->x +2;
nexthead->y =head->y ;
if(nexthead->x ==food->x &&nexthead->y==food->y ) //首先是有食物的。
{
nexthead->next =head;
head=nexthead;
p=head;
while(p!=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
score+=10;
createfood();
}
else
{
nexthead->next =head;
head=nexthead;
p=head;
while (p->next ->next !=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
gotoxy(p->next ->x ,p->next ->y );
printf(" ");
free(p->next );
p->next =NULL;
}
}
if(status==L)
{
nexthead->x =head->x -2;
nexthead->y =head->y ;
if (nexthead->x ==food->x &&nexthead->y ==food->y )
{
nexthead->next =head;
head=nexthead;
p=head;
while(p!=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
score+=10;
createfood();
}
else //没食物
{
nexthead->next =head;
head=nexthead;
p=head;
while(p->next ->next !=NULL)
{
gotoxy(p->x ,p->y );
printf("■");
p=p->next ;
}
gotoxy(p->next ->x ,p->next ->y );
printf(" ");
free(p->next );
p->next =NULL;
}
}
if(biteself()==1)
{
gameover();
}
acrosspoint();
}
void circlegame()
{
gotoxy(70,5);
printf("用方向键控制,space键暂停,esc键退出\n\n");
status=R;
while(1)
{
gotoxy(70,8);
printf("得分:%d\n",score);
gotoxy(70,9);
printf("每个食物分数:%d\n",10);
if(GetAsyncKeyState(VK_UP)&&status!=D)
{
flag=0;
status=U;
}
else if(GetAsyncKeyState(VK_DOWN)&&status!=U)
{
status=D;
}
else if(GetAsyncKeyState(VK_RIGHT)&&status!=L)
{
flag=0;
status=R;
}
else if(GetAsyncKeyState(VK_LEFT)&&status!=R)
{
flag=0;
status=L;
}
else if(GetAsyncKeyState(VK_SPACE))
{
flag=0;
pause(); //暂停函数。
}
else if(GetAsyncKeyState(VK_ESCAPE))
{
break;
}
Sleep(200);
gamemove();
}
}
int main()
{
initgame(); //首先要进行初始化游戏。(地图,蛇身链表,食物的随机出现)
circlegame();
return 0;
}