C#之五十 扫雷

举报
tea_year 发表于 2021/12/30 00:42:15 2021/12/30
【摘要】 视频课堂https://edu.csdn.net/course/play/7621 常规扫雷:      类:UserInfo:用户信息          Button:按钮            ...

视频课堂https://edu.csdn.net/course/play/7621

常规扫雷:

     类:UserInfo:用户信息

         Button:按钮

             TopScore:积分

             Main:主类

             用到一个声音文件:lei.wav

----------------------------------------------------部分参考代码如下--------------------

Main.cs


  
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace BigBangBomb
  10. {
  11. public partial class frmMain : Form
  12. {
  13. public static Timer time;
  14. static int stat;
  15. static int run=0;
  16. static int tim=0;
  17. int timertim = 0;
  18. static int status = 0;
  19. public static void end()
  20. {
  21. run = 1;
  22. time.Enabled = false;
  23. frmMain.showwin();
  24. }
  25. public static void blast()
  26. {
  27. run = 2;
  28. time.Enabled = false;
  29. frmMain.showlose();
  30. }
  31. public static void change_stat()
  32. {
  33. stat = 0;
  34. }
  35. static int width = 9;
  36. static int height = 9;
  37. static int mine=10;
  38. static string min1 = mine.ToString();
  39. static button[,] but1;
  40. public frmMain()
  41. {
  42. InitializeComponent();
  43. }
  44. public static button exp(int x, int y)
  45. {
  46. return (but1[x, y]);
  47. }
  48. private void button1_Click(object sender, EventArgs e)
  49. {
  50. frmMain frm = new frmMain();
  51. frmMain.time.Enabled = false;
  52. run = 0;
  53. frm.Show();
  54. this.Visible = false;
  55. this.Enabled = false;
  56. }
  57. public static void setatt(int x, int y, int m)
  58. {
  59. width = x;
  60. height = y;
  61. mine = m;
  62. }
  63. public static void setrun()
  64. {
  65. run = 0;
  66. }
  67. public static bool IsBomb(int x, int y)
  68. {
  69. if (x >= 0 && x < width)
  70. {
  71. if (y >= 0 && y < height)
  72. {
  73. return but1[x, y].Minded;
  74. }
  75. }
  76. return false;
  77. }
  78. public static void enabltime()
  79. {
  80. time.Enabled = true;
  81. }
  82. public static void distime()
  83. {
  84. time.Enabled = true;
  85. }
  86. public void start()
  87. {
  88. panel1.Width = width * 19 + 8;
  89. panel1.Height = height * 19 + 5;
  90. this.Width = panel1.Width + 13;
  91. this.Height = panel1.Height + 215;
  92. but1 = new button[width, height];
  93. button1.Left = this.Width / 2-28;
  94. int x = 0;
  95. int y = -19;
  96. time = new Timer();
  97. time.Interval = 1000;
  98. time.Tick += new EventHandler(time_Tick);
  99. for (int i = 0; i < width; i++)
  100. {
  101. for (int i1 = 0; i1 < height; i1++)
  102. {
  103. x = x + 19;
  104. if (i1 == 0)
  105. {
  106. x = 0;
  107. y = y + 19;
  108. }
  109. button b = new button(panel1, width, height, x, y,i,i1,mine);
  110. but1[i, i1] = b;
  111. }
  112. }
  113. int b4 = 0;
  114. Random r = new Random();
  115. while (b4 < mine)
  116. {
  117. int x1 = r.Next(0,width);
  118. int y1 = r.Next(0,height);
  119. button b3 = but1[x1, y1];
  120. if (!b3.Minded)
  121. {
  122. b3.Minded = true;
  123. b4++;
  124. }
  125. }
  126. }
  127. static void reset()
  128. {
  129. min1 = mine.ToString();
  130. }
  131. private void Form1_Load(object sender, EventArgs e)
  132. {
  133. stat = 1;
  134. tim = 0;
  135. timertim = 0;
  136. timer2.Enabled = true;
  137. frmMain.setatt(width,height,mine);
  138. reset();
  139. start();
  140. panel1.Visible = true;
  141. label2.Text = mine.ToString();
  142. button5.Text = "Pause";
  143. button5.Visible = true;
  144. }
  145. private void starterToolStripMenuItem_Click(object sender, EventArgs e)
  146. {
  147. frmMain.setatt(9, 9, 10);
  148. }
  149. private void customizedToolStripMenuItem_Click(object sender, EventArgs e)
  150. {
  151. Attributes at = new Attributes();
  152. at = new Attributes();
  153. at.Show();
  154. }
  155. private void timer1_Tick(object sender, EventArgs e)
  156. {
  157. }
  158. public static void chmine()
  159. {
  160. min1 = Convert.ToString(Convert.ToInt32(min1) - 1);
  161. }
  162. private void time_Tick(object sender, EventArgs e)
  163. {
  164. if (run == 0)
  165. {
  166. label1.Text = Convert.ToString(timertim + 1);
  167. timertim = timertim + 1;
  168. tim = timertim;
  169. }
  170. }
  171. public static void showlose()
  172. {
  173. MessageBox.Show("游戏结束时间 " + Convert.ToString(tim) + " seconds.");
  174. }
  175. public static void showwin()
  176. {
  177. MessageBox.Show("胜利时间" + Convert.ToString(tim) + " seconds.");
  178. if (status == 0)
  179. {
  180. bool ans;
  181. ans = CheckFile();
  182. if (ans == true)
  183. ReadCompareFileContents();
  184. else
  185. {
  186. StreamWriter SW = new StreamWriter("test.txt", false);
  187. frmUserInfo frm = new frmUserInfo();
  188. frm.ShowDialog();
  189. SW.WriteLine(frmUserInfo.UserName);
  190. SW.WriteLine(Convert.ToString(tim));
  191. SW.Close();
  192. }
  193. }
  194. }
  195. private static bool CheckFile()
  196. {
  197. StreamReader SR;
  198. try
  199. {
  200. SR = new StreamReader("test.txt");
  201. }
  202. catch (FileNotFoundException a)
  203. {
  204. return false;
  205. }
  206. SR.Close();
  207. return true;
  208. }
  209. private static void ReadCompareFileContents()
  210. {
  211. StreamReader SR = new StreamReader("test.txt");
  212. string str = SR.ReadLine();
  213. str = SR.ReadLine();
  214. SR.Close();
  215. int i = Convert.ToInt32(str);
  216. if (tim < i)
  217. {
  218. StreamWriter SW = new StreamWriter("test.txt", false);
  219. frmUserInfo frm = new frmUserInfo();
  220. frm.ShowDialog();
  221. SW.WriteLine(frmUserInfo.UserName);
  222. SW.WriteLine(Convert.ToString(tim));
  223. SW.Close();
  224. }
  225. }
  226. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  227. {
  228. }
  229. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  230. {
  231. Application.Exit();
  232. }
  233. private void panel1_MouseDown(object sender, MouseEventArgs e)
  234. {
  235. if (button5.Text == "Resume")
  236. {
  237. MessageBox.Show("游戏终止");
  238. }
  239. }
  240. private void button2_Click(object sender, EventArgs e)
  241. {
  242. status = 0;
  243. this.Visible = false;
  244. frmMain.setatt(9, 9, 10);
  245. frmMain frm = new frmMain();
  246. frmMain.time.Enabled = false;
  247. frm.Show();
  248. run = 0;
  249. }
  250. private void button3_Click(object sender, EventArgs e)
  251. {
  252. status = 1;
  253. Attributes at;
  254. at = new Attributes(this);
  255. at.Show();
  256. }
  257. private void button4_Click(object sender, EventArgs e)
  258. {
  259. Help.ShowHelp(this, "HelpBigBangBomb.chm");
  260. }
  261. private void button5_Click(object sender, EventArgs e)
  262. {
  263. if (button5.Text == "Pause")
  264. {
  265. time.Enabled = false;
  266. for (int yo = 0; yo < width; yo++)
  267. {
  268. for (int wo = 0; wo < height; wo++)
  269. {
  270. but1[yo, wo].b1.Enabled = false;
  271. }
  272. }
  273. button5.Text="Resume";
  274. }
  275. else
  276. {
  277. time.Enabled = true;
  278. for (int yo = 0; yo < width; yo++)
  279. {
  280. for (int wo = 0; wo < height; wo++)
  281. {
  282. but1[yo, wo].b1.Enabled = true;
  283. }
  284. }
  285. button5.Text = "Pause";
  286. }
  287. }
  288. private void panel1_Paint(object sender, PaintEventArgs e)
  289. {
  290. }
  291. private void timer2_Tick(object sender, EventArgs e)
  292. {
  293. label2.Text = min1;
  294. if (run > 0)
  295. {
  296. button1.Visible = true;
  297. time.Enabled = false;
  298. timer2.Enabled = false;
  299. }
  300. }
  301. private void button6_Click(object sender, EventArgs e)
  302. {
  303. Form frm = new frmTopScore();
  304. frm.Show();
  305. }
  306. }
  307. }


