您好,登錄后才能下訂單哦!
這篇文章主要介紹C++如何實現簡易貪吃蛇游戲,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
C++實現建議貪吃蛇(不會閃屏幕)
使用vs2013完成。記錄踏上游戲開發的道路。
// 2021.7.24.1貪吃蛇.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include <iostream> #include <list> #include <numeric> #include <algorithm> #include <Windows.h> #include <WinUser.h> #include <cstdlib> #include <ctime> #include <vector> using namespace std; #define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0) #define MAX_SNAKE_LEN 20 #define MAP_MAXIMUM_HEIGHT 20 #define MAP_MAXIMUM_WIDTH 20 #define MAX_NUMBER_FRUIT 5 struct sSnakeBody { void setPostion(int x, int y) { nSnakeBodyX = x; nSnakeBodyY = y; } void setPostion(sSnakeBody* temp) { nSnakeBodyX = temp->nSnakeBodyX; nSnakeBodyY = temp->nSnakeBodyY; } int nSnakeBodyX; int nSnakeBodyY; }; typedef list<sSnakeBody> LISTSNAKEBODY; struct sSnake { sSnake() { nSnakeHeadX = 1; nSnakeHeadY = 1; nSnakeDirection = 1; speed = 5; } bool isExit(sSnakeBody temp) { if (nSnakeHeadX==temp.nSnakeBodyX&&nSnakeHeadY==temp.nSnakeBodyY) { return true; } return false; } bool isExit(int x, int y) { if (nSnakeHeadX == x&&nSnakeHeadY == y) { return true; } return false; } void reduction(sSnakeBody temp) { nSnakeHeadX = 2 * nSnakeHeadXBk - temp.nSnakeBodyX; nSnakeHeadY = 2 * nSnakeHeadYBk - temp.nSnakeBodyY; } int nSnakeHeadX; int nSnakeHeadY; int nSnakeHeadXBk; int nSnakeHeadYBk; int nSnakeDirection;//0表示上,1表示右,2表示下,3表示左,順時針 int speed;//指的是幾個循環前進一次 LISTSNAKEBODY snakeBodyList[MAX_SNAKE_LEN]; }; struct sFruit { int FruitX; int FruitY; sFruit() { FruitX = -1; FruitY = -1; } sFruit(int x, int y) { FruitX = x; FruitY = y; } bool isExit(sSnake snake) { if (FruitX == snake.nSnakeHeadX && FruitY == snake.nSnakeHeadY) return true; else return false; } }; vector<sFruit> gFruitVector; sSnake gSnake; //顯示地圖 void showArrMap(int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { system("cls"); for (int i = 0; i < MAP_MAXIMUM_HEIGHT; i++) { for (int j = 0; j < MAP_MAXIMUM_WIDTH; j++) { if (1 == arrMap[i][j] || gSnake.isExit(i,j)) { cout << "■"; } else if (arrMap[i][j] == 2) { cout << "●"; } else { cout << " "; } } if (4 == i) { cout << "\t得分:" << gSnake.snakeBodyList->size(); } cout << endl; } } //將數據加載到地圖上。返回值為加載后游戲是否失敗 bool mapLoadData(int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { //加載水果的數據 for (unsigned int i = 0; i < gFruitVector.size(); i++) { arrMap[gFruitVector[i].FruitX][gFruitVector[i].FruitY] = 2; } //加載蛇的身體數據 for (LISTSNAKEBODY::iterator iter = gSnake.snakeBodyList->begin(); iter != gSnake.snakeBodyList->end(); iter++) { arrMap[iter->nSnakeBodyX][iter->nSnakeBodyY] = 1; } // 吃到第一個水果的時候,一定不是游戲失敗 if (gSnake.snakeBodyList->size() == 1 && gSnake.isExit(gSnake.snakeBodyList->front())) { return false; } if (arrMap[gSnake.nSnakeHeadX][gSnake.nSnakeHeadY] == 1) return true; return false; } //更新蛇的數據 void updateSnake(int &nProgramCounter) { if (nProgramCounter > gSnake.speed) { gSnake.nSnakeHeadXBk = gSnake.nSnakeHeadX; gSnake.nSnakeHeadYBk = gSnake.nSnakeHeadY; if (gSnake.nSnakeDirection == 0) { gSnake.nSnakeHeadX--; } else if (gSnake.nSnakeDirection == 1) { gSnake.nSnakeHeadY++; } else if (gSnake.nSnakeDirection == 2) { gSnake.nSnakeHeadX++; } else if (gSnake.nSnakeDirection == 3) { gSnake.nSnakeHeadY--; } if (gSnake.snakeBodyList->size() != 0) { if (gSnake.isExit(gSnake.snakeBodyList->front())) { gSnake.reduction(gSnake.snakeBodyList->front()); } } //蛇的身體移動 if (gSnake.snakeBodyList->size() != 0) { sSnakeBody snakeBody; snakeBody.setPostion(gSnake.nSnakeHeadXBk, gSnake.nSnakeHeadYBk); gSnake.snakeBodyList->push_front(snakeBody); gSnake.snakeBodyList->pop_back(); } nProgramCounter = 0; } } //綁定鍵盤事件 void bindKeyboardEvents() { if (KEY_DOWN(VK_UP)) { gSnake.nSnakeDirection = 0; } else if (KEY_DOWN(VK_RIGHT)) { gSnake.nSnakeDirection = 1; } else if (KEY_DOWN(VK_DOWN)) { gSnake.nSnakeDirection = 2; } else if (KEY_DOWN(VK_LEFT)) { gSnake.nSnakeDirection = 3; } if (KEY_DOWN(VK_SHIFT)) { gSnake.speed = 2; } else { gSnake.speed = 5; } } //隨機生成 N 個水果, void randomlyGeneratedFruit(int N, int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { int maxi = MAP_MAXIMUM_HEIGHT; int maxj = MAP_MAXIMUM_WIDTH; int AllCnt = 0; vector<int> randMap; for (int i = 0; i < maxi; i++) { for (int j = 0; j < maxj; j++) { if (0 == arrMap[i][j]) { randMap.push_back(i*maxj + j); AllCnt++; } } } if (AllCnt < N) { N = AllCnt; } for (int i = 0; i < N; i++) { int temp = (rand() % AllCnt--); randMap.erase(randMap.begin() + temp); sFruit fruitTemp = sFruit(temp / maxi + 1, temp % (maxi - 2) + 1); gFruitVector.push_back(fruitTemp); } } //初始化地圖 void initArrMap(int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { for (int i = 0; i < MAP_MAXIMUM_HEIGHT; i++) { for (int j = 0; j < MAP_MAXIMUM_WIDTH; j++) { if (i == 0 || j == 0 || i == MAP_MAXIMUM_HEIGHT - 1 || j == MAP_MAXIMUM_WIDTH - 1) arrMap[i][j] = 1; else arrMap[i][j] = 0; } } } //初始化水果數據 void initFruit(int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { randomlyGeneratedFruit(MAX_NUMBER_FRUIT, arrMap); } //蛇頭吃到水果事件 void eatFruitEvent(int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { for (unsigned int i = 0; i < gFruitVector.size(); i++) { if (gFruitVector[i].isExit(gSnake)) { sSnakeBody snakeBodyTemp; if (gSnake.snakeBodyList->size() == 0) { snakeBodyTemp.setPostion(gSnake.nSnakeHeadX, gSnake.nSnakeHeadY); } else { snakeBodyTemp.setPostion(&gSnake.snakeBodyList->back()); } gSnake.snakeBodyList->push_back(snakeBodyTemp); gFruitVector.erase(gFruitVector.begin() + i); randomlyGeneratedFruit(1, arrMap); } } } int _tmain(int argc, _TCHAR* argv[]) { HANDLE hOutput; COORD coord = { 0, 0 }; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); //創建新的緩沖區 HANDLE hOutBuf = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL ); //設置新的緩沖區為活動顯示緩沖 SetConsoleActiveScreenBuffer(hOutBuf); //隱藏兩個緩沖區的光標 CONSOLE_CURSOR_INFO cci; cci.bVisible = 0; cci.dwSize = 1; SetConsoleCursorInfo(hOutput, &cci); SetConsoleCursorInfo(hOutBuf, &cci); //雙緩沖處理顯示 DWORD bytes = 0; char data[3200]; //地圖,需要將數據映射到地圖上,再去渲染地圖。 int arrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]; srand(time(NULL)); //計數器。 int nProgramCounter = 0; //初始化各個數據 initArrMap(arrMap); initFruit(arrMap); while (true) { nProgramCounter++; initArrMap(arrMap); bindKeyboardEvents(); updateSnake(nProgramCounter); eatFruitEvent(arrMap); bool bIsWin = mapLoadData(arrMap); showArrMap(arrMap); if (bIsWin) { cout << endl << "游戲失敗!!!"; ReadConsoleOutputCharacterA(hOutput, data, 3200, coord, &bytes); WriteConsoleOutputCharacterA(hOutBuf, data, 3200, coord, &bytes); break; } ReadConsoleOutputCharacterA(hOutput, data, 3200, coord, &bytes); WriteConsoleOutputCharacterA(hOutBuf, data, 3200, coord, &bytes); } system("pause"); return 0; }
以上是“C++如何實現簡易貪吃蛇游戲”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。