C++實現簡單迷宮遊戲
本文實例為大傢分享瞭C++實現簡單迷宮遊戲的具體代碼,供大傢參考,具體內容如下
問題描述
程序開始運行時顯示一個迷宮地圖,迷宮中央有一隻老鼠,迷宮的右下方有一個糧倉。遊戲的任務是使用鍵盤上的方向健操縱老鼠在規定的時間內走到糧倉處。
基本要求
(1)老鼠形象可以辨認,可用鍵盤操縱老鼠上下左右移動;
(2)迷宮的墻足夠結實,老鼠不能穿墻而過;
(3)正確檢測結果,若老鼠在規定時間內走到糧倉處,提示 成功,並給出一條路徑,否則提示失敗;
(4)添加編輯迷宮功能,可修改當前迷宮,修改內容:墻變路、路變墻;
提高要求
(1)增加闖關和計分功能;
(2)找出走出迷宮的所有路徑及最短路徑。
源代碼
1.文件main.cpp
#include<iostream> #include"maze.h"//自定義的頭文件,存放類聲明和常量 #include<cstdlib>//各種函數頭文件,如rand函數 #include<time.h>//日期和時間的頭文件 #include<stdio.h>//標準輸入輸出 #include<Windows.h>//圖形界面設計頭文件 #include<conio.h>//獲取方向鍵的頭文件 using namespace std; void MainMenu();//顯示遊戲菜單函數 void MeunGame(int num);//顯示不同關卡函數 void Introduce();//遊戲介紹 int main() { Maze maze_1(11, 11), maze_2(13, 13), maze_3(15, 15), maze_4(17, 17);//定義四個關卡 int i = 0, choice = 0, choice_2, choice_3; char isexit = ' ', choice_1 = ' '; bool Go_on = false, judge[4] = { false }; cout << "\n\t歡迎試玩憨憨老鼠走迷宮遊戲"; for (i = 0;i < 4;i++) { Sleep(500); cout << "·"; } system("cls"); Introduce();//遊戲介紹 system("cls"); MainMenu();//顯示遊戲菜單 cout << "\n\t請輸入選擇:(1~4)"; cin >> choice; while (1) { switch (choice) { case 1: for (i = 1;i <= 4;i++) { if (i == 1) { MeunGame(i);//顯示關卡菜單 system("cls"); maze_1.Show_Map(); judge[0] = maze_1.Move(); if (judge[0] == true) { cout << "憨憨鼠所走過的路徑"; maze_1.KeepMap(); cout << "是否繼續闖關:(Y|N)" << endl; cin >> choice_1; if (choice_1 == 'y' || choice_1 == 'Y') { Go_on = true; } else if (choice_1 == 'n' || choice_1 == 'N') { Go_on = false; } } else { system("pause"); break; } } else if (i == 2) { MeunGame(i); system("cls"); maze_2.Show_Map(); judge[1] = maze_2.Move(); if (judge[1] == true) { cout << "憨憨鼠所走過的路徑"; maze_2.KeepMap(); cout << "是否繼續闖關:(Y|N)" << endl; cin >> choice_1; if (choice_1 == 'y' || choice_1 == 'Y') { Go_on = true; } else if (choice_1 == 'n' || choice_1 == 'N') { Go_on = false; } } else { system("pause"); break; } } else if (i == 3) { MeunGame(i); system("cls"); maze_3.Show_Map(); judge[2] = maze_3.Move(); if (judge[2] == true) { cout << "憨憨鼠所走過的路徑"; maze_3.KeepMap(); cout << "是否繼續闖關:(Y|N)" << endl; cin >> choice_1; if (choice_1 == 'y' || choice_1 == 'Y') { Go_on = true; } else if (choice_1 == 'n' || choice_1 == 'N') { Go_on = false; } } else { system("pause"); break; } } else if (i == 4) { MeunGame(i); system("cls"); maze_4.Show_Map(); judge[3] = maze_4.Move(); if (judge[3] == true) { cout << "憨憨鼠所走過的路徑"; maze_4.KeepMap(); system("pause"); system("cls"); cout << "\t★太棒瞭,恭喜你通過全部關卡★" << endl; cout << "\t 是否退出遊戲?(Y|N)" << endl; cin >> choice_1; if (choice_1 == 'y' || choice_1 == 'Y') { return 0; } else if (choice_1 == 'n' || choice_1 == 'N') { Go_on = false; } } else { system("pause"); break; } } if (Go_on == false) { break; } } break; case 2: system("cls"); cout << "請你輸入要編輯的關卡:" << endl; cout << "1.第一關" << endl; cout << "2.第二關" << endl; cout << "3.第三關" << endl; cout << "4.第四關" << endl; cin >> choice_2; if (choice_2 == 1 && judge[0] == true) { maze_1.EdidMap(); } else if (choice_2 == 2 && judge[1] == true) { maze_2.EdidMap(); } else if (choice_2 == 3 && judge[2] == true) { maze_3.EdidMap(); } else if (choice_2 == 4 && judge[3] == true) { maze_4.EdidMap(); } else { cout << "此關卡未通過,不能編譯此關!" << endl; system("pause"); } break; case 3: system("cls"); cout << "請你輸入要查看的關卡" << endl; cout << "1.第一關" << endl; cout << "2.第二關" << endl; cout << "3.第三關" << endl; cout << "4.第四關" << endl; cin >> choice_3; if (choice_3 == 1 && judge[0] == true) { maze_1.Short(); } else if (choice_3 == 2 && judge[1] == true) { maze_2.Short(); } else if (choice_3 == 3 && judge[2] == true) { maze_3.Short(); } else if (choice_3 == 4 && judge[3] == true) { maze_4.Short(); } else { cout << "此關卡未通過,不能查看此關!" << endl; system("pause"); } break; case 4: system("cls"); cout << "\n\n\t\t是否確定退出遊戲?(Y|N)"; cin >> isexit; if (isexit == 'Y' || isexit == 'y') { return 0; } else if (isexit == 'N' || isexit == 'n') { break; } default: cout << "\n\t輸入選擇選擇無效,請從新輸入:"; Sleep(500); break; } system("cls"); MainMenu(); cout << "\n\t請輸入輸入選擇:(1~4)"; cin >> choice; } } void MainMenu()//遊戲菜單函數 { cout << "\n\t\t**************************************************************" << endl; cout << "\t\t* *" << endl; cout << "\t\t* *老鼠走迷宮遊戲* *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 1.開始遊戲 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 2.編輯地圖 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 3.破解地圖 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 4.退出遊戲 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* *" << endl; cout << "\t\t**************************************************************" << endl; } void MeunGame(int num)//關卡菜單 { system("cls"); cout << "\n\t\t**************************************************************" << endl; cout << "\t\t* *" << endl; cout << "\t\t* ◤ 第" << num << "關 ◢ *" << endl; cout << "\t\t* *" << endl; cout << "\t\t**************************************************************" << endl; system("pause"); } void Introduce()//遊戲介紹 { cout << "\n\t\t********************************************************************" << endl; cout << "\t\t* *" << endl; cout << "\t\t* *老鼠走迷宮遊戲介紹* *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 1.玩傢可通過方向鍵↑↓←→控制老鼠移動 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 2.在選擇編輯地圖時,玩傢通過WASD編輯 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 3.在選擇破解地圖時,會給出坐標路線,原點為迷宮左上角 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t* 4.在規定時間內走到糧倉算過關,時間越短所獲積分越多 *" << endl; cout << "\t\t* *" << endl; cout << "\t\t********************************************************************" << endl; system("pause"); }
2.maze.cpp文件
#include<iostream> #include"maze.h"//自定義的頭文件,存放類聲明和常量 #include<cstdlib>//各種函數頭文件,如rand函數 #include<time.h>//日期和時間的頭文件 #include<stdio.h>//標準輸入輸出 #include<Windows.h>//圖形界面設計頭文件 #include<conio.h>//獲取方向鍵的頭文件 using namespace std; Maze::Maze(int l, int w)//構造函數初始化地圖和成員變量 { int i, j; Map_Length = l, Map_Width = w; for (i = 0;i <= Map_Length;i++) { for (j = 0;j <= Map_Width;j++) { if (i == 0 || j == 0) { Map[i][j] = road; } else { Map[i][j] = wall;//默認地圖中都是墻 } } } for (i = 0;i < Map_Length;i++) { for (j = 0;j < Map_Width;j++) { Visited[i][j] = 0; } } front = rear = -1; top = -1; } void Maze::CreateMap(int x, int y)//創建地圖 { int Direction[4][2] = { {1,0}, {0,1}, {0,-1}, {-1,0} };//定義四個方向 int i, j, temp; for (i = 0;i < 4;i++)//打亂四個方向 { j = rand() % 4; temp = Direction[i][0]; Direction[i][0] = Direction[j][0]; Direction[j][0] = temp; temp = Direction[i][1]; Direction[i][1] = Direction[j][1]; Direction[j][1] = temp; } Map[x][y] = road;//選取[x][y]為路 for (i = 0;i < 4;i++) { if (Map[x + 2 * Direction[i][0]][y + 2 * Direction[i][1]] == wall)//任意兩點之間有路 { Map[x + Direction[i][0]][y + Direction[i][1]] = road; CreateMap(x + 2 * Direction[i][0], y + 2 * Direction[i][1]); } } } void Maze::Show_Map()//顯示地圖 { //srand((unsigned)time(NULL));//種隨機粒子 CreateMap(2 * (rand() % (Map_Length / 2 + 1)), 2 * (rand() % (Map_Width / 2 + 1)));//隨機選取x,y坐標 Map[Map_Length / 2][Map_Width / 2] = Mouse;//定義老鼠的位置 Map[Map_Length - 1][Map_Width - 1] = End;//定義糧倉的位置 Display(); } void Maze::Display()//查看地圖 { int i, j; for (i = 0;i <= Map_Length;i++) { for (j = 0;j <= Map_Width;j++) { if (Map[i][j] == road) { cout << " "; } else if (Map[i][j] == wall) { cout << "■"; } else if (Map[i][j] == Mouse) { cout << "♂"; } else if (Map[i][j] == End) { cout << "★"; } else if (Map[i][j] == visited) { cout << " "; } } cout << endl; } } void Maze::KeepMap()//顯示老鼠走過的路徑 { int i, j; for (i = 0;i <= Map_Length;i++) { for (j = 0;j <= Map_Width;j++) { if (Map[i][j] == road) { cout << " "; } else if (Map[i][j] == wall) { cout << "■"; } else if (Map[i][j] == Mouse) { cout << "♂"; } else if (Map[i][j] == End) { cout << "★"; } else if (Map[i][j] == visited) { cout << "×"; } } cout << endl; } } bool Maze::Move()//老鼠移動 { int count = 30, m = 0, n = 0; bool temp = false; char Enter = ' '; int t = time(NULL);//獲取系統時間 pos_x = Map_Length / 2, pos_y = Map_Width / 2;//老鼠的初始位置 while (count >= 0) { if (_kbhit() == 0) { if (t != time(NULL)) { system("cls"); Display(); count--; cout << "|---剩餘時間:" << count << "---|"; if (count == 0) { system("cls"); cout << "闖關失敗" << endl; temp = false; } t = time(NULL);//獲取當前時間 } } if (_kbhit() != 0) { system("cls"); Enter = _getch(); if (Enter == -32)//*****鍵盤事件***** { switch (_getch()) { case 72://up Up(); Display(); break; case 80://down Dowm(); Display(); break; case 75://left Left(); Display(); break; case 77://right Right(); Display(); break; default: break; } } if (Map[Map_Length - 1][Map_Width - 1] != End) { system("cls"); cout << "★恭喜你闖關成功★" << endl; Map[pos_x][pos_y] = visited; cout << "★獲得" << count << "點積分★" << endl; temp = true; break; } } } return temp; } void Maze::Up()//老鼠向上移動 { if (pos_y <= 0) { return; } else if (Map[pos_x - 1][pos_y] != wall) { Map[pos_x][pos_y] = visited; pos_x--; Map[pos_x][pos_y] = Mouse; } } void Maze::Dowm()//老鼠向下移動 { if (pos_y > Map_Width - 1) { return; } else if (Map[pos_x + 1][pos_y] != wall) { Map[pos_x][pos_y] = visited; pos_x++; Map[pos_x][pos_y] = Mouse; } } void Maze::Left()//老鼠向左移動 { if (pos_x <= 0) { return; } else if (Map[pos_x][pos_y - 1] != wall) { Map[pos_x][pos_y] = visited; pos_y--; Map[pos_x][pos_y] = Mouse; } } void Maze::Right()//老鼠向右移動 { if (pos_x > Map_Width - 1) { return; } else if (Map[pos_x][pos_y + 1] != wall) { Map[pos_x][pos_y] = visited; pos_y++; Map[pos_x][pos_y] = Mouse; } } void Maze::EdidMap()//編輯地圖 { system("cls"); char Enter = ' '; bool isKeep = false; pos_x = Map_Length / 2, pos_y = Map_Width / 2;//確定老鼠坐標 Map[pos_x][pos_y] = Mouse; while (1) { Display(); Enter = _getch(); if (Enter == -32)//*****鍵盤事件***** { switch (_getch()) { case 72://up Up(); break; case 80://down Dowm(); break; case 75://left Left(); break; case 77://right Right(); break; default: break; } } switch (Enter) { case 119://W鍵 if (Map[pos_x - 1][pos_y] == wall) { Map[pos_x - 1][pos_y] = road; } else if (Map[pos_x - 1][pos_y] == road || Map[pos_x - 1][pos_y] == visited) { Map[pos_x - 1][pos_y] = wall; } break; case 97://A鍵 if (Map[pos_x][pos_y - 1] == wall) { Map[pos_x][pos_y - 1] = road; } else if (Map[pos_x][pos_y - 1] == road || Map[pos_x][pos_y - 1] == visited) { Map[pos_x][pos_y - 1] = wall; } break; case 115://S鍵 if (Map[pos_x + 1][pos_y] == wall) { Map[pos_x + 1][pos_y] = road; } else if (Map[pos_x + 1][pos_y] == road || Map[pos_x + 1][pos_y] == visited) { Map[pos_x + 1][pos_y] = wall; } break; case 100://D鍵 if (Map[pos_x][pos_y + 1] == wall) { Map[pos_x][pos_y + 1] = road; } else if (Map[pos_x][pos_y + 1] == road || Map[pos_x][pos_y + 1] == visited) { Map[pos_x][pos_y + 1] = wall; } break; case 0x0D://回車 system("cls"); Map[pos_x][pos_y] = road; cout << "*****保存成功*****" << endl; isKeep = true; system("pause"); break; default: break; } if (isKeep == true) { for (int i = 0;i < Map_Length;i++) { for (int j = 0;j < Map_Width;j++) { if (Map[i][j] == visited) { Map[i][j] = road; } } } break; } system("cls"); } } void Maze::Short() { rear = front = -1; int i, j; for (i = 1;i <= Map_Length;i++) { for (j = 1;j <= Map_Width;j++) { if (Map[i][j] == visited) { Map[i][j] = road;//被訪問的變成路 } } } for (i = 0;i <= Map_Length;i++) { for (j = 0;j <= Map_Width;j++) { Visited[i][j] = 0; } } Show_Map();//顯示地圖 system("cls"); int m = Map_Length - 1, n = Map_Width - 1; SmallRoadDisplay(m, n);//找最短路徑 while (top != -1) { top--; } } void Maze::SmallRoadDisplay(int x, int y)//最短路徑 { bool flag = false; Visited[x - 1][y - 1] = 1; Map[x][y] = End; int i = 0, j = 0, k = 0; int Direction[4][2] = { {1,0}, {0,1}, {0,-1}, {-1,0} };//定義四個方向 int arr[100] = { 0 };//存放x坐標的隊列 int brr[100] = { 0 };//存放y坐標的隊列 int record_x[100] = { 0 };//存放x坐標的棧 int record_y[100] = { 0 };//存放y坐標的棧 front = rear = -1; rear++; arr[rear] = x;//當前x坐標入隊 brr[rear] = y;//當前y坐標入隊 while (front != rear) { front++; for (i = 0;i < 4;i++) { if ((Map[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == road || Map[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == Mouse || Map[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == visited) && Visited[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == 0 && arr[front] < Map_Width && arr[front] >= 1 && brr[front] < Map_Length && brr[front] >= 1) { rear++; arr[rear] = arr[front] + Direction[i][0]; brr[rear] = brr[front] + Direction[i][1]; Visited[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] = 1; if (arr[rear] == (Map_Length / 2) && brr[rear] == (Map_Width / 2)) { flag = true; break; } } } if (flag == true) { break; } } front = rear + 1; rear = 0; top = -1; top++; record_x[top] = arr[front - 1]; record_y[top] = brr[front - 1]; while (rear != front) { front--; for (j = 0;j < 4;j++) { if (record_x[top] + Direction[j][0] == arr[front - 1] && record_y[top] + Direction[j][1] == brr[front - 1]) { top++; record_x[top] = arr[front - 1]; record_y[top] = brr[front - 1]; } } } Display(); cout << "最短路徑如下:" << endl; cout << "鼠" << "->"; for (i = 0;i <= top;i++) { cout << "(" << record_x[i] << "," << record_y[i] << ")" << "->"; } cout << "倉" << endl; system("pause"); }
3.maze.h文件
#ifndef MAZE_H #define MAZE_H const int MaxSize = 100; const int road = 0;//路 const int wall = 1;//墻 const int Mouse = 2;//老鼠 const int End = 3;//終點 const int visited = 4;//被訪問的路徑 const int MaxSmall = 5;//最短路徑 class Maze { private: int pos_x, pos_y;//主角老鼠的坐標 int Map_Length, Map_Width;//地圖的長寬 int Visited[MaxSize][MaxSize];//是否被訪問數組 int rear, front; int top; public: Maze(int l, int w);//構造函數 int Map[MaxSize][MaxSize];//地圖數組 void CreateMap(int, int);//創建地圖 void Show_Map();//顯示地圖 void Display();//查看地圖 void KeepMap();//顯示老鼠走過的路徑 bool Move();//老鼠移動 void Up();//老鼠向上移動 void Dowm();//老鼠向下移動 void Right();//老鼠向右移動 void Left();//老鼠向左移動 void EdidMap();//編輯地圖 void Short(); void SmallRoadDisplay(int x, int y);//最短路徑 }; #endif #pragma once
版本提示
所用開發版本為vs2019版,版本不同編譯可能存在錯誤。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。