迷宮游戲就是玩家在地圖中移動,移動至終點則游戲結(jié)束。
瓊山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
自己用文本文檔手打了個小地圖,0表示空白,1表示墻,文件名隨意,我改成了map.MapData。然后程序里定義一個全局變量char Map[MapLenX][MapLenY];(長寬自定義)行為X,列為Y。定義char型常量RoadSymbol = '0', WallSymbol = '1', PlayerSymbol = '+'。
本游戲為面向?qū)ο缶帉懙?,所以就要設計一個類。數(shù)據(jù)需要一個坐標和一個bool型儲存是否到達終點。所以自定義了個結(jié)構(gòu)體儲存坐標
struct point { int x, y; };
還需要構(gòu)造函數(shù),析構(gòu)函數(shù),然后寫個移動的函數(shù)PlayerMove(),再寫個判斷是否到達終點的函數(shù)CheckIfWin()。每走完一步就要刷新屏幕,所以還需要寫個函數(shù)Refresh(),然后PlayerActor類就完成了。
class PlayerActor { public: point m_Location; bool m_IfWin; PlayerActor(); ~PlayerActor(); void PlayerMove(int _Direc); void Refresh(void); void CheckIfWin(void); };
構(gòu)造函數(shù)析構(gòu)函數(shù)先不著急, 先定義一下PlayerMove()。思路是先判斷是否可移動。若能,當前位置的地圖標記設為RoadSymbol, 移動即更新坐標,新坐標位置在地圖上標記為PlayerSymbol, 刷新畫面,判斷輸贏。Refresh()思路為先用system("cls")清屏,然后逐行打印。若地圖上某點為RoadSymbol輸出空格, WallSymbol輸出'*', PlayerSymbol輸出'+'。
接下來定義玩家起始位置和終點PlayerStart和PlayerEnd并初始化。main函數(shù)大體流程如下:讀入地圖,實例化PlayerActor,當!m_IfWin時接收鍵盤按鍵來移動,當m_IfWIn時彈出提示框并結(jié)束。所以還需要一個全局函數(shù)PlayerControl來接收按鍵并調(diào)用PlayerMove()。
至此,構(gòu)造函數(shù)的流程也明確了。初始化m_IfWin和m_Location,在地圖上表明玩家位置和重點位置,刷新屏幕,沒了。然后再把能定義為常量的都定位常量,修改一下細節(jié),就能得到一個簡陋的走迷宮游戲了。
#include#include #include"conio.h" #include"windows.h" ///////////////////////// struct point { int x, y; }; /////////////////////// const point PlayerStart = {10, 2}; const point PlayerEnd = {2, 10}; const int MapLenX = 11, MapLenY = 10; const char EndSymbol = '#', PlayerSymbol = '+', WallSymbol = '1', RoadSymbol = '0'; char Map[MapLenX][MapLenY]; const int MoveX[4] = {-1, 1, 0, 0}, MoveY[4] = {0, 0, -1, 1}; //// UP, DOWN, LEFT, RIGHT const int _UP = 0, _DOWN = 1, _LEFT = 2, _RIGHT = 3; ///////// CLASS /////////////// class PlayerActor { public: point m_Location; bool m_IfWin; PlayerActor(); ~PlayerActor(); void PlayerMove(int _Direc); void Refresh(void); void CheckIfWin(void); }; /////////// MEMBER FUNCTIONS ///////////// PlayerActor::PlayerActor() { m_IfWin = false; this-> m_Location.x = PlayerStart.x; this-> m_Location.y = PlayerStart.y; Map[this-> m_Location.x][this-> m_Location.y] = PlayerSymbol; Map[PlayerEnd.x][PlayerEnd.y] = EndSymbol; PlayerActor::Refresh(); } PlayerActor::~PlayerActor() { } void PlayerActor::PlayerMove(int _Direct) { if ( Map[this-> m_Location.x+MoveX[_Direct]][this-> m_Location.y+MoveY[_Direct]] == RoadSymbol || Map[this-> m_Location.x+MoveX[_Direct]][this-> m_Location.y+MoveY[_Direct]] == EndSymbol )/////// JUDGE IF CAN MOVE { Map[this-> m_Location.x][this-> m_Location.y] = RoadSymbol; this-> m_Location.x += MoveX[_Direct]; this-> m_Location.y += MoveY[_Direct]; Map[this-> m_Location.x][this-> m_Location.y] = PlayerSymbol; PlayerActor::Refresh(); PlayerActor::CheckIfWin(); } } void PlayerActor::Refresh(void) { system("cls"); //////CLEAR SCREEN for (int i=1; i<=MapLenX; i++) { for (int j=1; j<=MapLenY; j++) { if (Map[i][j] == RoadSymbol) printf(" "); else if (Map[i][j] == WallSymbol) printf("* "); else if (Map[i][j] == '+') printf("%c ", PlayerSymbol); else if (Map[i][j] == EndSymbol) printf("%c ",EndSymbol); } printf("\n"); } } void PlayerActor::CheckIfWin(void) { if (this-> m_Location.x == PlayerEnd.x && this-> m_Location.y == PlayerEnd.y) m_IfWin = true; } ///////////// GLOBAL FUNCTION //////////////// void PlayerControl(PlayerActor* Player, int _KEY) { switch (_KEY) { case 119 : Player->PlayerMove(_UP); //// w 119 break; case 115 : Player->PlayerMove(_DOWN); ///////s 115 break; case 97 : Player->PlayerMove(_LEFT); //// a 97 break; case 100 : Player->PlayerMove(_RIGHT); //// d 100 break; default: break; } } //////// MAIN FUNCTION /////////// int main() { ///////// READ MAP ///////////// freopen("map.MapData", "r", stdin); for (int i=1; i<=MapLenX; i++) { for (int j=1; j<=MapLenY; j++) { std::cin >> Map[i][j]; } } //// CREATE PLAYERACTOR //// PlayerActor* Player = new PlayerActor; while (!Player->m_IfWin) { PlayerControl(Player, _getch()); } system("cls"); MessageBox(NULL, "You Win!", "Congratulations!", MB_OK); delete Player; return 0; }
地圖map.MapData:
1111111111
1000000001
1011111111
1010000001
1011111101
1000000101
1111110101
1000010101
1011110101
1000000001
1111111111
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。