#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<conio.h>
using namespace std;
const int N = 22;
int m[N][N];
class map
{
private:
typedef struct{ int x, y; }Point;
Point snake[400], Next;
int head, tail;
int grade, length, autotime;
char direction;
public:
void init();
void Update();
void hello();
int GO();
void EAT();
void FAILURE();
int getNx()
{
return Next.x;
}
int getNy()
{
return Next.y;
}
};
void map::init()
{
int i, j;
for (i = 1; i <= 20; i++)
{
for (j = 1; j <= 20; j++)
{
m[i][j] = ' ';
}
}
for (i = 0; i <= 21; i++)
{
m[0][i] = m[21][i] = m[i][0] = m[i][21] = '#';
}
m[1][1] = m[1][2] = 'o'; //蛇身(含蛇尾)
m[1][3] = '$'; //蛇头
head = 2; tail = 0; //开始时头和尾的下标
snake[head].x = 1; snake[head].y = 3; //开始时蛇头在地图上的下标
snake[tail].x = 1; snake[tail].y = 1; //开始时蛇尾在地图上的下标
snake[1].x = 1; snake[1].y = 2; //开始时蛇身在地图上的下标
grade = 1; length = 3; autotime = 500; //开始的等级、长度、自动前进时间
direction = 77; //初始的运动方向向右
}
void map::Update()
{
system("cls"); //清屏
int i, j;
printf("\n");
for (i = 0; i < 22; i++)
{
printf("\t");
for (j = 0; j < 22; j++)
printf("%c ", m[i][j]);
if (i == 14)
printf("\t等级为:%d", grade);
if (i == 16)
printf("\t长度为:%d", length);
if (i == 18)
printf("\t自动前进时间");
if (i == 20)
printf("\t间隔为:%d ms", autotime);
printf("\n");
}
}
void map::hello()
{
puts("\n\n\n\t\t\t贪吃蛇游戏即将开始!"); //准备开始
double start;
for (int i = 3; i >= 0; i--)
{
start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前为止运行的时间
while ((double)clock() / CLOCKS_PER_SEC - start <= 1); //经过1秒之后
if (i > 0)
{
system("cls"); //清屏
printf("\n\n\n\t\t\t进入倒计时:%d\n", i); //倒计时
}
else
Update(); //刷新地图
}
}
int map::GO()
{
bool timeover = true;
double start = (double)clock() / CLOCKS_PER_SEC;
while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= autotime / 1000.0)) && !_kbhit());
if (timeover)
{
_getch();
direction = _getch(); //获取方向
}
switch (direction)
{
case 72:
Next.x = snake[head].x - 1; Next.y = snake[head].y; //向上
break;
case 80:
Next.x = snake[head].x + 1; Next.y = snake[head].y; //向下
break;
case 75:
Next.x = snake[head].x; Next.y = snake[head].y - 1; //向左
break;
case 77:
Next.x = snake[head].x; Next.y = snake[head].y + 1; //向右
break;
default:
system("cls");
printf("\n\n\n");
puts("\t\t\tGame over!"); //按下非方向键游戏失败
return 0;
}
if (Next.x == 0 || Next.x == 21 || Next.y == 0 || Next.y == 21) //撞到边界
{
system("cls");
printf("\n\n\n");
puts("\t\t\tGame over!");
return 0;
}
if (m[Next.x][Next.y] != ' '&& m[Next.x][Next.y] != '@') //吃到自己
{
system("cls");
printf("\n\n\n");
puts("\t\t\tGame over!");
return 0;
}
if (length == 400) //最长蛇长
{
system("cls");
printf("\n\n\n");
puts("\t\t\tGame over!");
return 0;
}
return 1;
}
void map::EAT()
{
length++; //长度增加1
int _grade = length / 10 + 1; //计算等级
if (_grade != grade)
{
grade = _grade;
if (autotime >= 100)
autotime = 550 - grade * 50; //增加一级自动时间减短50毫秒
}
m[Next.x][Next.y] = '$'; //蛇头位置变化
m[snake[head].x][snake[head].y] = 'o'; //原蛇头位置变化为蛇身
head = (head + 1) % 400; //蛇头下标加1
snake[head].x = Next.x; snake[head].y = Next.y; //蛇头下标变化
//f(); //随机生成食物位置
Update(); //刷新地图
}
void map::FAILURE()
{
m[snake[tail].x][snake[tail].y] = ' '; //蛇尾原来的位置变成“ ”
tail = (tail + 1) % 400; //蛇尾下标加1
m[Next.x][Next.y] = '$'; //蛇头位置变化
m[snake[head].x][snake[head].y] = 'o'; //原蛇头位置变化为蛇身
head = (head + 1) % 400; //蛇头下标加1
snake[head].x = Next.x; //蛇头下标变化
snake[head].y = Next.y;
Update(); //刷新地图
}
class food
{
private:
int x;
int y;
public:
int getx()
{
return x;
}
int gety()
{
return y;
}
void create();
};
void food::create()
{
srand(int(time(0))); //种子函数
do{
x = rand() % 20 + 1;
y = rand() % 20 + 1;
} while (m[x][y] != ' ');
m[x][y] = '@';
}
int main()
{
map map1; //创建类对象
food food1; //创建类对象
map1.init();
food1.create();
map1.hello();
while (1)
{
if (map1.GO())
{
if (map1.getNx() == food1.getx() && map1.getNy() == food1.gety())
{
map1.EAT();
food1.create();
}
else
map1.FAILURE();
}
else
return 0;
}
return 0;
}
C++类方法实现简单的贪吃蛇游戏
最新推荐文章于 2022-10-02 12:22:24 发布