贪吃蛇功能实现:
(1)定义贪吃蛇游戏棋盘图
(2)初始化棋盘
(3)输出棋盘所在信息
(3)选择游戏难度
(4)随机产生食物
(5)更新游戏动态
(6)设置游戏相应的操作
(7)打印游戏结果
代码实现:
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 32;//定义贪吃蛇地图大小
//定义贪吃蛇的坐标类
class SnakePosition
{
public:
int x, y; //x表示行,y表示列
void Initialize(int & j)//初始化坐标
{
x = 1;
y = j;
}
};
//定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
SnakePosition position[(N - 2)*(N - 2) + 1];
class SnakeMap//定义贪吃蛇的游戏类
{
public:
SnakeMap(int h = 4, int t = 1, int l = 4, char d = 77, int s = 0)//构造
: length(l)
, direction(d)
, head(h)
, tail(t)
, score(s)
{}
void Initialize() //定义初始化函数,将贪吃蛇的棋盘图进行初始化
{
int i, j;
for (i = 1; i <= 3; i++)
s[1][i] = '*';
s[1][4] = '#';
for (i = 1; i <= N - 2; i++)
for (j = 1; j <= N - 2; j++)
s[i][j] = ' '; // 初始化贪吃蛇棋盘中间空白部分
for (i = 0; i <= N - 1; i++)
s[0][i] = s[N - 1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for (i = 1; i <= N - 2; i++)
s[i][0] = s[i][N - 1] = '|'; //初始化贪吃蛇棋盘左右墙壁
}
void ShowGame()//输出贪吃蛇棋盘信息
{
system("cls"); // 清屏
int i, j;
cout << endl;
for (i = 0; i<N; i++)
{
cout << '\t';
for (j = 0; j<N; j++)
cout << s[i][j] << ' '; // 输出贪吃蛇棋盘
if (i == 2)
cout << "\t等级:" << grade;
if (i == 6)
cout << "\t速度:" << gamespeed;
if (i == 10)
cout << "\t得分:" << score << "分";
if (i == 14)
cout << "\t暂停:按一下空格键";
if (i == 18)
cout << "\t继续:按两下空格键";
cout << endl;
}
}
int UpdataGame()//更新当前游戏进度
{
P p;
p.x1 = 0;
p.y1 = 0;
int gameover = 1;
int key = direction;
long start = clock();
while ((gameover = (clock() - start <= gamespeed)) && !_kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if (gameover)
{
_getch();
//_getch()接受任意一个从键盘上输入的字符,
//转化为对应的ASCII(不需要输入回车就可以接收)
key = _getch();
}
if (key == ' ')
{
//按空格键暂停
//按空格键继续的功能
while (_getch() != ' ')
{}
}
else
direction = key;
switch (direction)
{
case 72:// 向上
x = position[head].x - 1;
y = position[head].y;
break;
case 80:// 向下
x = position[head].x + 1;
y = position[head].y;
break;
case 75:// 向左
x = position[head].x;
y = position[head].y - 1;
break;
case 77:// 向右
x = position[head].x;
y = position[head].y + 1;
}
// 按键非方向键
if (!(direction == 72 || direction == 80 || direction == 75 || direction == 77))
gameover = 0;
else if (x == 0 || x == N - 1 || y == 0 || y == N - 1) // 碰到墙壁
gameover = 0;
else if (s[x][y] != ' ' && !(x == p.x1 && y == p.y1)) // 蛇头碰到蛇身
gameover = 0;
else if (x == p.x1 && y == p.y1)
{
// 吃到食物,长度加1
length++;
if (length >= 8 && gameauto)
{
length -= 8;
grade++;
if (gamespeed >= 200)
gamespeed -= 200; // 改变贪吃蛇前进速度
else
gamespeed = 100;
}
s[x][y] = '#'; //更新蛇头
s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身
head = (head + 1) % ((N - 2)*(N - 2)); //取蛇头坐标
position[head].x = x;
position[head].y = y;
ShowGame();
gameover = 1;
score += grade * 20; //加分
SetPoint(); //产生米
}
else
{
// 不吃米
s[position[tail].x][position[tail].y] = ' ';//将蛇尾置空
tail = (tail + 1) % ((N - 2) * (N - 2));//更新蛇尾坐标
s[position[head].x][position[head].y] = '*'; //将蛇头更为蛇身
head = (head + 1) % ((N - 2) * (N - 2));
position[head].x = x;
position[head].y = y;
s[position[head].x][position[head].y] = '#'; //更新蛇头
gameover = 1;
}
return gameover;
}
void SetPoint()//随机产生食物
{
P p;
srand((unsigned int)time(0));
//设置rand()随机序列种子。对于给定的种子seed, rand()会反复产生特定的随机序列
do
{
p.x1 = rand() % (N - 2) + 1;
p.y1 = rand() % (N - 2) + 1;
} while (s[p.x1][p.y1] != ' ');
s[p.x1][p.y1] = '*';
}
void GetGrade()//选择游戏等级
{
cin >> grade;
while (grade>7 || grade<1)
{
cout << "请输入数字1-7选择等级,输入其他数字无效" << endl;
cin >> grade;
}
switch (grade)
{
case 1:
gamespeed = 1000;
gameauto = 0;
break;
case 2:
gamespeed = 800;
gameauto = 0;
break;
case 3:
gamespeed = 600;
gameauto = 0;
break;
case 4:
gamespeed = 400;
gameauto = 0;
break;
case 5:
gamespeed = 200;
gameauto = 0;
break;
case 6:
gamespeed = 100;
gameauto = 0;
break;
case 7:
grade = 1;
gamespeed = 1000;
gameauto = 1;
break;
}
}
void Display()//输出等级,得分情况以及称号
{
cout << "\n\t\t\t\t等级:" << grade;
cout << "\n\n\n\t\t\t\t速度:" << gamespeed;
cout << "\n\n\n\t\t\t\t得分:" << score << "分";
}
private:
char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
int grade, length;
int gamespeed; //前进时间间隔
char direction; // 初始情况下,向右运动
int head, tail;
int score;
bool gameauto;
struct P
{
int x1;
int y1; // 随机出米
};
int x, y;
};
int main()
{
char ctn = 'y';
int nodead = 1;
cout << "\n\n\n\n\n\t\t\t 欢迎进入贪吃蛇游戏!" << endl;//欢迎界面;
cout << "\n\n\n\t\t\t 按任意键马上开始。。。" << endl;//准备开始;;
_getch();
while (ctn == 'y')
{
system("cls"); // 清屏
SnakeMap snake;
snake.Initialize();
cout << "\n\n请输入数字选择游戏等级:" << endl;
cout << "\n\n\n\t\t\t1.等级一:速度 1000 \n\n\t\t\t2.等级二:速度 800\
\n\n\t\t\t3.等级三:速度 600 \n\n\t\t\t4.等级四:速度 400\
\n\n\t\t\t5.等级五:速度 200 \n\n\t\t\t6.等级六:速度 100\
\n\n\t\t\t7.自动升级模式" << endl;
snake.GetGrade();//获取等级
for (int i = 1; i <= 4; i++)
{
position[i].Initialize(i);//初始化坐标
}
snake.SetPoint(); // 产生第一个米
do
{
snake.ShowGame();
nodead = snake.UpdataGame();
} while (nodead);
system("cls"); //清屏
cout << "\n\n\n\t\t\t\tGameover!\n\n" << endl;
snake.Display();//输出等级/得分情况
cout << "\n\n\n\t\t 是否选择继续游戏?输入 y 继续,n 退出" << endl;
cin >> ctn;
}
return 0;
}