C++基于EasyX实现简单扫雷游戏

时间:2021-09-05 05:34:25

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

C++基于EasyX实现简单扫雷游戏

【实现代码】

  1. #include <cmath>
  2. #include <time.h>
  3. #include <easyx.h>
  4. #include <conio.h>
  5. using namespace std;
  6. #define Size 500 //定义窗口大小
  7. #define SquareSize 50 //定义格子大小
  8. #define BackGroundColor LIGHTGRAY //定义背景颜色
  9. const int N = Size / SquareSize; //设置地图大小为N * N
  10. const int Num = N * N / 5; //设置总地雷数
  11. int num = Num, all = N * N - Num; //剩余地雷数和未格子访问数
  12. int Map[N + 10][N + 10]; //地图表示(i, j)旁边的地雷数目,等于-1表示该位置为地雷
  13. bool vis[N + 10][N + 10]; //标记是否(i, j)被访问
  14. bool vis_lm[N + 10][N + 10]; //标记(i, j)是否被标记为地雷
  15. int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
  16. int dy[] = { 1, -1, 0, 0, 1, -1, -1, 1 };
  17. void Init() {
  18. initgraph(Size, Size);
  19. setbkcolor(BackGroundColor);
  20. settextcolor(YELLOW);
  21. settextstyle(25, 0, _T("宋体"));
  22. cleardevice();
  23. }
  24. void BuildGameMap() { //构建地图
  25. srand((unsigned int)time(NULL)); //产生随机数
  26. for (int i = 1; i <= Num; ++i) {
  27. int x = rand() % N + 1, y = rand() % N + 1;
  28. while (Map[x][y] == -1) {
  29. x = rand() % N + 1, y = rand() % N + 1;
  30. }
  31. Map[x][y] = -1;
  32. }
  33. //计算每个格子旁边的地雷数目
  34. for (int i = 1; i <= N; ++i) {
  35. for (int j = 1; j <= N; ++j) {
  36. if (Map[i][j] == -1) continue;
  37. for (int k = 0; k < 8; ++k) {
  38. if (Map[i + dx[k]][j + dy[k]] == -1) ++Map[i][j];
  39. }
  40. }
  41. }
  42. }
  43. void InitGameGraph() { //地图初始化
  44. memset(Map, 0, sizeof(Map));
  45. memset(vis, false, sizeof(vis));
  46. memset(vis_lm, false, sizeof(vis_lm));
  47. num = Num, all = N * N - Num;
  48. BuildGameMap();
  49. cleardevice(); //清屏
  50. setfillcolor(WHITE);
  51. for (int i = 0; i <= Size; i += SquareSize) {
  52. line(i, 0, i, Size);
  53. }
  54. for (int i = 0; i <= Size; i += SquareSize) {
  55. line(0, i, Size, i);
  56. }
  57. }
  58. int Translate_X(int x) { //左边变换
  59. return x * SquareSize + SquareSize / 2;
  60. }
  61. void GameOver(bool tag) {
  62. RECT r = { 0, 0, Size - 1, Size - 1 };
  63. if (tag) {
  64. drawtext(_T("Win"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  65. }
  66. else {
  67. //游戏失败画出所有地雷
  68. setfillcolor(RED);
  69. for (int i = 1; i <= N; ++i) {
  70. for (int j = 1; j <= N; ++j) {
  71. if (Map[i][j] == -1) {
  72. if (vis_lm[i][j]) continue; //该地雷已被玩家标记,跳过
  73. outtextxy(Translate_X(i - 1), Translate_X(j - 1), 'X'); //否则显示出来
  74. }
  75. else if (vis_lm[i][j]) { //玩家标记了错误的地雷,标红
  76. fillcircle(Translate_X(i - 1), Translate_X(j - 1), SquareSize / 2 - 10);
  77. }
  78. }
  79. }
  80. drawtext(_T("Failed"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  81. }
  82. }
  83. void Help() { //调试函数(其实是挂)
  84. for (int i = 1; i <= N; ++i) {
  85. for (int j = 1; j <= N; ++j) {
  86. if (Map[i][j] == -1) {
  87. outtextxy(Translate_X(i - 1), Translate_X(j - 1), 'X');
  88. }
  89. }
  90. }
  91. }
  92. void StartGame() {
  93. Init();
  94. while (true) {
  95. InitGameGraph();
  96. //Help();
  97. bool IF_WIN = true;
  98. while (all || num) { //胜利条件是所有非地雷格子均被访问且所有地雷均被标记
  99. MOUSEMSG msg = GetMouseMsg(); //获取用户鼠标信息
  100. int x = ceil(msg.x * 1.0 / SquareSize * 1.0), y = ceil(msg.y * 1.0 / SquareSize * 1.0);
  101. if (vis[x][y]) continue; //该格已被访问,跳过
  102. if (msg.mkLButton) { //鼠标左键按下
  103. if (Map[x][y] == -1) { //如果是地雷那么游戏结束
  104. IF_WIN = false;
  105. break;
  106. }
  107. outtextxy(Translate_X(x - 1), Translate_X(y - 1), Map[x][y] + '0');
  108. --all;
  109. vis[x][y] = true;
  110. }
  111. else if (msg.mkRButton) { //鼠标右键按下
  112. if (!vis_lm[x][y]) { //如果该格子已被标记,消除标记
  113. vis_lm[x][y] = true;
  114. if (Map[x][y] == -1) --num;
  115. solidcircle(Translate_X(x - 1), Translate_X(y - 1), SquareSize / 2 - 10);
  116. }
  117. else if (vis_lm[x][y]) { //否则标记
  118. vis_lm[x][y] = false;
  119. if (Map[x][y] == -1) ++num;
  120. clearrectangle((x - 1) * SquareSize + 1, (y - 1) * SquareSize + 1, x * SquareSize - 1, y * SquareSize - 1);
  121. }
  122. }
  123. }
  124. GameOver(IF_WIN);
  125. int t = _getch();
  126. }
  127. }
  128. int main() {
  129. StartGame();
  130. return 0;
  131. }

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

原文链接:https://blog.csdn.net/weixin_44211980/article/details/104543819