该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#define X 30
#define Y 15
void guozi(int *x, int *y);
int main(void)
{
char map[X][Y];
int x;
int y;//横纵坐标
int i;
int j;//标记蛇头
int p, q;//标记蛇尾
int t, d;//寻找蛇尾
int n = 4;//蛇的长度
int r;//交换变量
int c, v;//果子坐标
int map1[X][Y];
int key; //定义key,用于接收按键键值
int f=4;//记录方向,起始方向向右 1上 2下 3左 4右
int t_time=time(NULL);//记录时间
for (y = 0; y < Y; y++)
for (x = 0; x < X; x++)
map[x][y] = * *;
for (y = 0; y < Y;)
{
for (x = 0; x < X; x++)
map[x][y] = *-*;
if (x == 30)
break;
}
for (x = 0; x < X;)
{
for (y = 1; y
map[x][y] = *|*;
if (y == 15)
break;
}
for (y = 14; y < Y;)
{
for (x = 0; x < X; x++)
map[x][y] = *-*;
if (x == 30)
break;
}
for (x = 29; x < X;)
{
for (y = 1; y
map[x][y] = *|*;
if (y == 14)
break;
}//绘图
for (y = 0; y < Y; y++)
for (x = 0; x < X; x++)
map1[x][y] = 0;
map1[4][2] = 1;
map1[3][2] = 2;
map1[2][2] = 3;
map1[1][2] = 4;
for (y = 0; y < Y; y++)
{
for (x = 0; x < X; x++)
{
if (map1[x][y] == 1)
map[x][y] = *o*;
else if (map1[x][y]>1)
map[x][y] = ***;
}
}
guozi(&c, &v);//生成果子
while (map1[c][v]>0 && (map[c][v] == *|* || map[c][v] == *-*))
{guozi(&c, &v);
}//描绘果子
if (map1[c][v] == 0 && map[c][v] != *|* && map[c][v] != *-*)
{
map1[c][v] = -1;
map[c][v] = ***;
}
for (y = 0; y < Y; y++)
{
for (x = 0; x < X; x++)
printf("%c", map[x][y]);
printf("\n");
}//画蛇
i = 4;
j = 2;//标记蛇头
p = 1;
q = 2;//标记蛇尾
while (1)
{
while (_kbhit() == 0)
if (t_time < time(NULL))
{
if (f == 4)
{
if (i == 28)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i + 1][j] == 2)
{
break;
}
if (map1[i + 1][j] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i + 1][j] == -1)
{
n++;
map1[i + 1][j] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v] > 0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto b4;
}
map1[i + 1][j] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
b4:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
i++;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
t_time = time(NULL);
}//*******************************自动向右
if (f == 3)
{
if (i == 1)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i - 1][j] == 2)
{
break;
}
if (map1[i - 1][j] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i - 1][j] == -1)
{
n++;
map1[i - 1][j] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v] > 0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto b3;
}
map1[i - 1][j] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
b3:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
i--;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
}//******************自动向左
if (f == 1)
{
if (j == 1)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j - 1] == 2)
{
break;
}
if (map1[i][j - 1] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j - 1] == -1)
{
n++;
map1[i][j - 1] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v] > 0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto b1;
}
map1[i][j - 1] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
b1:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
j--;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
}//***********************************自动向上
if (f == 2)
{
if (j == 13)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j + 1] == 2)
{
break;
}
if (map1[i][j + 1] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j + 1] == -1)
{
n++;
map1[i][j + 1] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v] > 0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto b2;
}
map1[i][j + 1] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
b2:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
j++;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
}//**************************自动向下
for (y = 0; y < Y; y++)
{
for (x = 0; x < X; x++)
{
if (map1[x][y] == 0)
map[x][y] = * *;
if (map1[x][y] == 1)
map[x][y] = *o*;
else if (map1[x][y]>1)
map[x][y] = ***;
}
}
for (y = 0; y < Y;)
{
for (x = 0; x < X; x++)
map[x][y] = *-*;
if (x == 30)
break;
}
for (x = 0; x < X;)
{
for (y = 1; y
map[x][y] = *|*;
if (y == 15)
break;
}
for (y = 14; y < Y;)
{
for (x = 0; x < X; x++)
map[x][y] = *-*;
if (x == 30)
break;
}
for (x = 29; x < X;)
{
for (y = 1; y
map[x][y] = *|*;
if (y == 14)
break;
}//绘图
system("cls");
for (y = 0; y < Y; y++)
{
for (x = 0; x < X; x++)
printf("%c", map[x][y]);
printf("\n");
}//画蛇
t_time = time(NULL);
}
key = _getch();
switch (key)
{
case 72:
if (j == 1)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j - 1] == 2)
{
break;
}
if (map1[i][j - 1] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j - 1] == -1)
{
n++;
map1[i][j - 1] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v]>0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto a1;
}
map1[i][j - 1] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
a1:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j]-1&&map1[t][d]!=1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
j--;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
f = 1;
break;
case 80:if (j == 13)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j + 1] == 2)
{
break;
}
if (map1[i][j + 1] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i][j + 1] == -1)
{
n++;
map1[i][j + 1] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v]>0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto a2;
}
map1[i][j + 1] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
a2:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
j++;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
f = 2; break;
case 75:if (i == 1)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i - 1][j] == 2)
{
break;
}
if (map1[i - 1][j] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i-1][j] == -1)
{
n++;
map1[i-1][j] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v]>0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto a3;
}
map1[i-1][j] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
a3:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
i--;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
f = 3; break;
case 77:
if (i == 28)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i + 1][j] == 2)
{
break;
}
if (map1[i + 1][j] > 2)
{
system("cls");
printf("Game over.\n");
system("pause");
return 0;
}
if (map1[i+1][j] == -1)
{
n++;
map1[i+1][j] = 1;
map1[i][j] = n;
guozi(&c, &v);//生成果子
while (map1[c][v]>0 || map[c][v] == *|* || map[c][v] == *-*)
{
guozi(&c, &v);
}//描绘果子
map1[c][v] = -1;
map[c][v] = ***;
goto a4;
}
map1[i + 1][j] = 1;//蛇头位移
map1[i][j] = map1[p][q];//蛇尾迁入
map1[p][q] = 0;//原蛇尾置零,擦掉蛇尾
a4:while (map1[i][j] > 2)
{
for (t = 0; t < X; t++)
for (d = 0; d < Y; d++)
{
if (map1[t][d] == map1[i][j] - 1 && map1[t][d] != 1)
{
p = t; q = d;//寻找新蛇尾
r = map1[i][j];//新旧蛇尾交换
map1[i][j] = map1[p][q];
map1[p][q] = r;
}
}
}
i++;
for (t = 0; t < X; t++)
{
for (d = 0; d < Y; d++)
{
if (map1[t][d] == n)
{
p = t; q = d;//寻找新蛇尾
}
}
}
f = 4;
break;
case 27:return 0; //键值27为Esc键,当按下这个键时退出程序
}
for (y = 0; y < Y; y++)
{
for (x = 0; x < X; x++)
{
if (map1[x][y] == 0)
map[x][y] = * *;
if (map1[x][y] == 1)
map[x][y] = *o*;
else if (map1[x][y]>1)
map[x][y] = ***;
}
}
for (y = 0; y < Y;)
{
for (x = 0; x < X; x++)
map[x][y] = *-*;
if (x == 30)
break;
}
for (x = 0; x < X;)
{
for (y = 1; y
map[x][y] = *|*;
if (y == 15)
break;
}
for (y = 14; y < Y;)
{
for (x = 0; x < X; x++)
map[x][y] = *-*;
if (x == 30)
break;
}
for (x = 29; x < X;)
{
for (y = 1; y
map[x][y] = *|*;
if (y == 14)
break;
}//绘图
system("cls");
for (y = 0; y < Y; y++)
{
for (x = 0; x < X; x++)
printf("%c", map[x][y]);
printf("\n");
}//画蛇
}
return 0;
}
void guozi(int *x, int *y)
{
srand((unsigned int)time(0));
*x = rand() % 28 + 1;
*y = rand() % 13 + 1;
}