扫雷按钮Button.cs


  
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. namespace BigBangBomb
  7. {
  8. public class button
  9. {
  10. public Button b1;
  11. public EventHandler expl;
  12. Panel p;
  13. bool mined;
  14. bool gover;
  15. bool disman;
  16. bool open;
  17. int posx, posy;
  18. int nposx, nposy;
  19. int width;
  20. int height;
  21. int mines;
  22. int detmin=0;
  23. public button(Panel p1,int w, int h,int x, int y,int pos, int posy1,int mine)
  24. {
  25. width = w;
  26. height = h;
  27. mines = mine;
  28. posx = pos;
  29. posy = posy1;
  30. b1 = new Button();
  31. b1.FlatStyle = FlatStyle.Flat;
  32. int bw = 19;
  33. int bh = 19;
  34. b1.Width = bw;
  35. b1.BackColor = SystemColors.ControlDark;
  36. b1.Height = bh;
  37. b1.Top = x;
  38. b1.Left = y;
  39. b1.Click += new EventHandler(Click);
  40. b1.MouseDown += new MouseEventHandler(down);
  41. p1.Controls.Add(b1);
  42. mined = false;
  43. disman = false;
  44. open = false;
  45. }
  46. public bool Minded
  47. {
  48. get { return (this.mined); }
  49. set { this.mined = value; }
  50. }
  51. public bool Dismant
  52. {
  53. get { return (this.disman); }
  54. }
  55. private void down(object sender, MouseEventArgs e)
  56. {
  57. if(e.Button==MouseButtons.Right)
  58. {
  59. if (Dismant)
  60. {
  61. disman = false;
  62. b1.BackColor = SystemColors.ControlDark;
  63. }
  64. else
  65. {
  66. disman = true;
  67. b1.BackColor = Color.Orange;
  68. if(frmMain.IsBomb(posx,posy))
  69. {
  70. check();
  71. }
  72. frmMain.chmine();
  73. }
  74. }
  75. }
  76. public void check()
  77. {
  78. int win = 2;
  79. int coumin = 0;
  80. for (int i = 0; i < width; i++)
  81. {
  82. for (int y = 0; y < height; y++)
  83. {
  84. if (frmMain.exp(i, y).disman == true && frmMain.exp(i,y).mined==true)
  85. {
  86. coumin = coumin + 1;
  87. }
  88. if (coumin == mines)
  89. {
  90. button bh = frmMain.exp(i, y);
  91. if (bh.open == false)
  92. {
  93. if (bh.disman == false)
  94. {
  95. win = 1;
  96. }
  97. else
  98. {
  99. win = 0;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. if (win == 0)
  106. {
  107. detmin = detmin + 1;
  108. frmMain.end();
  109. for (int i = 0; i < width; i++)
  110. {
  111. for (int y = 0; y < height; y++)
  112. {
  113. frmMain.exp(i, y).b1.Enabled = false;
  114. }
  115. }
  116. }
  117. }
  118. private void show()
  119. {
  120. if (open == false && disman==false)
  121. {
  122. int c = 0;
  123. open = true;
  124. if (frmMain.IsBomb(posx - 1, posy - 1))
  125. {
  126. c++;
  127. }
  128. if (frmMain.IsBomb(posx - 0, posy - 1))
  129. {
  130. c++;
  131. }
  132. if (frmMain.IsBomb(posx + 1, posy - 1))
  133. {
  134. c++;
  135. }
  136. if (frmMain.IsBomb(posx - 1, posy - 0))
  137. {
  138. c++;
  139. }
  140. if (frmMain.IsBomb(posx - 0, posy - 0))
  141. {
  142. c++;
  143. }
  144. if (frmMain.IsBomb(posx + 1, posy - 0))
  145. {
  146. c++;
  147. }
  148. if (frmMain.IsBomb(posx - 1, posy + 1))
  149. {
  150. c++;
  151. }
  152. if (frmMain.IsBomb(posx - 0, posy + 1))
  153. {
  154. c++;
  155. }
  156. if (frmMain.IsBomb(posx + 1, posy + 1))
  157. {
  158. c++;
  159. }
  160. if (c == 0)
  161. {
  162. b1.Text = "";
  163. }
  164. if (c > 0)
  165. {
  166. b1.Text = (c.ToString());
  167. b1.BackColor = Color.Cornsilk;
  168. b1.Enabled = false;
  169. }
  170. else
  171. {
  172. b1.BackColor = Color.Cornsilk;
  173. b1.Enabled = false;
  174. show1(Posx - 1, Posy - 1);
  175. show1(Posx - 0, Posy - 1);
  176. show1(Posx + 1, Posy - 1);
  177. show1(Posx - 1, Posy - 0);
  178. show1(Posx - 0, Posy - 0);
  179. show1(Posx + 1, Posy - 0);
  180. show1(Posx - 1, Posy + 1);
  181. show1(Posx - 0, Posy + 1);
  182. show1(Posx + 1, Posy + 1);
  183. }
  184. }
  185. }
  186. public int Posx
  187. {
  188. get { return (this.posx); }
  189. }
  190. public int Posy
  191. {
  192. get { return (this.posy); }
  193. }
  194. private void PlayFile(String url)
  195. {
  196. WMPLib.WindowsMediaPlayer Player = new WMPLib.WindowsMediaPlayer();
  197. Player.URL = url;
  198. Player.controls.play();
  199. }
  200. private void expld()
  201. {
  202. for (int i = 0; i < width; i++)
  203. {
  204. for (int y = 0; y < height; y++)
  205. {
  206. button but1 = frmMain.exp(i, y);
  207. if(frmMain.IsBomb(i,y))
  208. {
  209. if (but1.b1.BackColor == Color.Orange)
  210. {
  211. but1.b1.BackColor = Color.Green;
  212. }
  213. else
  214. {
  215. but1.b1.BackColor = Color.Red;
  216. but1.b1.BackgroundImage = Image.FromFile("dead.jpg");
  217. PlayFile(@"expl.wav");
  218. }
  219. }
  220. else if(frmMain.exp(i,y).disman==true)
  221. {
  222. frmMain.exp(i, y).b1.BackColor = Color.Red;
  223. }
  224. but1.b1.Enabled = false;
  225. }
  226. }
  227. gover = true;
  228. frmMain.blast();
  229. }
  230. public bool gstat()
  231. {
  232. return(gover);
  233. }
  234. private void dispau(Button b12)
  235. {
  236. b12.Enabled = false;
  237. }
  238. private void show1(int x,int y)
  239. {
  240. if (x >= 0 && x < width)
  241. {
  242. if (y >= 0 && y < height)
  243. {
  244. button bot = frmMain.exp(x, y);
  245. bot.show();
  246. }
  247. }
  248. }
  249. private void Click(object sender, System.EventArgs e)
  250. {
  251. frmMain.enabltime();
  252. if (!Dismant)
  253. {
  254. if (Minded)
  255. {
  256. expld();
  257. frmMain.distime();
  258. }
  259. else
  260. {
  261. show();
  262. check();
  263. }
  264. }
  265. }
  266. }
  267. }


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/51601091

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。