# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <conio.h>
# include <windows.h>
# include <time.h>
#define MAX 1000
void color(int b);//颜色函数
void gotoxy(int x, int y);//光标函数
void drawsnake();//蛇的绘画
void movesnake();//蛇的移动
void createmap();//创建地图并初始化
void begingame();//开始游戏
void createfood();//生成食物的函数
void diagnosesnake();//判断是否吃到食物
void changesnake();//蛇的移动,按键的
void bitself();//不能咬到自己
void cantcrosswall();//不能穿墙
void endgame();//函数结束
struct COOD
{
int x;
int y;
};
enum CH
{
up=72,
down=80,
left=75,
right=77
};
struct SNAKE
{
int n;
struct COOD a[MAX];
enum CH ch;
}snake;
void color(int b)//颜色函数
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole,b) ;
}
void gotoxy(int x, int y)//光标函数
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void drawsnake()//蛇的绘画
{
int i;
for(i=0;i<snake.n;i++)//生成蛇身
{
gotoxy(snake.a[i].x,snake.a[i].y);
color(12);
printf("o");
}
gotoxy(70,16);
printf("你的分数为%d",snake.n-1);
}
void movesnake()//蛇的移动
{
int i;
gotoxy(snake.a[0].x,snake.a[0].y);
printf(" ");
for(i=0;i<snake.n-1;i++){
snake.a[i].x=snake.a[i+1].x;
snake.a[i].y=snake.a[i+1].y;
}
changesnake();
switch(snake.ch)
{
case up:
snake.a[i].y=snake.a[i-1].y-1;
snake.a[i].x=snake.a[i-1].x;
break;
case down:
snake.a[i].y=snake.a[i-1].y+1;
snake.a[i].x=snake.a[i-1].x;
break;
case left:
snake.a[i].x=snake.a[i-1].x-1;
snake.a[i].y=snake.a[i-1].y;
break;
case right:
snake.a[i].x=snake.a[i-1].x+1;
snake.a[i].y=snake.a[i-1].y;
break;
default :
break;
}
diagnosesnake();//判断是否吃到食物
cantcrosswall();
bitself();
}
void createmap()//创建地图
{
int i;
for(i=0;i<58;i+=2)//打印上下边框
{
color(12);
gotoxy(i,0);
printf("*");
color(12);
gotoxy(i,26);
printf("*");
}
for(i=1;i<26;i++)//打印左右边框
{
color(12);
gotoxy(0,i);
printf("*");
color(12);
gotoxy(56,i);
printf("*");
}
snake.n=2;
snake.a [0].x=11;
snake.a [0].y=11;
snake.a [1].x=12;
snake.a [1].y=11;
snake.ch=down;
snake.a [2].x=16;
snake.a [2].y=16;//初始化
}
void begingame()
{
int a=200;
createmap();
gotoxy(snake.a[snake.n].x,snake.a[snake.n].y);
color(10);
printf("o");
while(1){
if(snake.n>5)
{
a=100;
gotoxy(70,15);
printf("加速啦");
}
Sleep(a);
drawsnake() ;
movesnake();
};
}
void createfood()//生成食物的函数
{
int i;
srand((int)time(0));
snake.a[snake.n].x =rand()%40+1;
snake.a[snake.n].y =rand()%25+1;
for(i=0;i<snake.n;i++)
{
if((snake.a[snake.n].x==snake.a[i].x )&&(snake.a[snake.n].y ==snake.a[i].y))
{
i=0;
snake.a[snake.n].x =rand()%40+1;
snake.a[snake.n].y =rand()%25+1;
}
}
gotoxy(snake.a[snake.n].x,snake.a[snake.n].y);
color(10);
printf("o");
}
void changesnake()//蛇的移动,按键的
{
if(GetAsyncKeyState(VK_UP) && snake.ch!=down)
{
snake.ch=up;
}
else if(GetAsyncKeyState(VK_DOWN) && snake.ch!=up)
{
snake.ch=down;
}
else if(GetAsyncKeyState(VK_LEFT)&& snake.ch!=right)
{
snake.ch=left;
}
else if(GetAsyncKeyState(VK_RIGHT)&& snake.ch!=left)
{
snake.ch=right;
}
}
void diagnosesnake()//判断是否吃到食物
{
if((snake.a[snake.n].x==snake.a[snake.n-1].x)&&(snake.a[snake.n].y==snake.a[snake.n-1].y))
{
snake.n+=1;
createfood();
}
}
void cantcrosswall()
{
if((snake.a[snake.n-1].x==0||snake.a[snake.n-1].x==56)||(snake.a[snake.n-1].y==0||snake.a[snake.n-1].y==26))
endgame();
}
void endgame()
{
system("cls");
color(5);
gotoxy(25,25);
printf("Game Over!");
system("pause");
}
void bitself()
{
int i;
for(i=0;i<snake.n-2;i++)
{
if(snake.a[snake.n-1].x==snake.a[i].x&&snake.a[snake.n-1].y==snake.a[i].y)
{
endgame();
}
}
}
int main(void)//主函数
{
gotoxy(25,13);
printf("Welcome to 贪吃蛇");
gotoxy(25,15);
printf("这是一个好玩的游戏");
Sleep(2000);
system("cls");
begingame();
return 0;
}