使用C++实现迷宫游戏

这篇文章主要为大家详细介绍了C++实现迷宫游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

迷宫游戏就是玩家在地图中移动,移动至终点则游戏结束。

自己用文本文档手打了个小地图,0表示空白,1表示墙,文件名随意,我改成了map.MapData。然后程序里定义一个全局变量char Map[MapLenX][MapLenY];(长宽自定义)行为X,列为Y。定义char型常量RoadSymbol = '0', WallSymbol = '1', PlayerSymbol = '+'。

本游戏为面向对象编写的,所以就要设计一个类。数据需要一个坐标和一个bool型储存是否到达终点。所以自定义了个结构体储存坐标

 struct point { int x, y; };

还需要构造函数,析构函数,然后写个移动的函数PlayerMove(),再写个判断是否到达终点的函数CheckIfWin()。每走完一步就要刷新屏幕,所以还需要写个函数Refresh(),然后PlayerActor类就完成了。

 class PlayerActor { public: point m_Location; bool m_IfWin; PlayerActor(); ~PlayerActor(); void PlayerMove(int _Direc); void Refresh(void); void CheckIfWin(void); };

构造函数析构函数先不着急, 先定义一下PlayerMove()。思路是先判断是否可移动。若能,当前位置的地图标记设为RoadSymbol, 移动即更新坐标,新坐标位置在地图上标记为PlayerSymbol, 刷新画面,判断输赢。Refresh()思路为先用system("cls")清屏,然后逐行打印。若地图上某点为RoadSymbol输出空格, WallSymbol输出'*', PlayerSymbol输出'+'。

接下来定义玩家起始位置和终点PlayerStart和PlayerEnd并初始化。main函数大体流程如下:读入地图,实例化PlayerActor,当!m_IfWin时接收键盘按键来移动,当m_IfWIn时弹出提示框并结束。所以还需要一个全局函数PlayerControl来接收按键并调用PlayerMove()。

至此,构造函数的流程也明确了。初始化m_IfWin和m_Location,在地图上表明玩家位置和重点位置,刷新屏幕,没了。然后再把能定义为常量的都定位常量,修改一下细节,就能得到一个简陋的走迷宫游戏了。

 #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

以上就是使用C++实现迷宫游戏的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言