C++實現貪吃蛇遊戲的詳細步驟及實戰示範

來源:互聯網
上載者:User
學習C++過程中做的一個小程式,比較簡單,只實現了貪吃蛇小遊戲最基本的功能。上傳代碼作為學習的記錄,同時也為其他同學提供作為一個小小的參考。實現環境為:Microsoft visual C++6.0整合式開發環境 。

#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 << " ";}elseprint_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/105561if (_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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.