记录一下我搞了好久的贪吃蛇
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#include<windows.h>
int v;
int snakes[200][3]={0};
enum{toeast = 2, towest = -2, tonorth = -1, tosouth = 1};
char map[20][48]={"静静静静静静静静静静静静静静静静静静静静静静静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静 静\n",
"静静静静静静静静静静静静静静静静静静静静静静静\n"
};
int hang,lie;
int snakedir = -2;
int d1;
int d=0;
int snakelen = 2;
int n=0;
void fmian()
{
printf("欢迎来到贪吃蛇\n");
printf("注释:W,S,A,D分别表示上下左右哦\n\n\n");
printf("按空格键进入,去选择难度吧!!");
}
void kge()
{
while(1)
{
if(' ' == getch()) break;
}
system("cls");
}
void xzndu()
{
printf("有五个难度供给选择:1,2,3,4,5依次递增!!\n");
printf("选择完毕后,可以按回车键开始游戏了\n\n");
printf("选择吧:\n");
char ch;
scanf("%c",&ch);
switch (ch)
{
case '1':
v=500;
break;
case '2':
v=400;
break;
case '3':
v=300;
break;
case '4':
v=200;
break;
case '5':
v=100;
break;
}
}
void xiesnake()
{
for(int i=0;snakes[i][0]!=0;i++)
{
strncpy(&map[snakes[i][0]][snakes[i][1]],"爱",2);
}
}
void snake ()
{
int x,y;
srand((unsigned)time(NULL));
x=rand()%19+1;
y=rand()%18+1;
snakes[0][0] = y;
snakes[0][1] = x*2;
snakes[0][2] = towest;
snakes[1][0] = y;
snakes[1][1] = x*2 + 2;
snakes[1][2] = towest;
snakes[2][0] = y;
snakes[2][1] = x*2 + 4;
snakes[2][2] = towest;
xiesnake();
}
void huisnake()
{
for(int i=0;snakes[i][0]!=0;i++)
{
strncpy(&map[snakes[i][0]][snakes[i][1]]," ",2);
}
}
void food()
{
srand((unsigned)time(NULL));
while(1)
{
int j=1;
hang=rand()%18+1;
lie=rand()%21+1;
for(int i=0;snakes[i][0]!=0;i++)
{
if(hang==snakes[i][0]&&lie*2==snakes[i][1])
{
j=0;
}
}
if(j==1)break;
}
strncpy(&map[hang][lie*2],"凌",2);
}
int change()
{
if(GetAsyncKeyState('W'))
{
if(snakes[0][2]!=tonorth)
{
snakedir=tonorth;
return 0;
}
}
else if(GetAsyncKeyState('S'))
{
if(snakes[0][2]!=tosouth)
{
snakedir=tosouth;
return 0;
}
}
else if(GetAsyncKeyState('A'))
{
if(snakes[0][2]!=towest)
{snakedir = towest;
return 0;}
}
else if(GetAsyncKeyState('D'))
{
if(toeast != snakes[0][2])
{
snakedir = toeast;
return 0;
}
}
return 1;
}
void snakeup()
{
if(hang == snakes[0][0] && lie*2 == snakes[0][1])
{
if(snakes[snakelen][2]==toeast)
{
snakes[snakelen+1][0] = snakes[snakelen][0];
snakes[snakelen+1][1] = snakes[snakelen][1] - 2;
snakes[snakelen+1][2] = snakes[snakelen][2];
}
else if(snakes[snakelen][2]==towest)
{
snakes[snakelen+1][0] = snakes[snakelen][0];
snakes[snakelen+1][1] = snakes[snakelen][1] +2;
snakes[snakelen+1][2] = snakes[snakelen][2];
}
else if(snakes[snakelen][2]==tonorth)
{
snakes[snakelen+1][0] = snakes[snakelen][0]+1;
snakes[snakelen+1][1] = snakes[snakelen][1];
snakes[snakelen+1][2] = snakes[snakelen][2];
}
else if(snakes[snakelen][2]==tosouth)
{
snakes[snakelen+1][0] = snakes[snakelen][0]-1;
snakes[snakelen+1][1] = snakes[snakelen][1];
snakes[snakelen+1][2] = snakes[snakelen][2];
}
food();
n++;
snakelen++;
}
}
void snakemove()
{
huisnake();
for(int i=199;i>=1;i--)
{
if(snakes[i][0]!=0)
{
snakes[i][0]=snakes[i-1][0];
snakes[i][1]=snakes[i-1][1];
snakes[i][2]=snakes[i-1][2];
}
}
snakes[0][2]=snakedir;
if(snakes[0][2]==tonorth||snakes[0][2]==tosouth)
{
snakes[0][0]+=snakes[0][2];
}
else
{
snakes[0][1]+=snakes[0][2];
}
xiesnake();
}
void xiemap()
{
for(int i=0;i<20;i++)
{
printf("%s",map[i]);
}
printf("当前分数:%d",n);
}
int sisnake()
{
if(snakes[0][2]==tonorth||snakes[0][2]==tosouth)
{
if(strncmp(&map[snakes[0][0]+snakes[0][2]][snakes[0][1]],"静",2)==0)
{
return 1;
}
else if(strncmp(&map[snakes[0][0]+snakes[0][2]][snakes[0][1]],"爱",2)==0)
{
return 1;
}
}
else
{
if(strncmp(&map[snakes[0][0]][snakes[0][1] + snakes[0][2]], "静", 2)==0)
{
return 1;
}
else if(strncmp(&map[snakes[0][0]][snakes[0][1]+snakes[0][2]],"爱",2)==0)
{
return 1;
}
}
return 0;
}
int main()
{
fmian();
kge();
xzndu();
snake();
food();
while(1)
{
system("cls");
d1=change();
snakeup();
snakemove();
xiemap();
if(d&&d1)
{
system("cls");
break;
}
if(sisnake())
{
d = 1;
}
else
{
d = 0;
}
Sleep(v);
}
system("pause");
return 0;
}