我是靠谱客的博主 执着铃铛,这篇文章主要介绍C++实现扫雷、排雷小游戏,现在分享给大家,希望可以做个参考。

本文实例为大家分享了C++实现扫雷、排雷小游戏的具体代码,供大家参考,具体内容如下

界面:

游戏思想: 

扫雷游戏:

1.随机给定雷点坐标

2.判断每个点的雷情况

3.由用户根据上下左右键到达指定位置,点击enter,翻开该点
        如果该点是雷点,此时翻开所有雷点,告知游戏结束
        非雷点,翻开该点坐标

代码:

复制代码
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include<iostream> #include<vector> #include<sstream> #include<algorithm> #include<graphics.h> #include<ctime> #include<conio.h> using namespace std; #define WIDTH 500 #define HEIGHT 500 #define SIDE 50 #define THUNDERNUM 10 #define TOTALSCORE 100 #define EVERY_OF_DESC 5 int score = TOTALSCORE; class Point { public: int x; int y; Point() {} Point(int _x, int _y) :x(_x), y(_y) {} void setPoint(int x, int y) { this->x = x; this->y = y; } Point(const Point& xy) :x(xy.x), y(xy.y) {} bool operator<(const Point& xy)const { if (x <= xy.x) return true; return y <= xy.y; } Point& operator=(const Point& xy) { if (this != &xy) { x = xy.x; y = xy.y; } return *this; } bool operator==(const Point& xy) { return ((x == xy.x) && (y = xy.y)); } bool operator!=(const Point& xy) { return !(*this == xy); } }; class ThunderPoint { private: vector<Point> storage; int num; public: ThunderPoint(int _num = THUNDERNUM) :num(_num) { //初始化雷点位置 int count = 0; while (count != num) { int x = (rand() % (WIDTH / SIDE)) * SIDE;//随机生成数据,使之在图形界面之中 int y = (rand() % (HEIGHT / SIDE)) * SIDE; Point tem(x, y); int i = -1; bool flag = false; for (i = 0; i < storage.size(); i++) { if (tem == storage[i]) { flag = true; break; } } if (flag==false) { //说明没有重复 storage.push_back(tem); count++; } } } vector<Point>& getThunderPoint() { //获得雷点位置 return storage; } void drawThunderPoint() { auto it = storage.begin(); while (it != storage.end()) { setfillcolor(RED); fillcircle((*it).x, (*it).y, SIDE / 2); ++it; } } }; class Grid { private: vector<vector<int>> nearbythunder; vector<vector<bool>> isopen; ThunderPoint thunder; Point currposition; Point preposition; vector<COLORREF> color; public: bool isOver; public: Grid() :thunder(10),currposition() { preposition.setPoint(0, 0); for (int i = 0; i < 10; ++i) { color.push_back(RGB(rand() % 256, rand() % 256, rand() % 256)); } isOver = false; isopen.resize(HEIGHT / SIDE, vector<bool>(WIDTH / SIDE, false)); nearbythunder.resize(HEIGHT/SIDE,vector<int>(WIDTH/SIDE,0)); currposition.setPoint(0, 0); //先将雷点的位置标出来,标为数字 9 // 任何一个点,他附近的雷点最多8个 auto it = thunder.getThunderPoint().begin(); while (it != thunder.getThunderPoint().end()) { int x = ((*it).x)/SIDE; int y = ((*it).y)/SIDE; nearbythunder[x][y] = 9; if (((y - SIDE/SIDE) >= 0) && (nearbythunder[x][y - SIDE/SIDE] != 9)) { nearbythunder[x][y - SIDE/SIDE]++; } if (((y - SIDE/SIDE) >= 0) && ((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y - SIDE/SIDE] != 9)) { nearbythunder[x - SIDE/SIDE][y - SIDE/SIDE]++; } if (((y - SIDE/SIDE) >= 0) && ((x + SIDE/SIDE) < WIDTH/SIDE) && (nearbythunder[x + SIDE/SIDE][y - SIDE/SIDE] != 9)) { nearbythunder[x + SIDE/SIDE][y - SIDE/SIDE]++; } if (((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y] != 9)) { nearbythunder[x - SIDE/SIDE][y]++; } if (((x + SIDE/SIDE) < WIDTH/SIDE) && (nearbythunder[x + SIDE/SIDE][y] != 9)) { nearbythunder[x + SIDE/SIDE][y]++; } if (((y + SIDE/SIDE) < HEIGHT/SIDE) && (nearbythunder[x][y + SIDE/SIDE] != 9)) { nearbythunder[x][y + SIDE/SIDE]++; } if (((y + SIDE/SIDE) < HEIGHT/SIDE) && ((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE] != 9)) { nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE]++; } if (((y + SIDE / SIDE) < HEIGHT / SIDE) && ((x + SIDE / SIDE) < WIDTH / SIDE) && (nearbythunder[x + SIDE / SIDE][y + SIDE / SIDE] != 9)) { nearbythunder[x + SIDE / SIDE][y + SIDE / SIDE]++; } ++it; } for (int i = 0; i < HEIGHT; i = i + SIDE) { setlinecolor(YELLOW); line(0, i, WIDTH, i); line(i, 0, i, HEIGHT); } } void keyDown() { char userKey = _getch(); if (userKey == -32) // 表明这是方向键 userKey = -_getch(); // 获取具体方向,并避免与其他字母的 ASCII 冲突 setfillcolor(GREEN); preposition = currposition; switch (userKey) { case 'w': case 'W': case -72: //上 { if (currposition.y - SIDE >= 0) { currposition.y -= SIDE; } } break; case 's': case 'S': case -80://下 { if (currposition.y + SIDE < HEIGHT) currposition.y += SIDE; } break; case 'a': case 'A': case -75://左 { if (currposition.x - SIDE >= 0) currposition.x -= SIDE; } break; case 'd': case 'D': case -77://右 { if (currposition.x + SIDE < WIDTH) { currposition.x += SIDE; } } break; case 'r': { score -= EVERY_OF_DESC; settextstyle(10, 10, "楷体"); settextcolor(GREEN); RECT rect; rect.left = WIDTH-100; rect.top = 100; rect.right = WIDTH; rect.bottom = 200; string ll = to_string(score); const char* str = ll.c_str(); //drawtext(str, &rect, 1); //现在开始翻 openOne(currposition); } break; } setlinecolor(BLACK); setlinestyle(1, 0); line(preposition.x, preposition.y, preposition.x + SIDE, preposition.y); setfillcolor(GREEN); setlinecolor(WHITE); setlinestyle(1, 2); line(currposition.x, currposition.y, currposition.x + SIDE, currposition.y ); } private: bool pred(bool x) { return x == true; } void openOne(const Point& cur) { //说明此时翻一个 isopen[cur.x / SIDE][cur.y / SIDE] = true; //此时应该着色 settextcolor(color[nearbythunder[cur.x/SIDE][cur.y/SIDE]]); RECT rect; settextstyle(SIDE, SIDE, "楷体"); rect.left = cur.x; rect.top = cur.y; rect.right = cur.x + SIDE; rect.bottom = cur.y + SIDE; drawtext((to_string(nearbythunder[cur.x / SIDE][cur.y / SIDE])).c_str(), &rect, 1); int count = 0; for (int i = 0; i < HEIGHT / SIDE; ++i) { for (int j = 0; j < WIDTH / SIDE; ++j) { if (isopen[i][j] == true) { count++; } } } cout << count << endl; if (nearbythunder[cur.x / SIDE][cur.y / SIDE] == 9 || count==((WIDTH/SIDE)*(HEIGHT/SIDE)- THUNDERNUM)) { //说明游戏结束 setfillcolor(RED); for (int i = 0; i < thunder.getThunderPoint().size(); i++) { fillcircle((thunder.getThunderPoint()[i]).x + SIDE / 2, (thunder.getThunderPoint()[i]).y + SIDE / 2, SIDE / 2); } settextcolor(WHITE); settextstyle(SIDE, SIDE, "楷体"); rect.left = 0; rect.top = 4*SIDE; rect.right = WIDTH; rect.bottom = HEIGHT; drawtext("GAMEOVER!", &rect, 2); isOver = true; return; } } }; int main() { initgraph(WIDTH, HEIGHT);//初始化界面 srand((unsigned)time(NULL));//设置随机数据种子,以当前的时间戳为种子 setbkcolor(RED); srand((unsigned)time(NULL)); settextcolor(BLUE); setbkmode(TRANSPARENT); // 设置文字输出模式为透明c setbkcolor(HSLtoRGB(100, 0.3, 7.5f)); Grid gr; while (gr.isOver==false) { gr.keyDown(); } system("pause"); clearcliprgn(); }

代码解读:

1.类之间的联系

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

最后

以上就是执着铃铛最近收集整理的关于C++实现扫雷、排雷小游戏的全部内容,更多相关C++实现扫雷、排雷小游戏内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(123)

评论列表共有 0 条评论

立即
投稿
返回
顶部