Snake Game

Snake Game 使用方式使用WSAD来实现上下左右移动Q退出P暂停。注意事项要C 11或以上的版本作者是在读大学生如果你对我的代码有任何问题或优化方法请务必告知❤️❤️代码#include iostream#include random#include vector#include Windows.h#include conio.h#include fstreamusing namespace std;const int WIDTH 22;const int LENGTH 32;// 蛇头的朝向enum Direction {UP,DOWN,LEFT,RIGHT};class GameMap {public:char gameMap[WIDTH][LENGTH]; // 游戏地图int food_x, food_y; // 食物的坐标int Score; // 得分int Round; // 轮次int AllTimeHigh_score; // 历史最高得分public:GameMap(){for (int j 0; j LENGTH; j) gameMap[0][j] gameMap[WIDTH - 1][j] -; // 上下边界for (int i 1; i WIDTH-1; i) gameMap[i][0] gameMap[i][LENGTH - 1] |; // 左右边界for (int i 1; i WIDTH - 1; i) for (int j 1; j LENGTH - 1; j) gameMap[i][j] ; // 中间清空Score Round 0; // 初始化Round用0是因为程序员的第一是0AllTimeHigh_score loadHighScore(); // 加载历史最高得分generate_food();}void generate_food(){random_device rd;mt19937 gen(rd());uniform_int_distribution dist_x(1, WIDTH - 2); food_x dist_x(gen);uniform_int_distribution dist_y(1, LENGTH - 2); food_y dist_y(gen);gameMap[food_x][food_y] $;}void gotoXY(int x, int y){HANDLE hConsole GetStdHandle(STD_OUTPUT_HANDLE); // 创建控制台操作许可证存储到变量hConsole中COORD pos { (SHORT)x, (SHORT)y }; // 创建光标要去的位置(x, y)的变量SetConsoleCursorPosition(hConsole, pos); // 设置控制台光标的位置}void show(){// system(cls); // 清屏刷新不再使用因为会使得屏幕闪烁体验差gotoXY(0, 0); // 操控光标到控制台左上角覆盖写入发生在cout后cout All-time high score: AllTimeHigh_score , Round: Round , Score: Score endl;for (int i 0; i WIDTH; i){for (int j 0; j LENGTH; j) cout gameMap[i][j];cout endl;}}int loadHighScore(){ifstream ifs(AllTimeHighScore.txt, ios::in);int hs 0;if (ifs hs) return hs;return 0;}void saveHighScore(int hs){ofstream ofs(AllTimeHighScore.txt, ios::out);ofs hs;}};class Snake : public GameMap {private:vectorpairint, int mySnake; // 蛇的坐标int head_x, head_y; // 蛇头Direction Dir; // 蛇头朝向public:Snake(){head_x 10, head_y 15;mySnake.push_back(make_pair(head_x, head_y));mySnake.push_back(make_pair(head_x, head_y-1)); // 初始时设置蛇的长度为2gameMap[head_x][head_y] 0;gameMap[head_x][head_y] o;Dir RIGHT;}bool control_direction(){if (_kbhit()){char key _getch();switch (key){case w: if(Dir ! DOWN) Dir UP; break;case s: if(Dir ! UP) Dir DOWN; break;case a: if(Dir ! RIGHT) Dir LEFT; break;case d: if(Dir ! LEFT) Dir RIGHT; break;case p: system(pause); break;case q: return false;}}return true;}bool move(){// 更新头部switch (Dir){case UP: head_x--; break;case DOWN: head_x; break;case LEFT: head_y--; break;case RIGHT: head_y; break;}mySnake.insert(mySnake.begin(), make_pair(head_x, head_y));// 撞墙检测if (head_x 0 || head_x WIDTH-1 || head_y 0 || head_y LENGTH-1){cout You hit the wall! endl;return false;}// 撞到自己检测for (int i 1; i (int)mySnake.size(); i)if (mySnake[i].first head_x mySnake[i].second head_y){cout You are eating yourself! endl;return false;}// 吃到食物了if (head_x food_x head_y food_y){if (Round 3) Score 5;else if (Round 7) Score 10;else if (Round 10) Score 15;else if (Round 30) Score 20;else if (Round 50) Score 25;else if (Round 100) Score 30;else Score 50;generate_food();Round;}// 没吃到要删除蛇尾else{gameMap[mySnake.back().first][mySnake.back().second] ;mySnake.pop_back();}gameMap[head_x][head_y] 0;gameMap[mySnake[1].first][mySnake[1].second] o;return true;}void run(){while (true){bool q control_direction();if (!move() || !q){cout Game over! endl;if (Score AllTimeHigh_score) saveHighScore(Score);return;}show();if (Round 3) Sleep(1000);else if (Round 7) Sleep(700);else if (Round 10) Sleep(500);else if (Round 30) Sleep(400);else if (Round 50) Sleep(300);else if (Round 100) Sleep(200);else Sleep(100);}}};int main(){Snake snake;snake.run();return 0;}