版权声明:转载请注明出处 https://blog.csdn.net/qq_35294564/article/details/82761215
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <iomanip>
#include <cmath>
#include <vector>
using namespace std;
//定义全局变量
int length = 4;
int speed = 1;
int map[20][20];
int appleX, appleY;//定义苹果坐标为全局变量
int headX, headY;//蛇头坐标
int score = 0;
char op = 'o';//获取操作指令
bool isEated = false;//是否吃到了?
bool GameOver = false;//游戏结束否?
bool crashed = false;//是否撞墙或者撞到自己
//函数声明
void init();//界面初始化
void refresh();//刷新界面
void goUp();//向上
void goDown();//向下
void goLeft();//向左
void goRight();//向右
void createApple();//随机产生苹果
void autoRun();//最后,会自己跑路了
void printPic();//打印
//定义类
class Point {
int xvar;
int yvar;
public:
Point() {};
~Point() {};
Point(int x, int y) :xvar(x), yvar(y) {}
int x()const { return xvar; };
int y()const { return yvar; };
};
enum class Direction{up,down,left,right};
Direction dire = Direction::right;//默认朝右走
class Snake {//蛇类
int length = length;
int speed = speed;
public:
Snake() {};
~Snake() {};
int len()const { return length; };
int sp()const { return speed; };
vector<Point> snakeBody{ Point(3,3),Point(3,4),Point(3,5),Point(3,6) };//蛇身
Direction direct = Direction::right;//蛇头方向
};
Snake *s = new Snake;//定义为全局蛇,方便操作
//主函数
int main() {
init();
refresh();
goRight();
system("pause");
return 0;
}
//函数实体
void init() {
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
if (i == 0 || i == 19 || j == 0 || j == 19)
map[i][j] = 1;
else
map[i][j] = 0;
createApple();//随机产生苹果位置
}
void refresh() {
if(isEated)
createApple();//随机产生苹果位置
for (vector<Point>::iterator it = s->snakeBody.begin(); it != s->snakeBody.end(); it++) {
map[(*it).x()][(*it).y()] = 1;
headX = (*(s->snakeBody.end() - 1)).x();
headY = (*(s->snakeBody.end() - 1)).y();//实时蛇头坐标
if (headX == appleX && headY == appleY)//吃到了!
isEated = true;
}
//printPic();
autoRun();
}
void printPic() {
system("cls");
cout << endl << "-----------------贪吃蛇-----------------" << endl;
cout << endl << " 分数:"<<score<< endl << endl;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (map[i][j] == 1) {
cout << "■";
}
else
cout << setw(2) << "";
}
cout << endl;
}
cout << "蛇头位置:(" <<headX << "," << headY << ")" << endl;
cout << "蛇长:" << s->snakeBody.size() << endl;
cout << "请输入W、S、A、D控制方向:";
}
void createApple() {//随机产生苹果位置
while (true) {
srand((int)time(NULL));
int row = rand() % 18 + 1;//产生1~18的随机数
int col = rand() % 18 + 1;
if (map[row][col] == 0) {
map[row][col] = 1;
appleX = row;
appleY = col;
break;
}
}
if (isEated)
score += 10;
isEated = false;//刚产生的苹果吃不到!
}
void goUp() { //四种情况①撞墙;②撞苹果;③撞自己;④都不撞
dire = Direction::up;
if (map[headX - 1][headY] == 0) {//都不撞(头插一点,尾删一点)
s->snakeBody.push_back(Point(headX - 1, headY));//头插一点
map[headX - 1][headY] = 1;
vector<Point>::iterator it = s->snakeBody.begin();//找到第一个点(尾部)
map[(*it).x()][(*it).y()] = 0;
s->snakeBody.erase(it);//尾删一点
}
else if ((headX - 1 == appleX) && (headY == appleY)) {//撞苹果,吃到(头插一点,尾不删)
s->snakeBody.push_back(Point(headX - 1, headY));//头插一点
map[headX - 1][headY] = 1;
}
else if (map[headX - 1][headY] == 1) {//不是撞苹果->撞墙:GameOver
crashed = true;
return;
}
printPic();
Sleep(300);
refresh();
}
void goDown() { //四种情况①撞墙;②撞苹果;③撞自己;④都不撞
dire = Direction::down;
if (map[headX + 1][headY] == 0) {//都不撞(头插一点,尾删一点)
s->snakeBody.push_back(Point(headX + 1, headY));//头插一点
map[headX + 1][headY] = 1;
vector<Point>::iterator it = s->snakeBody.begin();//找到第一个点(尾部)
map[(*it).x()][(*it).y()] = 0;
s->snakeBody.erase(it);//尾删一点
}
else if ((headX + 1 == appleX) && (headY == appleY)) {//撞苹果,吃到(头插一点,尾不删)
s->snakeBody.push_back(Point(headX + 1, headY));//头插一点
map[headX + 1][headY] = 1;
}
else if (map[headX + 1][headY] == 1) {//撞墙:GameOver
crashed = true;
return;
}
printPic();
Sleep(300);
refresh();
}
void goLeft() { //四种情况①撞墙;②撞苹果;③撞自己;④都不撞
dire = Direction::left;
if (map[headX][headY - 1] == 0) {//都不撞(头插一点,尾删一点)
s->snakeBody.push_back(Point(headX, headY - 1));//头插一点
map[headX][headY - 1] = 1;
vector<Point>::iterator it = s->snakeBody.begin();//找到第一个点(尾部)
map[(*it).x()][(*it).y()] = 0;
s->snakeBody.erase(it);//尾删一点
}
else if ((headX == appleX) && (headY - 1 == appleY)) {//撞苹果,吃到(头插一点,尾不删)
s->snakeBody.push_back(Point(headX, headY - 1));//头插一点
map[headX][headY - 1] = 1;
}
else if (map[headX][headY - 1] == 1) {//撞墙:GameOver
crashed = true;
return;
}
printPic();
Sleep(300);
refresh();
}
void goRight() { //四种情况①撞墙;②撞苹果;③撞自己;④都不撞
dire = Direction::right;
if (map[headX][headY + 1] == 0) {//都不撞(头插一点,尾删一点)
s->snakeBody.push_back(Point(headX, headY + 1));//头插一点
map[headX][headY + 1] = 1;
vector<Point>::iterator it = s->snakeBody.begin();//找到第一个点(尾部)
map[(*it).x()][(*it).y()] = 0;
s->snakeBody.erase(it);//尾删一点
}
else if ((headX == appleX) && (headY + 1 == appleY)) {//撞苹果,吃到(头插一点,尾不删)
s->snakeBody.push_back(Point(headX, headY + 1));//头插一点
map[headX][headY + 1] = 1;
}
else if (map[headX][headY + 1] == 1) {//撞墙:GameOver
crashed = true;
return;
}
printPic();
Sleep(300);
refresh();
}
void autoRun() {
while (!crashed) {
if (_kbhit()) {//非阻塞监听键盘事件,有键盘事件为真,否则返回零
op = _getche();
switch (op)
{
case 'w':
case 'W':
if (dire == Direction::down)
break;
dire = Direction::up; goUp(); break;
case 's':
case 'S':
if (dire == Direction::up)
break;
dire = Direction::down; goDown(); break;
case 'a':
case 'A':
if (dire == Direction::right)
break;
dire = Direction::left; goLeft(); break;
case 'd':
case 'D':
if (dire == Direction::left)
break;
dire = Direction::right; goRight(); break;
default:
break;
}
}
else {
if (dire == Direction::up) {
goUp();
}
else if (dire == Direction::down) {
goDown();
}
else if (dire == Direction::left) {
goLeft();
}
else {
goRight();
}
}
}
if (crashed) {
MessageBox(0, TEXT("不要灰心!下次努力!"), TEXT("Sorry,defeat!!"), 0);
delete(s);
exit(0);
}
}
运行效果: