#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
using namespace std; typedef struct{ int x, y; }Point; char map[][]; //定义一个22*22的地图(含边界)
Point snake[], food, Next; //定义蛇、食物、下一步蛇头的位置
int head, tail; //用于储存蛇头和蛇尾的下标
int grade, length, autotime; //游戏等级、蛇长、自动前进所需时间
char direction; //前进方向 //定位光标
void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition);
} //用inline定义内联函数节省程序运行时的调用开销
//刷新地图
inline void Update(char map[][], int grade, int length, int autotime)
{
//system("cls"); //清屏
gotoxy(, );
int i, j;
printf("\n");
for (i = ; i < ; i++)
{
printf("\t");
for (j = ; j < ; j++)
printf("%c ", map[i][j]);
if (i == )
printf("\t等级为:%d", grade);
if (i == )
printf("\t长度为:%d", length);
if (i == )
printf("\t自动前进时间");
if (i == )
printf("\t间隔为:%d ms", autotime);
printf("\n");
}
} //欢迎界面
inline void hello()
{
puts("\n\n\n\t\t\t贪吃蛇游戏即将开始!"); //准备开始
double start;
for (int i = ; i >= ; i--)
{
start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间
while ((double)clock() / CLOCKS_PER_SEC - start <= ); //经过1秒之后
if (i > )
{
system("cls"); //清屏
printf("\n\n\n\t\t\t进入倒计时:%d\n", i); //倒计时
}
else
Update(map, grade, length, autotime); //刷新地图
}
} //随机生成食物位置
inline void f()
{
srand(int(time())); //调用种子函数
do{
food.x = rand() % + ;
food.y = rand() % + ;
} while (map[food.x][food.y] != ' ');
map[food.x][food.y] = '!'; //食物为“!”
} //初始化
inline void init()
{
int i, j;
for (i = ; i <= ; i++)
for (j = ; j <= ; j++)
map[i][j] = ' ';
for (i = ; i <= ; i++)
map[][i] = map[][i] = map[i][] = map[i][] = '*'; //边界
map[][] = map[][] = 'O'; //蛇身(含蛇尾)
map[][] = '@'; //蛇头
head = ; tail = ; //开始时头和尾的下标
snake[head].x = ; snake[head].y = ; //开始时蛇头在地图上的下标
snake[tail].x = ; snake[tail].y = ; //开始时蛇尾在地图上的下标
snake[].x = ; snake[].y = ; //开始时蛇身在地图上的下标
f(); //随机生成食物位置
grade = ; length = ; autotime = ; //开始的等级、长度、自动前进时间
direction = ; //初始的运动方向向右
} //预前进
inline int GO()
{
bool timeover = true;
double start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间 L:
//自动经过1秒或者等待1秒内的键盘输入
while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= autotime / 1000.0)) && !_kbhit());
//键盘输入
if (timeover)
{
//_getch();
char d = _getch(); //获取方向
if (d != && d != && d != && d != || direction == && d == || direction == && d == || direction == && d == || direction == && d == )
goto L;
else
direction = d;
}
switch (direction)
{
case :
Next.x = snake[head].x - ; Next.y = snake[head].y; //向上
break;
case :
Next.x = snake[head].x + ; Next.y = snake[head].y; //向下
break;
case :
Next.x = snake[head].x; Next.y = snake[head].y - ; //向左
break;
case :
Next.x = snake[head].x; Next.y = snake[head].y + ; //向右
break;
default:
puts("\tGame over!"); //按下非方向键游戏失败
return ;
}
if (Next.x == || Next.x == || Next.y == || Next.y == ) //撞到边界
{
puts("\tGame over!");
return ;
}
if (map[Next.x][Next.y] != ' '&&!(Next.x == food.x&&Next.y == food.y)) //吃到自己
{
puts("\tGame over!");
return ;
}
if (length == ) //最长蛇长
{
puts("\tGood game!");
return ;
}
return ;
} //吃到食物
inline void EAT()
{
length++; //长度增加1
int _grade = length / + ; //计算等级
if (_grade != grade)
{
grade = _grade;
if (autotime >= )
autotime = - grade * ; //增加一级自动时间减短50毫秒
}
map[Next.x][Next.y] = '@'; //蛇头位置变化
map[snake[head].x][snake[head].y] = 'O'; //原蛇头位置变化为蛇身
head = (head + ) % ; //蛇头下标加1
snake[head].x = Next.x; snake[head].y = Next.y; //蛇头下标变化
f(); //随机生成食物位置
Update(map, grade, length, autotime); //刷新地图
} //没吃到食物
inline void FAILURE()
{
map[snake[tail].x][snake[tail].y] = ' '; //蛇尾原来的位置变成“ ”
tail = (tail + ) % ; //蛇尾下标加1
map[Next.x][Next.y] = '@'; //蛇头位置变化
map[snake[head].x][snake[head].y] = 'O'; //原蛇头位置变化为蛇身
head = (head + ) % ; //蛇头下标加1
snake[head].x = Next.x; //蛇头下标变化
snake[head].y = Next.y;
Update(map, grade, length, autotime); //刷新地图
} //main函数
int main()
{
system("color F0");
init(); //初始化
hello(); //欢迎界面
while ()
{
if (GO()) //预前进
{
if (Next.x == food.x&&Next.y == food.y)
EAT(); //吃到食物
else
FAILURE(); //没吃到食物
}
else
return ; //失败或者胜利,游戏结束
}
return ;
}
//
运行结果截图: