当前位置:首页 > 通信资讯 > 正文

c++ 扫雷游戏(c语言扫雷小游戏)

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

界面:

c++ 扫雷游戏(c语言扫雷小游戏)

游戏思想:

扫雷游戏:

1.随机给定雷点坐标

2.判断每个点的雷情况

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

代码:

  1. #include<iostream>
  2. #include<vector>
  3. #include<sstream>
  4. #include<algorithm>
  5. #include<graphics.h>
  6. #include<ctime>
  7. #include<conio.h>
  8. using namespace std;
  9. #define WIDTH 500
  10. #define HEIGHT 500
  11. #define SIDE 50
  12. #define THUNDERNUM 10
  13. #define TOTALSCORE 100
  14. #define EVERY_OF_DESC 5
  15. int score = TOTALSCORE;
  16. class Point {
  17. public:
  18. int x;
  19. int y;
  20. Point() {}
  21. Point(int _x, int _y)
  22. :x(_x), y(_y)
  23. {}
  24. void setPoint(int x, int y) {
  25. this->x = x;
  26. this->y = y;
  27. }
  28. Point(const Point& xy)
  29. :x(xy.x), y(xy.y)
  30. {}
  31. bool operator<(const Point& xy)const {
  32. if (x <= xy.x)
  33. return true;
  34. return y <= xy.y;
  35. }
  36. Point& operator=(const Point& xy) {
  37. if (this != &xy) {
  38. x = xy.x;
  39. y = xy.y;
  40. }
  41. return *this;
  42. }
  43. bool operator==(const Point& xy) {
  44. return ((x == xy.x) && (y = xy.y));
  45. }
  46. bool operator!=(const Point& xy) {
  47. return !(*this == xy);
  48. }
  49. };
  50. class ThunderPoint {
  51. private:
  52. vector<Point> storage;
  53. int num;
  54. public:
  55. ThunderPoint(int _num = THUNDERNUM)
  56. :num(_num) {
  57. //初始化雷点位置
  58. int count = 0;
  59. while (count != num) {
  60. int x = (rand() % (WIDTH / SIDE)) * SIDE;//随机生成数据,使之在图形界面之中
  61. int y = (rand() % (HEIGHT / SIDE)) * SIDE;
  62. Point tem(x, y);
  63. int i = -1;
  64. bool flag = false;
  65. for (i = 0; i < storage.size(); i++) {
  66. if (tem == storage[i]) {
  67. flag = true;
  68. break;
  69. }
  70. }
  71. if (flag==false) {
  72. //说明没有重复
  73. storage.push_back(tem);
  74. count++;
  75. }
  76. }
  77. }
  78. vector<Point>& getThunderPoint() {
  79. //获得雷点位置
  80. return storage;
  81. }
  82. void drawThunderPoint() {
  83. auto it = storage.begin();
  84. while (it != storage.end()) {
  85. setfillcolor(RED);
  86. fillcircle((*it).x, (*it).y, SIDE / 2);
  87. ++it;
  88. }
  89. }
  90. };
  91. class Grid {
  92. private:
  93. vector<vector<int>> nearbythunder;
  94. vector<vector<bool>> isopen;
  95. ThunderPoint thunder;
  96. Point currposition;
  97. Point preposition;
  98. vector<COLORREF> color;
  99. public:
  100. bool isOver;
  101. public:
  102. Grid()
  103. :thunder(10),currposition()
  104. {
  105. preposition.setPoint(0, 0);
  106. for (int i = 0; i < 10; ++i) {
  107. color.push_back(RGB(rand() % 256, rand() % 256, rand() % 256));
  108. }
  109. isOver = false;
  110. isopen.resize(HEIGHT / SIDE, vector<bool>(WIDTH / SIDE, false));
  111. nearbythunder.resize(HEIGHT/SIDE,vector<int>(WIDTH/SIDE,0));
  112. currposition.setPoint(0, 0);
  113. //先将雷点的位置标出来,标为数字 9
  114. // 任何一个点,他附近的雷点最多8个
  115. auto it = thunder.getThunderPoint().begin();
  116. while (it != thunder.getThunderPoint().end()) {
  117. int x = ((*it).x)/SIDE;
  118. int y = ((*it).y)/SIDE;
  119. nearbythunder[x][y] = 9;
  120. if (((y - SIDE/SIDE) >= 0) && (nearbythunder[x][y - SIDE/SIDE] != 9)) {
  121. nearbythunder[x][y - SIDE/SIDE]++;
  122. }
  123. if (((y - SIDE/SIDE) >= 0) && ((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y - SIDE/SIDE] != 9)) {
  124. nearbythunder[x - SIDE/SIDE][y - SIDE/SIDE]++;
  125. }
  126. if (((y - SIDE/SIDE) >= 0) && ((x + SIDE/SIDE) < WIDTH/SIDE) && (nearbythunder[x + SIDE/SIDE][y - SIDE/SIDE] != 9)) {
  127. nearbythunder[x + SIDE/SIDE][y - SIDE/SIDE]++;
  128. }
  129. if (((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y] != 9)) {
  130. nearbythunder[x - SIDE/SIDE][y]++;
  131. }
  132. if (((x + SIDE/SIDE) < WIDTH/SIDE) && (nearbythunder[x + SIDE/SIDE][y] != 9)) {
  133. nearbythunder[x + SIDE/SIDE][y]++;
  134. }
  135. if (((y + SIDE/SIDE) < HEIGHT/SIDE) && (nearbythunder[x][y + SIDE/SIDE] != 9)) {
  136. nearbythunder[x][y + SIDE/SIDE]++;
  137. }
  138. if (((y + SIDE/SIDE) < HEIGHT/SIDE) && ((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE] != 9)) {
  139. nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE]++;
  140. }
  141. if (((y + SIDE / SIDE) < HEIGHT / SIDE) && ((x + SIDE / SIDE) < WIDTH / SIDE) && (nearbythunder[x + SIDE / SIDE][y + SIDE / SIDE] != 9)) {
  142. nearbythunder[x + SIDE / SIDE][y + SIDE / SIDE]++;
  143. }
  144. ++it;
  145. }
  146. for (int i = 0; i < HEIGHT; i = i + SIDE) {
  147. setlinecolor(YELLOW);
  148. line(0, i, WIDTH, i);
  149. line(i, 0, i, HEIGHT);
  150. }
  151. }
  152. void keyDown()
  153. {
  154. char userKey = _getch();
  155. if (userKey == -32) // 表明这是方向键
  156. userKey = -_getch(); // 获取具体方向,并避免与其他字母的 ASCII 冲突
  157. setfillcolor(GREEN);
  158. preposition = currposition;
  159. switch (userKey)
  160. {
  161. case 'w':
  162. case 'W':
  163. case -72: //上
  164. {
  165. if (currposition.y - SIDE >= 0) {
  166. currposition.y -= SIDE;
  167. }
  168. }
  169. break;
  170. case 's':
  171. case 'S':
  172. case -80://下
  173. {
  174. if (currposition.y + SIDE < HEIGHT)
  175. currposition.y += SIDE;
  176. }
  177. break;
  178. case 'a':
  179. case 'A':
  180. case -75://左
  181. {
  182. if (currposition.x - SIDE >= 0)
  183. currposition.x -= SIDE;
  184. }
  185. break;
  186. case 'd':
  187. case 'D':
  188. case -77://右
  189. {
  190. if (currposition.x + SIDE < WIDTH) {
  191. currposition.x += SIDE;
  192. }
  193. }
  194. break;
  195. case '\r':
  196. {
  197. score -= EVERY_OF_DESC;
  198. settextstyle(10, 10, "楷体");
  199. settextcolor(GREEN);
  200. RECT rect;
  201. rect.left = WIDTH-100; rect.top = 100; rect.right = WIDTH; rect.bottom = 200;
  202. string ll = to_string(score);
  203. const char* str = ll.c_str();
  204. //drawtext(str, &rect, 1);
  205. //现在开始翻
  206. openOne(currposition);
  207. }
  208. break;
  209. }
  210. setlinecolor(BLACK);
  211. setlinestyle(1, 0);
  212. line(preposition.x, preposition.y, preposition.x + SIDE, preposition.y);
  213. setfillcolor(GREEN);
  214. setlinecolor(WHITE);
  215. setlinestyle(1, 2);
  216. line(currposition.x, currposition.y, currposition.x + SIDE, currposition.y );
  217. }
  218. private:
  219. bool pred(bool x) {
  220. return x == true;
  221. }
  222. void openOne(const Point& cur) {
  223. //说明此时翻一个
  224. isopen[cur.x / SIDE][cur.y / SIDE] = true;
  225. //此时应该着色
  226. settextcolor(color[nearbythunder[cur.x/SIDE][cur.y/SIDE]]);
  227. RECT rect;
  228. settextstyle(SIDE, SIDE, "楷体");
  229. rect.left = cur.x; rect.top = cur.y; rect.right = cur.x + SIDE; rect.bottom = cur.y + SIDE;
  230. drawtext((to_string(nearbythunder[cur.x / SIDE][cur.y / SIDE])).c_str(), &rect, 1);
  231. int count = 0;
  232. for (int i = 0; i < HEIGHT / SIDE; ++i) {
  233. for (int j = 0; j < WIDTH / SIDE; ++j) {
  234. if (isopen[i][j] == true) {
  235. count++;
  236. }
  237. }
  238. }
  239. cout << count << endl;
  240. if (nearbythunder[cur.x / SIDE][cur.y / SIDE] == 9 || count==((WIDTH/SIDE)*(HEIGHT/SIDE)- THUNDERNUM)) {
  241. //说明游戏结束
  242. setfillcolor(RED);
  243. for (int i = 0; i < thunder.getThunderPoint().size(); i++) {
  244. fillcircle((thunder.getThunderPoint()[i]).x + SIDE / 2, (thunder.getThunderPoint()[i]).y + SIDE / 2, SIDE / 2);
  245. }
  246. settextcolor(WHITE);
  247. settextstyle(SIDE, SIDE, "楷体");
  248. rect.left = 0; rect.top = 4*SIDE; rect.right = WIDTH; rect.bottom = HEIGHT;
  249. drawtext("GAMEOVER!", &rect, 2);
  250. isOver = true;
  251. return;
  252. }
  253. }
  254. };
  255. int main() {
  256. initgraph(WIDTH, HEIGHT);//初始化界面
  257. srand((unsigned)time(NULL));//设置随机数据种子,以当前的时间戳为种子
  258. setbkcolor(RED);
  259. srand((unsigned)time(NULL));
  260. settextcolor(BLUE);
  261. setbkmode(TRANSPARENT); // 设置文字输出模式为透明c
  262. setbkcolor(HSLtoRGB(100, 0.3, 7.5f));
  263. Grid gr;
  264. while (gr.isOver==false) {
  265. gr.keyDown();
  266. }
  267. system("pause");
  268. clearcliprgn();
  269. }

代码解读:

1.类之间的联系

c++ 扫雷游戏(c语言扫雷小游戏)

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

原文链接:https://blog.csdn.net/xinger_28/article/details/106174296

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。