您好,登錄后才能下訂單哦!
小編給大家分享一下C++如何實現貪吃蛇游戲,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
#include<iostream> #include<cmath> #include<cstdlib> #include<cstdio> #include<ctime> #include<conio.h> #include<windows.h> using namespace std; /*光標定位*/ HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; void locate(int x, int y) { coord.X = y; coord.Y = x; SetConsoleCursorPosition(hout,coord); }; /*光標隱藏*/ void hide() { CONSOLE_CURSOR_INFO cursor_info = {1,0}; //bVisible=0;隱藏光標 SetConsoleCursorInfo(hout, &cursor_info);//獲取光標狀態 } /*生成隨機數*/ double random(double start, double end) { return start + (end - start)*rand()/(RAND_MAX+1.0);//生成一個數,大于等于start,小于end; } /*定義地圖的長和寬*/ int m, n; /*定義蛇 的長度,坐標,方向,食物的位置*/ struct node { int x, y; }snake[1000];//蛇的坐標 int snake_length, dir;//蛇的長度,方向 node food; int direct[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} };//食物的位置 /*輸出圍墻:一個矩形框*/ void print_wall() { //輸出第一行 “----------” cout << " "; for (int i = 1; i <= n; i++) { cout << "-"; } cout << endl; //輸出第一列“|”,中間輸入空格,最后一列輸出“|” for (int j = 0; j <= m-1; j++) { cout << "|"; for (int i = 1; i <= n; i++) cout << " "; cout << "|" << endl; } cout << " "; //輸出最后一行“----------” for (int i = 1; i <= n; i++) cout << "-"; } /*首次輸出蛇,其中snake[0]代表頭部*/ //蛇的外型:“@*****” void print_snake() { locate(snake[0].x,snake[0].y); cout << "@"; for (int i = 1; i < snake_length - 1; i++) { locate(snake[i].x, snake[i].y); cout << "*"; } } /*判斷是否撞墻或者頭部是否碰到身體的任意一個部位,碰到則游戲失敗*/ bool is_correct() { if (snake[0].x == 0 || snake[0].y == 0 || snake[0].x == m + 1 || snake[0].y == n + 1) return false;//頭部碰到邊緣 for (int i = 1; i <= snake_length - 1; i++) if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)return false;//頭部碰到身體的任意一個部位 return true; } /*隨機生成食物的位置*/ bool print_food() { srand((unsigned)time(0));//隨機種子 bool e; while (1) { e = true; int i = (int)random(0,m)+1; int j = (int)random(0,n)+1; food.x = i; food.y = j;//食物的位置隨機 for (int k = 0; k <= snake_length - 1; k++) //食物不能出現在蛇的身體的任意位置處 { if (snake[k].x == food.x && snake[k].y == food.y) { e= false; break; } } if (e)break; } //在食物的位置處標記,食物符號為“$”; locate(food.x,food.y); cout << "$"; return true; } /*蛇的前進*/ bool go_ahead() { node tmp; bool e = false; tmp = snake[snake_length-1];//蛇尾 for (int i = snake_length - 1; i >= 1;i--) { snake[i] = snake[i - 1];//后移一位 } snake[0].x += direct[dir][0]; snake[0].y += direct[dir][1]; locate(snake[1].x, snake[1].y);//定位到頭部的后一位 cout << "*"; /*吃到食物*/ if (snake[0].x == food.x&&snake[0].y == food.y) { snake_length++; e = true; snake[snake_length - 1] = tmp; } /*輸出此時蛇狀態*/ if (!e) { locate(tmp.x, tmp.y); cout << " "; } else print_food(); locate(snake[0].x, snake[0].y); cout << "@"; /*** 如果自撞 ***/ if (!is_correct()) { system("cls"); cout << "You lose!" << endl << "Length: " << snake_length << endl; return false; } return true; } int main() { //游戲提示: cout << "--------------------貪吃蛇---------------------" << endl; cout << "請先輸入兩個數,表示地圖大小.要求長寬均不小于10." << endl; cout << "請注意窗口大小,以免發生錯位.建議將窗口調為最大." << endl; cout << "再選擇難度.請在1-10中輸入1個數,1最簡單,10則最難" << endl; cout << "然后進入游戲畫面,以方向鍵控制方向.祝你游戲愉快!" << endl; cout << "-----------------------------------------------" << endl; cin >> m >> n; if (m < 10 || n < 10 || m>25 || n>40) { cout << "ERROR" << endl; system("pause"); return 0; } //輸入難度系數:1-10; int hard; cin >> hard; if (hard <= 0 || hard > 100) { cout << "ERROR" << endl; system("pause"); return 0; } //數據初始化:蛇的位置,長度,方向 snake_length = 5;//長度 clock_t a, b; char ch; double hard_len; for (int i = 0; i <= 4; i++)//位置 { snake[i].x = 1; snake[i].y = 5 - i; } dir = 3;//方向 //輸出原始地圖、食物和蛇 system("cls"); hide(); print_wall(); print_food(); print_snake(); //在(0,m+2)出顯示長度: locate(m + 2, 0); cout << "Now Length:"; //開始游戲 while (1) { /*難度隨長度的增加而提高*/ hard_len = (double)snake_length / (double)(m*n); /*調節時間,單位是ms*/ a = clock(); while (1) { b = clock(); if (b - a >= (int)(400 - 30 * hard)*(1 - sqrt(hard_len)))break; } //接收鍵盤輸入的方向 //https://blog.csdn.net/wenweimin/article/details/105561 if (_kbhit()) { ch = _getch(); if (ch == -32) { ch = _getch(); switch (ch) { case 72:if (dir == 2 || dir == 3)dir = 0; break; case 80:if (dir == 2 || dir == 3)dir = 1; break; case 75:if (dir == 0 || dir == 1)dir = 2; break; case 77:if (dir == 0 || dir == 1)dir = 3; break; } } } //前進 if (!go_ahead())break; //輸出此時的長度 locate(m + 2, 12); cout << snake_length; } system("pause"); return 0; }
看完了這篇文章,相信你對C++如何實現貪吃蛇游戲有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。