服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 使用C++实现迷宫游戏

使用C++实现迷宫游戏

2021-08-27 14:51Viitasca C/C++

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

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

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

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

?
1
2
3
4
struct point
{
 int x, y;
};

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

?
1
2
3
4
5
6
7
8
9
10
11
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,在地图上表明玩家位置和重点位置,刷新屏幕,没了。然后再把能定义为常量的都定位常量,修改一下细节,就能得到一个简陋的走迷宫游戏了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#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

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

原文链接:https://blog.csdn.net/qq_18352005/article/details/79397390

延伸 · 阅读

精彩推荐