不知多少年前写的
发出来玩玩算了
/************************************************************************************/
// 方向键控制蛇方向
// q键退出
/************************************************************************************/
#include
#include
#include
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define width 78 //宽度
#define height 24 //高度
#define gamespeed 20 //游戏速度
struct gamestrcut
{
enum res
{
null, //空
snakebody, //蛇身体
wood, //木箱
food, //食物
};
res type; //类型
long time; //仅限蛇身用,用来计算蛇长
};
gamestrcut map[width][height]; //地图
char snakehead[4]; //x,y,direct,length //x轴位置,y轴位置,前进方向,蛇长
void main()
{
printf("操作方式:\n");
printf("方向键控制蛇方向,Q键退出\n");
printf("回车开始游戏\n");
getchar();
srand(time(0)); //设置随机数
ZeroMemory(map, sizeof(map));
ZeroMemory(snakehead, sizeof(snakehead));
//设置木箱
for (unsigned long w = 0; w < width; ++w)
{
map[w][0].type = gamestrcut::wood;
map[w][height-1].type = gamestrcut::wood;
}
for (unsigned long h = 0; h < height; ++h)
{
map[0][h].type = gamestrcut::wood;
map[width-1][h].type = gamestrcut::wood;
}
//设置蛇数据
snakehead[0] = width / 2;
snakehead[1] = height / 2;
snakehead[2] = 0;
snakehead[3] = 2;
map[snakehead[0]][snakehead[1]].type = gamestrcut::snakebody;
map[snakehead[0]][snakehead[1]].time = snakehead[3];
//提前生成一个食物
{
do
{
unsigned long x = ((float)rand() / RAND_MAX) * width;
unsigned long y = ((float)rand() / RAND_MAX) * height;
if (map[x][y].type == gamestrcut::null)
{
map[x][y].type = gamestrcut::food;
break;
}
} while (true);
}
do
{
//取按下键状态,该循环是为了提高灵敏度
for (unsigned long t = 0; t < gamespeed;++t)
{
Sleep(1);
if (KEYDOWN(VK_UP))
{
if (snakehead[2] != 3)
snakehead[2] = 1;
}
else if (KEYDOWN(VK_RIGHT))
{
if (snakehead[2] != 4)
snakehead[2] = 2;
}
else if (KEYDOWN(VK_DOWN))
{
if (snakehead[2] != 1)
snakehead[2] = 3;
}
else if (KEYDOWN(VK_LEFT))
{
if (snakehead[2] != 2)
snakehead[2] = 4;
}
else if (KEYDOWN('Q'))
{
goto end;
}
}
//控制蛇头
switch (snakehead[2])
{
case 1:
--snakehead[1];
break;
case 2:
++snakehead[0];
break;
case 3:
++snakehead[1];
break;
case 4:
--snakehead[0];
break;
default:
break;
}
//各种控制反应
switch (map[snakehead[0]][snakehead[1]].type)
{
case gamestrcut::null:
map[snakehead[0]][snakehead[1]].type = gamestrcut::snakebody;
map[snakehead[0]][snakehead[1]].time = snakehead[3];
break;
case gamestrcut::snakebody:
if (snakehead[2] != 0)
goto end;
break;
case gamestrcut::food:
map[snakehead[0]][snakehead[1]].type = gamestrcut::snakebody;
map[snakehead[0]][snakehead[1]].time = snakehead[3];
++snakehead[3];
for (unsigned long s = 0; s < sizeof(map) / sizeof(map[0]); ++s)
{
if (((gamestrcut*)map)[s].type == gamestrcut::snakebody)
++((gamestrcut*)map)[s].time;
}
for (unsigned long i = 0; i < width; ++i)
{
for (unsigned long j = 0; j < height; j++)
{
if (map[i][j].type == gamestrcut::snakebody)
{
++map[i][j].time;
}
}
}
break;
case gamestrcut::wood:
goto end;
break;
default:
break;
}
//刷新食物
{
static char foodfresh;
++foodfresh;
do
{
if (foodfresh <= 10)
break;
foodfresh = 0;
unsigned long x = ((float)rand() / RAND_MAX) * width;
unsigned long y = ((float)rand() / RAND_MAX) * height;
if (map[x][y].type == gamestrcut::null)
{
map[x][y].type = gamestrcut::food;
break;
}
} while (true);
}
//刷新蛇身
for (unsigned long i = 0; i < width; ++i)
{
for (unsigned long j = 0; j < height; j++)
{
if (snakehead[2] == 0)
break;
if (map[i][j].type == gamestrcut::snakebody)
{
--map[i][j].time;
if (map[i][j].time <= 0)
map[i][j].type = gamestrcut::null;
}
}
}
//屏幕输出
system("cls");
for (unsigned long i = 0; i < height; ++i)
{
char line[width + 3];
for (unsigned long j = 0; j < width; ++j)
{
switch (map[j][i].type)
{
case gamestrcut::null:
line[j] = ' ';
break;
case gamestrcut::snakebody:
line[j] = 'S';
break;
case gamestrcut::food:
line[j] = 'F';
break;
case gamestrcut::wood:
line[j] = 'W';
break;
default:
line[j] = '!';
break;
}
}
line[width] = 0x0D;
line[width + 1] = 0x0A;
line[width + 2] = 0;
printf(line);
}
} while (true);
end:
printf("你的分数是:%d\n",(long)snakehead[3] - 2);
getchar();
return;
}