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

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

服务器之家 - 编程语言 - C/C++ - 利用c++和easyx图形库做一个低配版扫雷游戏

利用c++和easyx图形库做一个低配版扫雷游戏

2021-08-12 12:11TTODS. C/C++

这篇文章主要介绍了用c++和easyx图形库做一个低配版扫雷游戏,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

游戏界面

由于这个游戏是我抱着玩一玩的心态做出来的,所以没有过多的去设计界面,也没有去找游戏的资源(图片、游戏音效等)。仅使用了不同颜色的方块来表示游戏中方块的状态和种类。(绿色为初始状态(未翻转的状态),黄色为翻转后的背景颜色,蓝色表示已插旗的方块,红色代表地雷)

图1 游戏主菜单界面

利用c++和easyx图形库做一个低配版扫雷游戏

图二 模式一的游戏界面(20*20 40个雷)

利用c++和easyx图形库做一个低配版扫雷游戏

图三 模式二的游戏界面(10*10 20个雷)

利用c++和easyx图形库做一个低配版扫雷游戏

图四 游戏成功界面

利用c++和easyx图形库做一个低配版扫雷游戏

图五 游戏失败界面

利用c++和easyx图形库做一个低配版扫雷游戏

2.全部代码

  1. #include<graphics.h>
  2. #include<iostream>
  3. #include<conio.h>
  4. #include<time.h>
  5. using namespace std;
  6. #define POINTWIDTH 30
  7. //雷的数量
  8. int mineCnt;
  9. int mapSize;
  10. //已翻转的个数
  11. int _count;
  12. //是否有雷
  13. bool mine[20][20] = { false };
  14. //是否已翻转
  15. bool overturned[20][20] = { false };
  16. bool flaged[20][20] = { false };
  17. //游戏模式
  18. int mode;
  19. //游戏重新开始的标志
  20. int cmd = 1;
  21. //游戏结束标志
  22. bool gameOver;
  23. //玩家获胜标志
  24. bool _win;
  25. //小方格(坐标)
  26. typedef struct point {
  27. int x;
  28. int y;
  29. point(int _x, int _y) {
  30. x = _x; y = _y;
  31. }
  32. }point;
  33. //计算周围的地雷数量
  34. int getAroundMineCnt(point p) {
  35. int cnt=0;
  36. for (int i = p.x - 1; i <= p.x + 1; i++) {
  37. for (int j = p.y - 1; j <= p.y + 1; j++) {
  38. if (i >= 0 && i < 20 && j >= 0 && j < 20 && mine[i][j])
  39. cnt++;
  40. }
  41. }
  42. return cnt;
  43. }
  44. //画点(小方格)
  45. void drawPoint(point p,int color) {
  46. setfillcolor(color);
  47. fillrectangle(p.x*POINTWIDTH +140,p.y*POINTWIDTH +140, p.x * POINTWIDTH + 140+ POINTWIDTH, p.y * POINTWIDTH + 140+ POINTWIDTH);
  48. }
  49. //画地雷(红色方块代替)
  50. void drawMine(point p) {
  51. setfillcolor(RED);
  52. fillrectangle(p.x * POINTWIDTH + 140, p.y * POINTWIDTH + 140, p.x * POINTWIDTH + 140+POINTWIDTH, p.y * POINTWIDTH + 140+POINTWIDTH);
  53. }
  54. //画旗(蓝色方块代替)
  55. void drawflag(point p) {
  56. flaged[p.x][p.y] = true;
  57. drawPoint(p, BLUE);
  58. }
  59. //游戏结束对话框与"重玩"、"退出"
  60. void gameover(int &cmd) {
  61. gameOver = 1;
  62. for (int i = 0; i < mapSize; i++) {
  63. for (int j = 0; j < mapSize; j++) {
  64. if (mine[i][j]) {
  65. point p(i, j);
  66. drawMine(p); }
  67. }
  68. }
  69. Sleep(500);
  70. setfillcolor(LIGHTGRAY);
  71. fillrectangle(200,300,700,550);
  72. rectangle(200, 500, 350, 550);
  73. rectangle(550, 500, 700, 550);
  74. setbkmode(1);
  75. settextstyle(60, 0, 0);
  76. outtextxy(300, 400, _T("Game over"));
  77. settextstyle(38, 0, 0);
  78. outtextxy(220, 510, _T("Restart"));
  79. outtextxy(560, 510, _T( " Quit"));
  80. MOUSEMSG m;
  81. while (1) {
  82. m = GetMouseMsg();
  83. if (m.mkLButton&&m.y > 500 && m.y < 550 && m.x>200 && m.x < 350)
  84. break;
  85. else if (m.mkLButton&&m.y > 500 && m.y < 550 && m.x>550 && m.x < 700) {
  86. cmd = 0;
  87. break;
  88. }
  89. }
  90. }
  91. //游戏胜利对话框与"重玩"、"退出"
  92. void win(int &cmd) {
  93. _win = 1;
  94. setfillcolor(LIGHTGRAY);
  95. fillrectangle(200, 300, 700, 550);
  96. rectangle(200, 500, 350, 550);
  97. rectangle(550, 500, 700, 550);
  98. setbkmode(1);
  99. settextstyle(60, 0, 0);
  100. outtextxy(300, 400, _T("You Win!"));
  101. settextstyle(38, 0, 0);
  102. outtextxy(220, 510, _T("Restart"));
  103. outtextxy(560, 510, _T(" Quit"));
  104. MOUSEMSG m;
  105. while (1) {
  106. m = GetMouseMsg();
  107. if (m.mkLButton&&m.y > 500 && m.y < 550 && m.x>200 && m.x < 350)
  108. break;
  109. else if (m.mkLButton&&m.y > 500 && m.y < 550 && m.x>550 && m.x < 700) {
  110. cmd = 0;
  111. break;
  112. }
  113. }
  114. }
  115. //翻转
  116. void overturn(point p,int t) {
  117. settextstyle(POINTWIDTH*0.8 , POINTWIDTH*0.8 , 0);
  118. settextcolor(BLACK);
  119. if (t == 1) {
  120. if (!mine[p.x][p.y]) {
  121. _count++;
  122. drawPoint(p, YELLOW);
  123. overturned[p.x][p.y] =true ;
  124. //判断周围的雷的数量是否为0,为0则翻转该方块周边的8个方块
  125. if (getAroundMineCnt(p) != 0) {
  126. int cnt = getAroundMineCnt(p);
  127. _TCHAR a[3];
  128. _stprintf_s(a, L"%d", cnt);
  129. outtextxy(p.x*POINTWIDTH+POINTWIDTH*0.1+ 140, p.y*POINTWIDTH+POINTWIDTH*0.1 + 140, a);
  130. if (_count == mapSize * mapSize - mineCnt) { win(cmd); return; }
  131. }
  132. else {
  133. for (int i = p.x - 1; i <= p.x + 1; i++) {
  134. for(int j=p.y-1;j<=p.y+1;j++)
  135. if (i >= 0 && i < mapSize && j >= 0 && j < mapSize&&!overturned[i][j]) {
  136. point temp(i, j);
  137. overturn(temp, 1);
  138. }
  139. }
  140. }
  141. }
  142. else { gameover(cmd); return; }
  143. }
  144. else {
  145. if (!flaged[p.x][p.y]) {
  146. drawflag(p);
  147. }
  148. else {
  149. flaged[p.x][p.y] = false;
  150. drawPoint(p, GREEN);
  151. }
  152. }
  153. }
  154. //右键插旗
  155. void play() {
  156.  
  157. while (true) {
  158. MOUSEMSG m;
  159. m = GetMouseMsg();
  160. if (m.mkLButton&&m.x > 140 && m.x < 140+mapSize*POINTWIDTH && m.y > 140 && m.y < 140+mapSize*POINTWIDTH)
  161. {
  162. point p((m.x - 140) / POINTWIDTH, (m.y - 140) / POINTWIDTH);//将鼠标点击的坐标转换成对应位置的方块
  163. if(!overturned[p.x][p.y])
  164. overturn(p,1);
  165. }
  166. if (m.mkRButton&&m.x > 140 && m.x < 740 && m.y > 140 && m.y < 740)
  167. {
  168. point p((m.x - 140) / POINTWIDTH, (m.y - 140) / POINTWIDTH);
  169. if(!overturned[p.x][p.y])
  170. overturn(p, 2);
  171. }
  172. if (gameOver) return;
  173. else if (_win) return;
  174. }
  175. }
  176. //初始化游戏界面
  177. void initGameface() {
  178. if(mode==1)
  179. rectangle(140, 140, 740, 740);
  180. else rectangle(140, 140, 440, 440);
  181. setbkcolor(LIGHTGRAY);
  182. cleardevice();
  183. setbkcolor(YELLOW);
  184. point p(0, 0);
  185. for (int i = 0; i < mapSize; i++) {
  186. for (int j = 0; j < mapSize; j++) {
  187. p.x = i;
  188. p.y = j;
  189. drawPoint(p, GREEN);
  190. }
  191. }
  192.  
  193. }
  194. //地雷的随机生成器
  195. void generator() {
  196. int cnt = 0;
  197. while (cnt < mineCnt) {
  198. int i = rand() % mapSize;
  199. int j = rand() % mapSize;
  200. if (!mine[i][j]) {
  201. mine[i][j] = true;
  202. cnt++;
  203. }
  204. }
  205. }
  206. //游戏的开始界面(图1)
  207. void startInterface(int &mode) {
  208. mode = 1;
  209. initgraph(880, 880);
  210. setbkcolor(LIGHTGRAY);
  211. cleardevice();
  212. setlinecolor(RED);
  213. rectangle(100, 100, 780, 300);
  214. rectangle(300, 400, 580, 500);
  215. rectangle(300, 530, 580, 630);
  216. rectangle(300,660, 580, 760);
  217. settextcolor(RED);
  218. settextstyle(100,0,0);
  219. outtextxy(300,140,L"扫 雷");
  220. settextstyle(60, 0, 0);
  221. outtextxy(320, 420, L"新 游 戏");
  222. outtextxy(320, 550, L"简 单");
  223. outtextxy(320, 680, L"游戏帮助");
  224. MOUSEMSG m;
  225. while (1) {
  226. m = GetMouseMsg();
  227. if (m.mkLButton&&m.x > 300 && m.x < 580 && m.y>400 && m.y < 500)
  228. break;
  229. else if (m.mkLButton&&m.x > 300 && m.x < 580 && m.y>530 && m.y < 630)
  230. if (mode == 1) {
  231. mode = 2;
  232. rectangle(300, 530, 580, 630);
  233. outtextxy(320, 550, L"困 难");
  234. }
  235. else {
  236. mode = 1;
  237. rectangle(300, 530, 580, 630);
  238. outtextxy(320, 550, L"简 单");
  239. }
  240. else if (m.mkLButton&&m.x > 300 && m.x < 580 && m.y>660 && m.y < 760) {
  241. cleardevice();
  242. MOUSEMSG mm;
  243. while (1) {
  244. mm = GetMouseMsg();
  245. if (mm.mkLButton) break;
  246. }
  247. startInterface(mode);
  248. }
  249. }
  250. }
  251. //初始化游戏
  252. void initgame(int mode) {
  253. _win = 0;
  254. _count = 0;
  255. gameOver = 0;
  256. if (mode == 1) {
  257. mineCnt= 40;
  258. mapSize = 20;
  259. }
  260. else {
  261. mineCnt = 20;
  262. mapSize = 10;
  263. }
  264. for (int i = 0; i < mapSize; i++) {
  265. for (int j = 0; j < mapSize; j++) {
  266. mine[i][j] = 0;
  267. flaged[i][j] = 0;
  268. overturned[i][j] = 0;
  269. }
  270. }
  271. }
  272. //整个游戏过程
  273. void game() {
  274. srand(unsigned(time));
  275. startInterface(mode);
  276. while (cmd) {
  277. initgame(mode);
  278. initGameface();
  279. generator();
  280. play();
  281. }
  282. }
  283. int main() {
  284. game();
  285. }

3. 符加说明:本程序使用了简单好用的easyx图形库:可以Easyx官网中下载安装,且Easyx官网提供的文档详细的介绍了各种函数的用法,很容易上手。

总结

以上所述是小编给大家介绍的利用c++和easyx图形库做一个低配版扫雷游,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://blog.csdn.net/qq_44525150/article/details/103787072

延伸 · 阅读

精彩推荐