说明
上个学期暑假心血来潮,查了比较多的资料,写了一个星期写的贪吃蛇代码,bug还有,开始游戏不能按空格,你按啥键开始都行,就是不能按空格,参考了C语言网的贪吃蛇,所以很像,里面的代码有参考,但是全是重新写的,没有直接照抄,不然也不会有bug了对不对
代码里面有我写的注释,应该可以看懂
贪吃蛇通过链表实现你可以前往我的github下载此代码,VC++6.0和VS2017均可通过编译
效果图
代码
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"//包含Sleep函数,来控制运行速度
#include "conio.h"//测试时用到了getch()来获取按键,最后完成改了获取方式没用到,不过还是放着吧
#include "time.h"//设置食物时随机生成坐标用到time做种子
typedef struct SNAKE//包含坐标及尾指针,为什么用typedef因为可以直接snake定义指针,方便
{
int x;
int y;
struct SNAKE *next;
}snake;//typedef的用法http://blog.csdn.net/wangqiulin123456/article/details/8284939
void POS();//移动光标函数
void StayAwayFromWall();//判断撞墙函数
void DonnotBiteYouself();//判断咬到自己的函数
void drawmap();//画地图函数
void initsnake();//初始化蛇函数
void createfood();//创建食物函数
void gamecircle();//游戏移动循环函数
void move();//移动函数
void pause();//暂停函数
void welcome();//欢迎界面
void game();//游戏界面
snake *head,*end;//定义蛇头、蛇尾
snake *p;//移动时用到的遍历蛇的指针
int i,speed=300;//循环变量i、速度
int levelup,score=0;//达到一定分数变快、分数
int foodx,foody;//食物的(x,y)坐标
char key;//定义按键获取
int main()
{
welcome();
game();
getch();
return 0;
}
void POS(int x,int y)//设置光标位置,参考网址https://baike.so.com/doc/1283395-1357065.html
{
COORD pos;
HANDLE output;
pos.X=x;
pos.Y=y;
output=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output,pos);
}
void StayAwayFromWall()
{
if (head->x==0||head->x==58||head->y==0||head->y==29)
{
POS(28,14);
printf("游戏结束你撞墙了\n");
POS(28,15);
system("pause");
exit(0);
}
}
void DonnotBiteYouself()
{
while(p->next!=NULL)
{
p=p->next;
if (head->x==p->x&&head->y==p->y)
{
POS(28,14);
printf("游戏结束你咬到自己了\n");
POS(28,15);
system("pause");
exit(0);
}
}
}
void drawmap()//画棋盘
{
for(i=0;i<60;i+=2)//打印上下边框
{
POS(i,0);
printf("■");
POS(i,28);
printf("■");
}
for(i=1;i<28;i++)//打印左右边框
{
POS(0,i);
printf("■");
POS(58,i);
printf("■");
}
}
void initsnake()//初始化蛇身
{
end=(snake*)malloc(sizeof(snake));
end->x=28;
end->y=14;
end->next=NULL;
for (i=1;i<=3;i++)
{
head=(snake*)malloc(sizeof(snake));
head->next=end;
head->x=28+2*i;
head->y=15;
end=head;
}
while (end->next!=NULL)
{
POS(end->x,end->y);
printf("■");
end=end->next;
}
}
void createfood()
{
srand(time(0));//配合rand使用
flag:
while(1)//由于food的x坐标必须为偶数,所以设置循环判断是否为偶数
{
foody=rand()%27+1;//先生成y坐标,不能先生成x,x有判断,先生成x会退出while不生成y
foodx=rand()%55+2;//rand产生范围数公式rand()%(m-n+1)+n……其中n<=要生成数的范围<=m
if (foodx%2==0)
{
break;
}
}
p=head;
while(1)
{
if(p->x==foodx&&p->y==foody)//若生成坐标和蛇重叠了,回到生成坐标循环
{
goto flag;
}
if(p->next==NULL)
{
break;
}
p=p->next;
}
POS(foodx,foody);
printf("■");
}
void gamecircle()//游戏循环,包含移动,暂停功能
{
key='d';
while (1)
{
if (GetAsyncKeyState(VK_UP)&&key!='s')
{
key='w';
}
else if (GetAsyncKeyState(VK_DOWN)&&key!='w')
{
key='s';
}
else if (GetAsyncKeyState(VK_RIGHT)&&key!='a')
{
key='d';
}
else if (GetAsyncKeyState(VK_LEFT)&&key!='d')
{
key='a';
}
else if (GetA