温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用C++实现迷宫游戏

发布时间:2020-09-28 19:43:11 来源:脚本之家 阅读:172 作者:Viitasca 栏目:编程语言

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

自己用文本文档手打了个小地图,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<iostream>
#include<cstdio>
#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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI