扫雷游戏设计代码(VB完善版)
vb扫雷代码
ScaleHeight = 3060
ScaleWidth = 4620
StartUpPosition = 3 '窗口缺省
Begin monDialog CommonDialog1
End
Begin VB.Menu cls
Caption = "清空榜单"
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Dim lin As Integer '记录上一次游戏情况的容器
Dim zong As Integer '格子数量,从0开始计算
Dim lei As Integer '雷的数量
Dim shu As Integer '一行格子的数量
Dim heng As Integer '一列格子的数量
Left = 840
Picture = "扫雷.frx":0000
Style = 1 'Graphical
TabIndex = 1
TabStop = 0 'False
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 375
VB课程设计报告——扫雷游戏
VB课程设计报告-------扫雷游戏马学清1008260132 指导老师:张小兵一、题目介绍在计算机逐步渗入社会生活各个层面的今天,计算机已经成为人们日常生活的一分,越来越多的人使用计算机办公、娱乐等等。
扫雷游戏是Windows操作系统自带的一款小游戏,在过去的几年里,Windows操作系统历经数次换代更新,变得越来越庞大、复杂,功能也越来越强大,但是这款小游戏依然保持原来的容貌,可见这款小游戏受到越来越多人的喜爱。
我利用Turbo C编写了与它功能相仿的扫雷游戏,寓学于乐。
程序功能二、程序功能程序的功能是随机生成地雷数,通过鼠标操作玩游戏,不接受键盘,按任意键结束程序。
单击屏幕上的“开始”按钮可以重新开始游戏。
所有地雷标出后胜利,当鼠标左键单击到地雷时失败。
三、程序设计目的1.培养综合运用所学知识独立完成课题的能力。
2.更深入地理解和掌握该课程中的有关基本概念,程序设计思想和方法。
3.提高对工作认真负责、一丝不苟,对同学团结友爱,协作攻关的基本素质。
4.培养勇于探索、严谨推理、实事求是、有错必改,用实践来检验理论,全方位考虑问题等科学技术人员应具有的素质。
5.培养从资料文献、科学实验中获得知识的能力,提高从别人经验中找到解决问题的新途径的悟性,初步培养工程意识和创新能力。
6.对掌握知识的深度、运用理论去处理问题的能力、实验能力、课程设计能力、书面及口头表达能力进行考核四、游戏规则设计10×10格区域,单击鼠标后,在此格中显示的数字,则表示它周围就有几个空格内含有的地雷数,周围没有地雷的显示空白,则不能再单击了。
如果是地雷,则游戏结束。
如果判断是地雷,可以单击鼠标右键标出,显示红色,要取消红旗标志则再单击鼠标右键,当所有地雷都标出,每个空格都处理过,那么游戏就胜利结束。
五、程序源代码'级别Private Sub Form_Load() '默认初级dengji = 1Call nandu(dengji)End SubPrivate Sub zidingyi_Click() '自定义类型Load Form2Form2.ShowEnd SubPrivate Sub again_Click() '重新开始Call nandu(dengji)End SubPrivate Sub chuji_Click() '1级dengji = 1Call nandu(dengji)End SubPrivate Sub zhongji_Click() '2级dengji = 2Call nandu(dengji)End SubPrivate Sub gaoji_Click() '3级dengji = 3Call nandu(dengji)End SubPrivate Sub tuichu_Click() '退出Unload MeEnd SubPrivate Sub bang_Click() '显示英雄榜Load Form3Form3.ShowEnd SubPrivate Sub about_Click() '显示关于扫雷Load Form4Form4.ShowEnd SubPrivate Sub Timer1_Timer() '处理计时器If time < 999 Thentime = time + 1stime1 = Format(time, "000")heng = Pic1.Width - (4 + 13) - 13 - 13 '时间处理For i = 1 To 3stime2 = Mid(stime1, i, 1)Pic1.PaintPicture Clip1.GraphicCell(11 - Val(stime2)), heng, 4heng = heng + 13Nextshijian(dengji) = timeEnd IfIf time = 999 Then '游戏失败MsgBox "你输了", 0, "失败"End IfEnd SubPublic Sub nandu(dengji)'调用主程序Dim s As IntegerOpen App.Path & "mingzi" For Random As 1For s = 1 To 3Get 1, s, yuanshijian(s)NextClose 1Pic1.Picture = LoadPicture() '清空计时器和记雷显示器Pic2.Picture = LoadPicture()Pic2.Enabled = TrueTimer1.Enabled = Falseq = 0mousedown = FalseIf dengji = 1 Then '判断等级hang = 9: lie = 9: chulei = 9ElseIf dengji = 2 Thenhang = 16: lie = 16: chulei = 39ElseIf dengji = 3 Thenhang = 30: lie = 30: chulie = 99ElseIf dengji = 4 Thenhang = dingh: lie = dingl: chulei = dingleiEnd IfReDim fankai(hang - 1, lie - 1) '重定义行列数ReDim lei(hang - 1, lie - 1)ReDim jilu(hang - 1, lie - 1)With Pic2.Height = 16 * hang + 3.Width = 16 * lie + 4.Left = 1 * 8.Top = 6 * 8End WithWith Pic1.Height = 8 * 4.Width = 16 * lie + 4.Left = 1 * 8.Top = 1 * 8End WithForm1.Width = 30 * Screen.TwipsPerPixelX + 16 * Screen.TwipsPerPixelX * lie '确定窗体高宽度Form1.Height = 100 * Screen.TwipsPerPixelY + 13 * Screen.TwipsPerPixelY + 16 * Screen.TwipsPerPixelY * hang + 100For i = 0 To lie - 1 '开始时雷区For j = 0 To hang - 1Pic2.PaintPicture Clip3.GraphicCell(0), i * 16, j * 16NextNextDo While k < chulei '布雷m = Int(Rnd * hang)n = Int(Rnd * lie)If lei(m, n) <> 1 Thenlei(m, n) = 1k = k + 1End IfLoopPic1.PaintPicture Clip1.GraphicCell(11 - k \ 100), 4, 4 '雷数Pic1.PaintPicture Clip1.GraphicCell(11 - (k Mod 100) \ 10), 4 + 13, 4Pic1.PaintPicture Clip1.GraphicCell(11 - (k Mod 10)), 4 + 26, 4Pic1.PaintPicture Clip1.GraphicCell(11), Pic1.Width - (4 + 13), 4 '时间Pic1.PaintPicture Clip1.GraphicCell(11), Pic1.Width - (4 + 26), 4Pic1.PaintPicture Clip1.GraphicCell(11), Pic1.Width - (4 + 13 * 3), 4Pic1.PaintPicture Clip1.GraphicCell(11), 4, 4Pic1.PaintPicture Clip2.GraphicCell(4), Pic1.Width / 2 - 12, 4 '显示笑脸End Sub'图片框1的MouseDown事件Private Sub Pic1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If X >= Pic1.Width / 2 - 12 And X <= Pic1.Width / 2 + 12 And Y >= 4 And Y <= 24 + 4 Then Pic1.PaintPicture Clip2.GraphicCell(0), Pic1.Width / 2 - 12, 4End IfEnd Sub'图片框1的MouseUp事件Private Sub Pic1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)If X >= Pic1.Width / 2 - 12 And X <= Pic1.Width / 2 + 12 And Y >= 4 And Y <= 24 + 4 Then time = 0Call nandu(dengji)End IfEnd Sub'图片框的MouseDown事件Private Sub Pic2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Static intq As Integerk = chuleimouseup = Falsei = X \ 16: j = Y \ 16If fankai(i, j) = False ThenIf Button = 1 Then '左键mousedown = TruePic1.PaintPicture Clip2.GraphicCell(3), Pic1.Width / 2 - 12, 4If jilu(i, j) <> 1 And jilu(i, j) <> -1 ThenPic2.PaintPicture Clip3.GraphicCell(15), i * 16, j * 16ElseIf b = -1 ThenPic2.PaintPicture Clip3.GraphicCell(6), i * 16, j * 16End IfElseIf Button = 2 Then '右键jilu(i, j) = jilu(i, j) + 1Pic2.PaintPicture Clip3.GraphicCell(jilu(i, j)), i * 16, j * 16If jilu(i, j) \ 2 = 1 Then jilu(i, j) = -1If jilu(i, j) = 1 Then intq = intq + 1If intq < 9 ThenPic1.PaintPicture Clip1.GraphicCell(11 - (k Mod 10) + intq), 4 + 26, 4 '画小旗ElseIf intq < 99 ThenFor intc = 0 To 9Pic1.PaintPicture Clip1.GraphicCell(11 - (k Mod 10) + intq), 4 + 26, 4 '雷数减少Next intcPic1.PaintPicture Clip1.GraphicCell(11 - (k Mod 100) \ 10 + intq), 4 + 13, 4 End IfIf jilu(i, j) = 1 And lei(i, j) = 1 Thenq = q + 1ElseIf jilu(i, j) = -1 Thenq = q - 1End IfIf q = chulei Then Call shengliEnd IfEnd IfEnd Sub'图片框2的MouseUp事件Private Sub Pic2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) mousedown = FalseIf Timer1.Enabled = False Then Timer1.Enabled = Truei = X \ 16: j = Y \ 16If Button = 1 ThenIf i >= 0 And i < hang And j >= 0 And j < lie ThenPic1.PaintPicture Clip2.GraphicCell(4), Pic1.Width / 2 - 12, 4If fankai(i, j) = False ThenIf jilu(i, j) <> 1 ThenIf lei(i, j) = 1 ThenCall jieshu(i, j)MsgBox "你输了", 0, "失败"ElseIf panduan(i, j) = 0 ThenCall fan(i, j)ElseIf panduan(i, j) <> 0 ThenPic2.PaintPicture Clip3.GraphicCell(15 - panduan(i, j)), i * 16, j * 16 '显示周围相邻雷的数量q = q + 1fankai(i, j) = TrueEnd IfEnd IfEnd IfEnd IfIf q = hang * lie - chulei Then Call shengliEnd IfEnd Sub'图片框2的MouseMove事件Private Sub Pic2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)If mousedown = False ThenExit SubEnd Ifm = X \ 16: n = Y \ 16Call yidong(m, n)End Sub'通用过程:移动Private Sub yidong(m, n)If m >= 0 And m < hang And n >= 0 And n < lie ThenIf fankai(m, n) = False ThenIf jilu(m, n) = -1 ThenPic2.PaintPicture Clip3.GraphicCell(6), m * 16, n * 16ElseIf jilu(m, n) = 0 ThenPic2.PaintPicture Clip3.GraphicCell(15), m * 16, n * 16End IfEnd IfFor i = m - 1 To m + 1For j = n - 1 To n + 1If i >= 0 And i < hang And j >= 0 And j < lie ThenIf fankai(i, j) = False ThenIf i <> m Or j <> n ThenIf jilu(i, j) <> 1 ThenIf jilu(i, j) = 0 ThenPic2.PaintPicture Clip3.GraphicCell(0), i * 16, j * 16ElseIf jilu(i, j) = -1 ThenPic2.PaintPicture Clip3.GraphicCell(2), i * 16, j * 16End IfEnd IfEnd IfEnd IfEnd IfNextNextEnd IfEnd SubPrivate Sub jieshu(m, n)Pic2.PaintPicture Clip3.GraphicCell(3), m * 16, n * 16Pic1.PaintPicture Clip2.GraphicCell(2), Pic1.Width / 2 - 12, 4Pic2.Enabled = FalseTimer1.Enabled = FalseFor i = 0 To hang - 1For j = 0 To lie - 1If i <> m And j <> n ThenIf lei(i, j) = 1 And jilu(i, j) <> 1 ThenPic2.PaintPicture Clip3.GraphicCell(5), i * 16, j * 16ElseIf lei(i, j) <> 1 And jilu(i, j) = 1 ThenPic2.PaintPicture Clip3.GraphicCell(4), i * 16, j * 16End IfEnd IfNextNextEnd Sub'判断周围雷的数量Private Function panduan(m, n)Dim leishu As IntegerFor i = m - 1 To m + 1For j = n - 1 To n + 1If i >= 0 And i < hang And j >= 0 And j < lie ThenIf lei(i, j) = 1 Thenleishu = leishu + 1End IfEnd IfNextNextpanduan = leishuEnd Function'翻开递归Private Sub fan(m, n)Dim int1 As IntegerFor i = m - 1 To m + 1For j = n - 1 To n + 1If i >= 0 And i < hang And j >= 0 And j < lie ThenIf fankai(i, j) = False ThenIf lei(i, j) <> 1 ThenIf panduan(i, j) = 0 ThenPic2.PaintPicture Clip3.GraphicCell(15), i * 16, j * 16fankai(i, j) = Trueq = q + 1For int1 = 1 To 20 ',防止直接全部翻开Call fan(i, j) '递归:打开相邻无雷区域Next int1ElsePic2.PaintPicture Clip3.GraphicCell(15 - panduan(i, j)), i * 16, j * 16fankai(i, j) = Trueq = q + 1End IfEnd IfEnd IfEnd IfNextNextEnd Sub'成功Private Sub shengli()Pic1.PaintPicture Clip2.GraphicCell(1), Pic1.Width / 2 - 12, 4Pic2.Enabled = FalseTimer1.Enabled = FalseFor i = 0 To hang - 1For j = 0 To lie - 1If lei(i, j) = 1 And jilu(i, j) <> 1 ThenPic2.PaintPicture Clip3.GraphicCell(5), i * 16, j * 16End IfNextNextIf shijian(dengji) < yuanshijian(dengji) And dengji <> 4 Then mingzi(dengji) = InputBox("破纪录,输入姓名", "成功") Open App.Path & "mingzi" For Random As #1Put 1, dengji + 3, mingzi(dengji)Put 1, dengji, shijian(dengji)Close 1ElseMsgBox "你赢了", "0", "成功"End IfEnd SubPrivate Sub Command1_Click()dingh = Val(Text1.Text)dingl = Val(Text1.Text)dinglei = Val(Text3.Text)If Text1 <> "" And Text2 <> "" And Text3 <> "" ThenIf dinglei < dingh * dingl ThenCall Form1.nandu(4)Unload MeElseMsgBox "输入不合规范", 0, "请重新输入"End IfElseMsgBox "输入不合规范", 0, "请重新输入"End IfEnd SubPrivate Sub Command2_Click()Unload MeEnd SubPrivate Sub Command1_Click()Unload Form3End SubPrivate Sub Form_Load()Dim lab1(1 To 3) As IntegerDim lab2(1 To 3) As StringDim w As Integer, i As Integer, r As Integer, e As IntegerOpen App.Path & "mingzi" For Random As 1For i = 1 To 3Get 1, i, lab1(i)NextClose 1Open App.Path & "mingzi" For Random As 1For j = 4 To 6Get 1, j, lab2(j - 3)NextClose 1For e = 1 To 3bel4(e - 1).Caption = lab1(e)bel4(e + 2).Caption = lab2(e)NextFor r = 1 To 3yuanshijian(r) = lab1(r)NextEnd Sub六、心得体会课程设计是本科学习阶段一次非常难得的理论与实际相结合的机会,通过这次比较完整的一个程序的设计,我摆脱了单纯的理论知识学习状态,和实际设计的结合锻炼了我的综合运用所学的基础知识,解决实际问题的能力,同时也提高我查阅文献资料、对程序整体的把握等其他能力水平,而且通过对整体的掌控,对局部的取舍,以及对细节的斟酌处理,都使我的能力得到了锻炼,经验得到了丰富。
扫雷vb实现
扫雷vb代码实现_____________________________________________________________‘Form1Dim L(12) As Integer '产生的地雷随机数、Public min, sec As Integer '用来计时的分、秒Public temp As Integer '储存红旗剩余数的全局变量Public result As IntegerFunction f(ByVal i As Integer, ByVal j As Integer) As Integer '对各个区域进行初始化赋值f = 0For k = 0 To 11If i = Int(L(k) / 10) And j = (L(k) Mod 10) Then f = 1NextEnd FunctionPrivate Sub Command1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)Dim m, n As Integerm = Int(Index / 10)n = Index Mod 10If Button = 2 Then '右击命令控件时Command1.Item(Index).Picture = LoadPicture(App.Path & "\0.jpg")If f(m, n) = 1 Then result = result - 1temp = temp - 1: Label2.Caption = "剩余红旗:" & Str(temp)If result = 0 Then Timer1.Enabled = False: Form2.Show '通向胜利的大门ElseIf Button = 1 Then '左击命令控件时If f(m, n) = 0 ThenCommand1.Item(Index).Caption = Str(sum(m, n))If sum(m, n) = 0 ThenCall disp(m, n)End IfElseCommand1.Item(Index).Picture = LoadPicture(App.Path & "\1.jpg")result = -1Timer1.Enabled = FalseFor m = 0 To 9For n = 0 To 9If f(m, n) = 1 ThenCommand1.Item(m * 10 + n).Picture = LoadPicture(App.Path & "\1.jpg")ElseCommand1.Item(m * 10 + n).Caption = Str(sum(m, n))End IfNext nNext mForm2.Show '踩到地雷的感觉如何End IfEnd IfEnd IfEnd SubPrivate Sub Form_Load() '窗口1初载时执行的动作sec = 0: min = 0: temp = 12: result = 12Label2.Caption = "剩余红旗:" & Str(temp)data '调用函数初始化地雷数据End SubFunction data() As Integer '对数据进行初始化RandomizeDim n, i, j As Integern = 12L(0) = Int(Rnd * 100)For i = 1 To n - 1L(i) = Int(Rnd * 100)For j = 0 To i - 1If L(i) = L(j) Then i = i - 1NextNextEnd FunctionFunction sum(ByVal i As Integer, ByVal j As Integer) '某一区域周围的地雷数sum = f(i - 1, j - 1) + f(i - 1, j) + f(i - 1, j + 1) + f(i, j - 1) + f(i, j + 1) + f(i + 1, j - 1) + f(i + 1, j) + f(i + 1, j + 1)End FunctionPrivate Sub Timer1_Timer() '计时sec = sec + 1If sec = 60 Then min = min + 1: sec = sec - 60Label1.Caption = "耗时:" + Format(min, "00") + ":" + Format(sec, "00") End SubFunction disp(ByVal i As Integer, ByVal j As Integer) As Integer '打通周围没有地雷的区域Command1.Item(i * 10 + j).Caption = "0"Command1.Item(i * 10 + j).Enabled = FalseIf i < 0 Or j < 0 Or i > 9 Or j > 9 Then Exit FunctionDim m, n As IntegerFor m = i - 1 To i + 1If m = i ThenFor n = j - 1 To j + 1 Step 2If m < 0 Or m > 9 Or n < 0 Or n > 9 ThenElseIf sum(m, n) = 0 And Command1.Item(m * 10 + n).Enabled <> False Thendisp m, nElseIf Command1.Item(m * 10 + n).Enabled = True Then Command1.Item(m * 10 + n).Caption = Str(sum(m, n))End IfEnd IfNextEnd IfFor n = j - 1 To j + 1If m < 0 Or m > 9 Or n < 0 Or n > 9 Thendisp = 0ElseIf Command1.Item(m * 10 + n).Enabled = False Thendisp = 0ElseIf sum(m, n) = 0 Thendisp m, nElseCommand1.Item(m * 10 + n).Caption = Str(sum(m, n))End IfEnd IfNextNextEnd Function_____________________________________________________________‘form2Private Sub Command1_Click()Unload Form1Form1.ShowUnload MeEnd SubPrivate Sub Command2_Click()Form1.temp = 12Form1.result = 12Form1.min = 0Form1.sec = 0Dim i As IntegerFor i = 0 To 99mand1.Item(i).Enabled = Truemand1.Item(i).Caption = ""mand1.Item(i).Picture = LoadPicture("")Form1.Timer1.Enabled = TrueNextUnload MeEnd SubPrivate Sub Command3_Click()Unload MeUnload Form1End SubPrivate Sub Form_Load()If Form1.result = -1 ThenLabel1.Caption = " 兔崽子你给炸死了吧" & Chr(13) + Chr(10) + "你的最终成绩是" & Str(Form1.min) & "分" & Str(Form1.sec) & "秒"ElseIf Form1.result = 0 ThenLabel1.Caption = "恭喜你顺利通过" & Chr(13) + Chr(10) + "你的最终成绩是" & Str(Form1.min) & "分" & Str(Form1.sec) & "秒"End IfEnd Sub。
扫雷游戏设计代码(VB完善版)
扫雷游戏VB设计实现最终界面如下:显然只需完成三个窗体的设计即可,具体设计界面和代码如下:一、主窗体(form10)设计界面:主窗体(form10)的代码:Dim d(11, 11) As IntegerDim k As IntegerDim v(100) As Integer '定义全局变量'Private Sub Command1_Click(Index As Integer)Timer1.Enabled = True '当点击任意一个命令按钮时(即开始游戏),则启动计时器' i = Index \ 10 + 1j = Index Mod 10 + 1 '将二维数组的元素与命令按钮一一对应'If d(i, j) = 1 Then '判断是否点到地雷'Timer1.Enabled = False '关闭计时器,游戏结束'For i = 1 To 10For j = 1 To 10n = 10 * (i - 1)m = j - 1If d(i, j) = 1 ThenCommand1.Item(m + n).Picture = LoadPicture(App.Path & "\2.jpg") '在按钮上显示地雷图片'End IfForm1.Show '弹出子窗体1(判断输赢)'Next jNext iElseIf v(Index) = Index + 1 Then '判断是否插上红旗或是问号图片'Command1.Item(Index).Picture = LoadPicture() '清除图片'Command1.Item(Index).Caption = f(i, j) '调用函数,显示周围地雷数'Command1.Item(Index).Enabled = False '将按钮设为不可用'Label7.Caption = Val(Label7.Caption) + 1v(Index) = 0End Ifh = s(i, j) '调用函数,显示周围的情况(边界)'For Y = 0 To 99If v(Y) = Y + 1 ThenCommand1.Item(Y).Enabled = True '如果是按钮插上了红旗则将按钮设置为可用'End IfNext YEnd IfFor k = 0 To 99If Command1.Item(k).Enabled = False Thenp = p + 1 '统计扫过的按钮个数'If p = 90 ThenFor Y = 0 To 100v(Y) = 0Next Y '将数组v的元素重新置0(此步目的为实现重玩而设)'Form2.Show '如果等于90个则结束游戏,弹出子窗体2(赢了)'End IfEnd IfNext kEnd SubPrivate Sub Command1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 Then '右击鼠标'If v(Index) <> Index + 1 Then '判断是否已插上红旗'Command1.Item(Index).Picture = LoadPicture(App.Path & "\1.jpg") '插上红旗'v(Index) = Index + 1 '给数组元素赋值,以此实现按钮图片的切换或显示状态'Label7.Caption = Val(Label7.Caption) - 1 '在标签7中显示插上的红旗个数'ElseCommand1.Item(Index).Picture = LoadPicture(App.Path & "\3.jpg") '将问号图片替换红旗'Label7.Caption = Val(Label7.Caption) + 1v(Index) = 0End IfEnd IfFor i = 1 To 10For j = 1 To 10If d(i, j) = 1 Thenn = 10 * (i - 1)m = j - 1If v(m + n) = m + n + 1 Then c = c + 1If c = 10 ThenFor Y = 0 To 100v(Y) = 0Next Y '将数组v的元素重新置0(此步目的为实现重玩而设)'Form2.Show '如果等于90个则结束游戏,弹出子窗体2(赢了)'End IfEnd IfNext jNext iEnd SubPrivate Sub Form_Load()k = 0For i = 1 To 10For j = 1 To 10d(i, j) = 0 '将数组元素置0'Next jNext iDo While n <> 10Randomizei = Int(10 * Rnd + 1)j = Int(10 * Rnd + 1)If d(i, j) = 0 Then d(i, j) = 1: n = n + d(i, j) '产生十个随机数,即相当于十个地雷'Loopbel7.Caption = 10bel2.Caption = 0bel3.Caption = 0End SubFunction f(i, j) '定义一个函数求一个区域周围地雷的个数'f = f + d(i - 1, j - 1) + d(i - 1, j) + d(i - 1, j + 1) + d(i, j - 1)f = f + d(i, j + 1) + d(i + 1, j - 1) + d(i + 1, j) + d(i + 1, j + 1)End FunctionFunction s(i, j) '定义一个函数显示点击区域周围的情况(即边界)' For Y = 0 To 100If v(Y) = Y + 1 ThenCommand1.Item(Y).Enabled = False '如插上了红旗,则将按钮先设为不可用'End IfNext YIf f(i, j) <> 0 Then '函数嵌套,调用函数判断周围是否无地雷'n = 10 * (i - 1)m = j - 1Command1.Item(m + n).Picture = LoadPicture()Command1.Item(n + m).Caption = f(i, j)Command1.Item(n + m).Enabled = False '清除图片,显示地雷数,设置按钮不可用'ElseFor a = i - 1 To i + 1For b = j - 1 To j + 1If a <> 0 And b <> 11 And a <> 11 And b <> 0 Thenn = 10 * (a - 1)m = b - 1If Command1.Item(n + m).Enabled = True ThenCommand1.Item(m + n).Picture = LoadPicture()Command1.Item(n + m).Caption = f(a, b)Command1.Item(n + m).Enabled = False '清除图片,显示地雷数,设置按钮不可用'h = s(a, b) '调用函数本身,即实现递归'End IfEnd IfNext bNext aEnd IfEnd FunctionPrivate Sub Timer1_Timer() '设计一个计时器'Label2.Caption = Val(Label2.Caption) + 1Label3.Caption = Val(Label2.Caption) \ 60 + Val(Label3.Caption)Label2.Caption = Val(Label2.Caption) Mod 60End Sub二、子窗体一(form1)界面如下:子窗体一(form1)的代码:Private Sub Command1_Click()Unload Form10Unload form1Form10.ShowEnd SubPrivate Sub Command2_Click()Unload Form10Unload form1End SubPrivate Sub Command3_Click()For i = 0 To 99mand1.Item(i).Picture = LoadPicture() mand1.Item(i).Caption = ""mand1.Item(i).Enabled = TrueNext ibel7.Caption = 10bel2.Caption = 0bel3.Caption = 0Unload form1End Sub三、子窗体(form2)界面如下:子窗体二(form2)的代码:Private Sub Command1_Click()Unload Form10Form10.ShowUnload form2End SubPrivate Sub Command2_Click()Unload Form10Unload form2End SubPrivate Sub Command3_Click()For i = 0 To 99mand1.Item(i).Picture = LoadPicture()mand1.Item(i).Caption = ""mand1.Item(i).Enabled = TrueNext iUnload form2bel7.Caption = 10bel2.Caption = 0bel3.Caption = 0End Sub注释:共三个窗体(这里是form10、form1、form2)、三个标签(这里是label2、label3、label7)设计时,根据具体的情况对应修改即可。
VB扫雷小游戏编程代码
VB扫雷小游戏一.编程目的二.编程思路1.新建command_up和label_down控件2.用load加载控件3.根据雷区的X、Y、以及难度进行随机布雷。
4.统计每一个label周围雷的数量并作为label的caption。
5.在单击command的时候显示label6.在右击command的时候进行标记7.在label上左右键同时按下的时候检查已标记雷的数量与label显示的数量是否一致。
三.界面设计四.代码设计Dim Start_Time, End_TimeDim Area_X%, Area_Y%, Area%, Area_List()Dim Current_Mine%Dim Difficulty#Dim Continue_Flag%, Success_Flag%, LeftAndRight_Flag%Dim Near_ListDim Mine_CountPrivate Sub Command_End_Click()EndEnd SubPrivate Sub Delete_Item(List(), Index As Integer)Dim i%For i = LBound(List) + Index - 1 To UBound(List) - 1List(i) = List(i + 1)Next i'防止100%的困难度If UBound(List) > LBound(List) Then ReDim Preserve List(LBound(List) To UBound(List) - 1) End SubPrivate Sub Command_retry_Click()'卸载For i = 1 To AreaUnload Label_Down(i)Unload Command_Up(i)Next iCommand_Start.Caption = "开始游戏"Call Command_Start_ClickEnd SubPrivate Sub Command_Up_Click(Index As Integer)Success_Flag = 1If Continue_Flag = 1 ThenIf Timer1.Enabled = False Then Call Command_Start_ClickIf Label_Down(Index).Caption = "X" ThenSuccess_Flag = 0Continue_Flag = 0For i = 1 To AreaIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbRed Then'标记雷正确Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_correct.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbGreen Then'标记雷错误Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_wrong.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseCommand_Up(i).Visible = FalseLabel_Down(i).Visible = TrueEnd IfNext iTimer1.Enabled = Falsetemp = MsgBox("Game Over !", vbOKOnly, "游戏结束")ElseIf Val(Label_Down(Index).Caption) > 0 ThenCommand_Up(Index).Visible = FalseLabel_Down(Index).Visible = TrueElse'如果等于0的话应该将周边的清零Command_Up(Index).Visible = FalseLabel_Down(Index).Visible = Truej = IndexFor i = 1 To 8'判断控件是否存在If j + Near_List(i) > 0 And j + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(j + Near_List(i)).Left -Label_Down(j).Left) <= Label_Down(j).Width And Abs(Label_Down(j + Near_List(i)).Top -Label_Down(j).Top) <= Label_Down(j).Height Then'判断是否有雷If Label_Down(j + Near_List(i)).BackColor = vbGreen And Command_Up(j + Near_List(i)).Visible = True ThenCall Command_Up_Click(j + Near_List(i)) '注意此处循环调用的时候一定要避免陷入死循环End IfEnd IfEnd IfNext iEnd If'检查是否游戏成功For i = 1 To AreaIf Command_Up(i).Visible = True And Label_Down(i).Caption <> "X" ThenSuccess_Flag = 0Exit ForEnd IfNext iIf Success_Flag = 1 ThenIf Continue_Flag = 1 ThenTimer1.Enabled = FalseFor i = 1 To AreaIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbRed Then'标记雷正确Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_correct.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbGreen Then'标记雷错误Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_wrong.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseCommand_Up(i).Visible = FalseLabel_Down(i).Visible = TrueEnd IfNext itemp = MsgBox("恭喜,扫雷成功!" & vbCrLf & "耗时:" & Mid(Label_Time.Caption, 4) & vbCrLf & "鸣谢:平方X O(∩_∩)O~", vbOKOnly, "成功") End IfContinue_Flag = 0 '提示一次后结束,防止在调用Command_Click事件中重复提示End IfEnd IfCommand_Start.SetFocusEnd SubPrivate Sub Command_Start_Click()If Command_Start.Caption = "开始游戏" ThenCommand_Start.Caption = "重新开始"Continue_Flag = 1Timer1.Enabled = TrueDifficulty = Val(Text_Difficulty.Text) / 100Area_X = Val(Text_X.Text)Area_Y = Val(Text_Y.Text)Area = Area_X * Area_Y'初始化这里进行二次初始化的原因是如果在之前的运行中对字体进行了改变,将有可能造成此处的控件大小发生变化With Picture_show.Left = 200.Top = 200.Width = 750 * 10.Height = 750 * 10.Visible = FalseEnd WithWith Command_Up(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = 200.Top = 200.Width = 750 * 10 / IIf(Area_X > Area_Y, Area_X, Area_Y).FontSize = 25 * (.Width / 750) '会自动缩放,必须先设置了.Height = 750 * 10 / IIf(Area_X > Area_Y, Area_X, Area_Y).Visible = FalseEnd WithWith Command_Up(0).Left = 200.Top = 200.Width = Label_Down(0).Width.Height = Label_Down(0).Height.Visible = FalseEnd WithReDim Near_List(1 To 8)Near_List(1) = 0 - 1 - Area_YNear_List(2) = 0 - 0 - Area_YNear_List(3) = 0 + 1 - Area_YNear_List(4) = 0 - 1Near_List(5) = 0 + 1Near_List(6) = 0 - 1 + Area_YNear_List(7) = 0 - 0 + Area_YNear_List(8) = 0 + 1 + Area_Y'如果在列表中有相等的元素将有可能造成统计雷的数目错误For i = 1 To 8For j = i + 1 To 8If Near_List(i) = Near_List(j) Then Near_List(i) = 0Next jNext iArea_temp = 0For Y = 1 To Area_Y'加载labelFor X = 1 To Area_XArea_temp = Area_temp + 1Load Label_Down(Area_temp)With Label_Down(Area_temp).Left = Label_Down(0).Left + Label_Down(0).Width * ((Area_temp -1) Mod Area_Y).Top = Label_Down(0).Top + Label_Down(0).Height * ((Area_temp -1) \ Area_Y).BackColor = vbGreen.Visible = False.Alignment = 2.Font = .FontBoldEnd With'加载commandLoad Command_Up(Area_temp)With Command_Up(Area_temp)'对列数求余的话就是在这一行第几个了.Left = Command_Up(0).Left + Command_Up(0).Width * ((Area_temp - 1) Mod Area_Y)'整除列数的话可以确定第几行.Top = Command_Up(0).Top + Command_Up(0).Height * ((Area_temp - 1) \ Area_Y).Visible = TrueEnd WithNext XNext YReDim Area_List(1 To Area)For i = 1 To AreaArea_List(i) = iNext i' 随即布雷RandomizeMine_Count = Val(Text_Mine_Count.Text)For i = 1 To Mine_CountCurrent_Mine = Int(Rnd * (UBound(Area_List) - LBound(Area_List) + 1) + 1) '在数组中随机一个,注意此处2个+1的必要性和准确性Label_Down(Area_List(Current_Mine)).BackColor = vbRed '将该位置标记为雷Call Delete_Item(Area_List, Current_Mine) '删除该位置,防止再次标记Next i'检查雷的数目For j = 1 To AreaIf Label_Down(j).BackColor = vbRed ThenLabel_Down(j).Caption = "X"ElseMine_Number = 0For i = 1 To 8'判断控件是否存在If j + Near_List(i) > 0 And j + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(j + Near_List(i)).Left -Label_Down(j).Left) <= Label_Down(j).Width And Abs(Label_Down(j + Near_List(i)).Top -Label_Down(j).Top) <= Label_Down(j).Height Then'判断是否有雷If Label_Down(j + Near_List(i)).BackColor = vbRed ThenMine_Number = Mine_Number + 1End IfEnd IfEnd IfNext iLabel_Down(j).Caption = Mine_NumberEnd IfNext jStart_Time = Now()ElseIf Command_Start.Caption = "重新开始" ThenCall Command_retry_ClickEnd IfEnd SubPrivate Sub Command_Up_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 ThenIf Command_Up(Index).Caption = "" ThenCommand_Up(Index).Caption = "X"Command_Up(Index).Picture = LoadPicture(App.Path + "\pictures\mine.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)ElseIf Command_Up(Index).Caption = "X" ThenCommand_Up(Index).Caption = "?"Command_Up(Index).Picture = LoadPicture(App.Path + "\pictures\Unknown.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)ElseIf Command_Up(Index).Caption = "?" ThenCommand_Up(Index).Caption = ""Command_Up(Index).Picture = LoadPicture("") End IfEnd IfEnd SubPrivate Sub Form_Load()With Picture_show.Left = 200.Top = 200.Width = 750 * 10.Height = 750 * 10.Visible = FalseEnd WithWith Command_Up(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd With'加载计时器Timer1.Enabled = FalseTimer1.Interval = 100'加载滚动条With HScroll_Difficulty.LargeChange = 5.SmallChange = 1.Max = 100.Min = 0.Value = 10End WithWith HScroll_Area_X.LargeChange = 5.SmallChange = 1.Max = 100.Min = 1.Value = 10End WithWith HScroll_Area_Y.LargeChange = 5.SmallChange = 1.Max = 100.Min = 1.Value = 10End WithWith HScroll_Mine_Count.LargeChange = 5.SmallChange = 1.Max = 100.Min = 0.Value = 10End With'由于很多数据不方便处理,索性让其禁用了Text_Difficulty.Enabled = FalseText_Mine_Count.Enabled = FalseText_X.Enabled = FalseText_Y.Enabled = FalseEnd SubPrivate Sub HScroll_Area_X_Change()Text_X.Text = HScroll_Area_X.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_X_Scroll()Text_X.Text = HScroll_Area_X.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_Y_Change()Text_Y.Text = HScroll_Area_Y.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_Y_Scroll()Text_Y.Text = HScroll_Area_Y.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Difficulty_Change()Text_Difficulty.Text = HScroll_Difficulty.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Difficulty_Scroll()Text_Difficulty.Text = HScroll_Difficulty.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Mine_Count_Change()Text_Mine_Count.Text = HScroll_Mine_Count.ValueHScroll_Difficulty.Value = HScroll_Mine_Count.Value / (HScroll_Area_X.Value * HScroll_Area_Y.Value) * 100End SubPrivate Sub HScroll_Mine_Count_Scroll()Text_Mine_Count.Text = HScroll_Mine_Count.ValueHScroll_Difficulty.Value = HScroll_Mine_Count.Value / (HScroll_Area_X.Value * HScroll_Area_Y.Value) * 100End SubPrivate Sub Label_Down_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" And Command_Up(Index + Near_List(i)).Caption <> "?" ThenCommand_Up(Index + Near_List(i)).Picture = LoadPicture("")End IfEnd IfEnd IfNext iEnd SubPrivate Sub label_down_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If LeftAndRight_Flag + Button = 3 Then '双击完成Mine_Number = Val(Label_Down(Index).Caption)Mark_mine_number = 0For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption = "X" ThenMark_mine_number = Mark_mine_number + 1End IfEnd IfEnd IfNext iIf Val(Label_Down(Index).Caption) - Mark_mine_number <= 0 Then '已全部标出,自动点开For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" ThenCall Command_Up_Click(Index + Near_List(i))End IfEnd IfEnd IfNext iElse '如果没有全部标注的话应该显示一下嘛For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" ThenCommand_Up(Index + Near_List(i)).Picture = LoadPicture(App.Path + "\pictures\xia.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)End IfEnd IfEnd IfNext iEnd IfElseLeftAndRight_Flag = Button'Print LeftAndRight_FlagEnd IfEnd SubPrivate Sub Timer1_Timer()LeftAndRight_Flag = 0End_Time = Now()spend_time = (End_Time - Start_Time) * 10 ^ 5Label_Time.Caption = "时间:" & Format(Int(spend_time) \ (60 * 60), "00") & ":" & Format((Int(spend_time) Mod (60 * 60)) \ 60, "00") & ":" & Format(Int(spend_time) Mod 60, "00") & "." & Format(Int((spend_time - Int(spend_time)) * 1000), "000")End Sub五.软件截图1 2 3。
利用VB开发扫雷游戏
、
初 始 化
1 .在 本 程 序 中 ,设 置 程 序 界 面 为 6X 6共 3 6个 子 区 域 ,
Fr o i= 1T 6 o3
R no i ad mz e
初 始 化 雷 的个 数
I n (0 fIt 1
R d +1 n )>8T en h
每 一 个 区 域 i按 从 左 至 右 ,从 上 至 下 标 记 ) lbl() cn ( 有 aeli和 o —
徐 勇
摘
要
本 文介 绍在 Wid w 环境 下利 用 V 6 0开发 一个 简单 的扫 雷 游戏 的方 法 。 no s B,
扫 雷 ,V 6 0 B . ,控件
关 键词
扫 雷 游 戏是 Widw 操 作 系 统 自带 的 一个 小 游 戏 ,过 去 的 no s
几 年 里 Widw 操 作 系 统 历 经 数 次换 代 , 变得 越 来 越 庞 大 、复 no s
b l qzs。
Fr o i= 2 T o 5
F r 1 L b l i . p in = ar i一 1 + a ri+ 1 o m . a e1() Ca t o r( ) r( ) + a ri+ 5 + a ri+ 6 + ar i+ 7) r( )~ r( ) r(
尝 试 着 开发 一 个 类 似 的 “ 雷 游 戏 ” ,下 面就 将 笔 者 在 教 学 过 扫
4 .用 一 个 cm a d控 件 表 示 游 戏 进 行 的 状 态 ( 始 化 按 omn 初 钮 ) 初 始 状 态 该 cm a d控 件 的 p tr 性 为 图 :平 静 的 , omn i ue属 c
路。
一
P i e ) 为 O表 示 什 么 也 没 有 , 示 红 旗 , ( d x值 n 1表 2是 问 号
VB扫雷小游戏编程代码
VB扫雷小游戏一.编程目的二.编程思路1.新建command_up和label_down控件2.用load加载控件3.根据雷区的X、Y、以及难度进行随机布雷。
4.统计每一个label周围雷的数量并作为label的caption。
5.在单击command的时候显示label6.在右击command的时候进行标记7.在label上左右键同时按下的时候检查已标记雷的数量与label显示的数量是否一致。
三.界面设计四.代码设计Dim Start_Time, End_TimeDim Area_X%, Area_Y%, Area%, Area_List()Dim Current_Mine%Dim Difficulty#Dim Continue_Flag%, Success_Flag%, LeftAndRight_Flag%Dim Near_ListDim Mine_CountPrivate Sub Command_End_Click()EndEnd SubPrivate Sub Delete_Item(List(), Index As Integer)Dim i%For i = LBound(List) + Index - 1 To UBound(List) - 1List(i) = List(i + 1)Next i'防止100%的困难度If UBound(List) > LBound(List) Then ReDim Preserve List(LBound(List) To UBound(List) - 1) End SubPrivate Sub Command_retry_Click()'卸载For i = 1 To AreaUnload Label_Down(i)Unload Command_Up(i)Next iCommand_Start.Caption = "开始游戏"Call Command_Start_ClickEnd SubPrivate Sub Command_Up_Click(Index As Integer)Success_Flag = 1If Continue_Flag = 1 ThenIf Timer1.Enabled = False Then Call Command_Start_ClickIf Label_Down(Index).Caption = "X" ThenSuccess_Flag = 0Continue_Flag = 0For i = 1 To AreaIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbRed Then'标记雷正确Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_correct.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbGreen Then'标记雷错误Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_wrong.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseCommand_Up(i).Visible = FalseLabel_Down(i).Visible = TrueEnd IfNext iTimer1.Enabled = Falsetemp = MsgBox("Game Over !", vbOKOnly, "游戏结束")ElseIf Val(Label_Down(Index).Caption) > 0 ThenCommand_Up(Index).Visible = FalseLabel_Down(Index).Visible = TrueElse'如果等于0的话应该将周边的清零Command_Up(Index).Visible = FalseLabel_Down(Index).Visible = Truej = IndexFor i = 1 To 8'判断控件是否存在If j + Near_List(i) > 0 And j + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(j + Near_List(i)).Left -Label_Down(j).Left) <= Label_Down(j).Width And Abs(Label_Down(j + Near_List(i)).Top -Label_Down(j).Top) <= Label_Down(j).Height Then'判断是否有雷If Label_Down(j + Near_List(i)).BackColor = vbGreen And Command_Up(j + Near_List(i)).Visible = True ThenCall Command_Up_Click(j + Near_List(i)) '注意此处循环调用的时候一定要避免陷入死循环End IfEnd IfEnd IfNext iEnd If'检查是否游戏成功For i = 1 To AreaIf Command_Up(i).Visible = True And Label_Down(i).Caption <> "X" ThenSuccess_Flag = 0Exit ForEnd IfNext iIf Success_Flag = 1 ThenIf Continue_Flag = 1 ThenTimer1.Enabled = FalseFor i = 1 To AreaIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbRed Then'标记雷正确Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_correct.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseIf Command_Up(i).Visible = True And Command_Up(i).Caption = "X" And Label_Down(i).BackColor = vbGreen Then'标记雷错误Command_Up(i).Picture = LoadPicture(App.Path + "\pictures\mine_wrong.gif", , , Command_Up(i).Width, Command_Up(i).Height)Command_Up(i).Visible = TrueLabel_Down(i).Visible = TrueElseCommand_Up(i).Visible = FalseLabel_Down(i).Visible = TrueEnd IfNext itemp = MsgBox("恭喜,扫雷成功!" & vbCrLf & "耗时:" & Mid(Label_Time.Caption, 4) & vbCrLf & "鸣谢:平方X O(∩_∩)O~", vbOKOnly, "成功") End IfContinue_Flag = 0 '提示一次后结束,防止在调用Command_Click事件中重复提示End IfEnd IfCommand_Start.SetFocusEnd SubPrivate Sub Command_Start_Click()If Command_Start.Caption = "开始游戏" ThenCommand_Start.Caption = "重新开始"Continue_Flag = 1Timer1.Enabled = TrueDifficulty = Val(Text_Difficulty.Text) / 100Area_X = Val(Text_X.Text)Area_Y = Val(Text_Y.Text)Area = Area_X * Area_Y'初始化这里进行二次初始化的原因是如果在之前的运行中对字体进行了改变,将有可能造成此处的控件大小发生变化With Picture_show.Left = 200.Top = 200.Width = 750 * 10.Height = 750 * 10.Visible = FalseEnd WithWith Command_Up(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = 200.Top = 200.Width = 750 * 10 / IIf(Area_X > Area_Y, Area_X, Area_Y).FontSize = 25 * (.Width / 750) '会自动缩放,必须先设置了.Height = 750 * 10 / IIf(Area_X > Area_Y, Area_X, Area_Y).Visible = FalseEnd WithWith Command_Up(0).Left = 200.Top = 200.Width = Label_Down(0).Width.Height = Label_Down(0).Height.Visible = FalseEnd WithReDim Near_List(1 To 8)Near_List(1) = 0 - 1 - Area_YNear_List(2) = 0 - 0 - Area_YNear_List(3) = 0 + 1 - Area_YNear_List(4) = 0 - 1Near_List(5) = 0 + 1Near_List(6) = 0 - 1 + Area_YNear_List(7) = 0 - 0 + Area_YNear_List(8) = 0 + 1 + Area_Y'如果在列表中有相等的元素将有可能造成统计雷的数目错误For i = 1 To 8For j = i + 1 To 8If Near_List(i) = Near_List(j) Then Near_List(i) = 0Next jNext iArea_temp = 0For Y = 1 To Area_Y'加载labelFor X = 1 To Area_XArea_temp = Area_temp + 1Load Label_Down(Area_temp)With Label_Down(Area_temp).Left = Label_Down(0).Left + Label_Down(0).Width * ((Area_temp -1) Mod Area_Y).Top = Label_Down(0).Top + Label_Down(0).Height * ((Area_temp -1) \ Area_Y).BackColor = vbGreen.Visible = False.Alignment = 2.Font = .FontBoldEnd With'加载commandLoad Command_Up(Area_temp)With Command_Up(Area_temp)'对列数求余的话就是在这一行第几个了.Left = Command_Up(0).Left + Command_Up(0).Width * ((Area_temp - 1) Mod Area_Y)'整除列数的话可以确定第几行.Top = Command_Up(0).Top + Command_Up(0).Height * ((Area_temp - 1) \ Area_Y).Visible = TrueEnd WithNext XNext YReDim Area_List(1 To Area)For i = 1 To AreaArea_List(i) = iNext i' 随即布雷RandomizeMine_Count = Val(Text_Mine_Count.Text)For i = 1 To Mine_CountCurrent_Mine = Int(Rnd * (UBound(Area_List) - LBound(Area_List) + 1) + 1) '在数组中随机一个,注意此处2个+1的必要性和准确性Label_Down(Area_List(Current_Mine)).BackColor = vbRed '将该位置标记为雷Call Delete_Item(Area_List, Current_Mine) '删除该位置,防止再次标记Next i'检查雷的数目For j = 1 To AreaIf Label_Down(j).BackColor = vbRed ThenLabel_Down(j).Caption = "X"ElseMine_Number = 0For i = 1 To 8'判断控件是否存在If j + Near_List(i) > 0 And j + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(j + Near_List(i)).Left -Label_Down(j).Left) <= Label_Down(j).Width And Abs(Label_Down(j + Near_List(i)).Top -Label_Down(j).Top) <= Label_Down(j).Height Then'判断是否有雷If Label_Down(j + Near_List(i)).BackColor = vbRed ThenMine_Number = Mine_Number + 1End IfEnd IfEnd IfNext iLabel_Down(j).Caption = Mine_NumberEnd IfNext jStart_Time = Now()ElseIf Command_Start.Caption = "重新开始" ThenCall Command_retry_ClickEnd IfEnd SubPrivate Sub Command_Up_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 ThenIf Command_Up(Index).Caption = "" ThenCommand_Up(Index).Caption = "X"Command_Up(Index).Picture = LoadPicture(App.Path + "\pictures\mine.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)ElseIf Command_Up(Index).Caption = "X" ThenCommand_Up(Index).Caption = "?"Command_Up(Index).Picture = LoadPicture(App.Path + "\pictures\Unknown.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)ElseIf Command_Up(Index).Caption = "?" ThenCommand_Up(Index).Caption = ""Command_Up(Index).Picture = LoadPicture("") End IfEnd IfEnd SubPrivate Sub Form_Load()With Picture_show.Left = 200.Top = 200.Width = 750 * 10.Height = 750 * 10.Visible = FalseEnd WithWith Command_Up(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd WithWith Label_Down(0).Left = Picture_show.Left.Top = Picture_show.Top.Width = Picture_show.Width / 10.Height = Picture_show.Height / 10.FontSize = 1 '防止自动缩放.Visible = FalseEnd With'加载计时器Timer1.Enabled = FalseTimer1.Interval = 100'加载滚动条With HScroll_Difficulty.LargeChange = 5.SmallChange = 1.Max = 100.Min = 0.Value = 10End WithWith HScroll_Area_X.LargeChange = 5.SmallChange = 1.Max = 100.Min = 1.Value = 10End WithWith HScroll_Area_Y.LargeChange = 5.SmallChange = 1.Max = 100.Min = 1.Value = 10End WithWith HScroll_Mine_Count.LargeChange = 5.SmallChange = 1.Max = 100.Min = 0.Value = 10End With'由于很多数据不方便处理,索性让其禁用了Text_Difficulty.Enabled = FalseText_Mine_Count.Enabled = FalseText_X.Enabled = FalseText_Y.Enabled = FalseEnd SubPrivate Sub HScroll_Area_X_Change()Text_X.Text = HScroll_Area_X.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_X_Scroll()Text_X.Text = HScroll_Area_X.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_Y_Change()Text_Y.Text = HScroll_Area_Y.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Area_Y_Scroll()Text_Y.Text = HScroll_Area_Y.ValueHScroll_Mine_Count.Max = HScroll_Area_X.Value * HScroll_Area_Y.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Difficulty_Change()Text_Difficulty.Text = HScroll_Difficulty.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Difficulty_Scroll()Text_Difficulty.Text = HScroll_Difficulty.ValueHScroll_Mine_Count.Value = HScroll_Area_X.Value * HScroll_Area_Y.Value / 100 * HScroll_Difficulty.ValueEnd SubPrivate Sub HScroll_Mine_Count_Change()Text_Mine_Count.Text = HScroll_Mine_Count.ValueHScroll_Difficulty.Value = HScroll_Mine_Count.Value / (HScroll_Area_X.Value * HScroll_Area_Y.Value) * 100End SubPrivate Sub HScroll_Mine_Count_Scroll()Text_Mine_Count.Text = HScroll_Mine_Count.ValueHScroll_Difficulty.Value = HScroll_Mine_Count.Value / (HScroll_Area_X.Value * HScroll_Area_Y.Value) * 100End SubPrivate Sub Label_Down_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" And Command_Up(Index + Near_List(i)).Caption <> "?" ThenCommand_Up(Index + Near_List(i)).Picture = LoadPicture("")End IfEnd IfEnd IfNext iEnd SubPrivate Sub label_down_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If LeftAndRight_Flag + Button = 3 Then '双击完成Mine_Number = Val(Label_Down(Index).Caption)Mark_mine_number = 0For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption = "X" ThenMark_mine_number = Mark_mine_number + 1End IfEnd IfEnd IfNext iIf Val(Label_Down(Index).Caption) - Mark_mine_number <= 0 Then '已全部标出,自动点开For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" ThenCall Command_Up_Click(Index + Near_List(i))End IfEnd IfEnd IfNext iElse '如果没有全部标注的话应该显示一下嘛For i = 1 To 8'判断控件是否存在If Index + Near_List(i) > 0 And Index + Near_List(i) <= Area Then'判断是否相邻If Abs(Label_Down(Index + Near_List(i)).Left -Label_Down(Index).Left) <= Label_Down(Index).Width And Abs(Label_Down(Index + Near_List(i)).Top -Label_Down(Index).Top) <= Label_Down(Index).Height Then'判断是否有标记雷If Command_Up(Index + Near_List(i)).Caption <> "X" ThenCommand_Up(Index + Near_List(i)).Picture = LoadPicture(App.Path + "\pictures\xia.gif", , , Command_Up(Index).Width, Command_Up(Index).Height)End IfEnd IfEnd IfNext iEnd IfElseLeftAndRight_Flag = Button'Print LeftAndRight_FlagEnd IfEnd SubPrivate Sub Timer1_Timer()LeftAndRight_Flag = 0End_Time = Now()spend_time = (End_Time - Start_Time) * 10 ^ 5Label_Time.Caption = "时间:" & Format(Int(spend_time) \ (60 * 60), "00") & ":" & Format((Int(spend_time) Mod (60 * 60)) \ 60, "00") & ":" & Format(Int(spend_time) Mod 60, "00") & "." & Format(Int((spend_time - Int(spend_time)) * 1000), "000")End Sub五.软件截图1 2 3。
扫雷游戏代码
扫雷游戏代码standalone; self-contained; independent; self-governed;autocephalous; indie; absolute; unattached; substantive/**/#ifndef BLOCK_H_#define BLOCK_H_#include<QLabel>class QWidget;class Block:public QLabel{Q_OBJECTpublic:explicit Block(bool mine_flag,QWidget*parent=0);void set_number(int number);void turn_over();bool is_mine()const;bool is_turn_over()const;signals:void turn_over(bool is_mine);protected:void mousePressEvent(QMouseEvent*event); private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;};#endif#include""#include<QLabel>#include<QMouseEvent>#include<QPixmap>#include<QWidget>Block::Block(bool mine_flag,QWidget*parent) :QLabel(parent){mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(QPixmap(":/images/"));}void Block::set_number(int number){number_=number;}void Block::turn_over(){if(!turn_over_flag_){turn_over_flag_=true;if(mine_flag_)setPixmap(QPixmap(":/images/"));elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();}}bool Block::is_mine()const{return mine_flag_;}bool Block::is_turn_over()const{return turn_over_flag_;}/*鼠标事件的实现*/void Block::mousePressEvent(QMouseEvent*event){if(event->button()==Qt::LeftButton){if(!turn_over_flag_&&!mark_flag_){turn_over_flag_=true;if(mine_flag_==true){setPixmap(QPixmap(":/images/"));update();emit turn_over(true);}else{setPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();emit turn_over(false);}}}else if(event->button()==Qt::RightButton){if(!turn_over_flag_){if(!mark_flag_){mark_flag_=true;setPixmap(QPixmap(":/images/"));}else{mark_flag_=false;setPixmap(QPixmap(":/images/"));}update();}}QLabel::mousePressEvent(event);}#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#include""#include<QWidget>class QEvent;class QGridLayout;class QObject;class BlockArea:public QWidget{Q_OBJECTpublic:BlockArea(int row,int column,int mine_number,QWidget* parent=0);void set_block_area(int row,int column,intmine_number,int init_flag=false);signals:void game_over(bool is_win);protected:bool eventFilter(QObject*watched,QEvent*event); private slots:void slot_turn_over(bool is_mine);private:int calculate_mines(int x,int y)const;rg(easy_record_time_)),1,1);up_layout->addWidget(new QLabel(easy_record_name_),1,2);up_layout->addWidget(new QLabel(tr("Middle")),2,0);up_layout->addWidget(newQLabel(QString("%1").arg(middle_record_time_)),2,1);up_layout->addWidget(newQLabel(middle_record_name_),2,2);up_layout->addWidget(new QLabel(tr("Hard")),3,0);up_layout->addWidget(newQLabel(QString("%1").arg(hard_record_time_)),3,1);up_layout->addWidget(new QLabel(hard_record_name_),3,2);QPushButton*recount_button=newQPushButton(tr("recount"));QPushButton*close_button=new QPushButton(tr("close"));close_button->setDefault(true);connect(recount_button,SIGNAL(clicked()),&dialog,SLOT(ac cept()));connect(close_button,SIGNAL(clicked()),&dialog,SLOT(reje ct()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(recount_button);bottom_layout->addWidget(close_button);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted){easy_record_time_=middle_record_time_=hard_record_time_= g_no_record_time;easy_record_name_=middle_record_name_=hard_record_name_= g_no_record_name;}}void MainWindow::slot_show_game_toolBar(bool show){if(show)game_toolBar->show();elsegame_toolBar->hide();}void MainWindow::slot_show_statusBar(bool show){if(show)statusBar()->show();elsestatusBar()->hide();}/*游戏的设置容易、中等、困难及自定义*/void MainWindow::slot_standard(QAction*standard_action) {if(standard_action==easy_standard_action){current_standard_=0;row_=9;column_=9;mine_number_=10;}else if(standard_action==middle_standard_action){ current_standard_=1;row_=16;column_=16;mine_number_=40;}else if(standard_action==hard_standard_action){current_standard_=2;row_=16;column_=30;mine_number_=99;}else if(standard_action==custom_standard_action){ QDialog dialog;(tr("set standard"));QSpinBox*row_spinBox=new QSpinBox;row_spinBox->setRange(5,50);row_spinBox->setValue(row_);QSpinBox*column_spinBox=new QSpinBox;column_spinBox->setRange(5,50);column_spinBox->setValue(column_);QSpinBox*mine_spinBox=new QSpinBox;mine_spinBox->setValue(mine_number_);QHBoxLayout*up_layout=new QHBoxLayout;up_layout->addWidget(row_spinBox);up_layout->addWidget(column_spinBox);up_layout->addWidget(mine_spinBox);QDialogButtonBox*dialog_buttonBox=new QDialogButtonBox;dialog_buttonBox->addButton(QDialogButtonBox::Ok);dialog_buttonBox->addButton(QDialogButtonBox::Cancel);connect(dialog_buttonBox,SIGNAL(accepted()),&dialog,SLOT (accept()));connect(dialog_buttonBox,SIGNAL(rejected()),&dialog,SLOT (reject()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(dialog_buttonBox);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted)if(row_spinBox->value()*column_spinBox->value()>mine_spinBox->value()){current_standard_=3;row_=row_spinBox->value();column_=column_spinBox->value();mine_number_=mine_spinBox->value();}}slot_new_game();}/*实现帮助菜单中的关于游戏,及功能*/void MainWindow::slot_about_game(){QString introduction("<h2>"+tr("About Mine Sweepr")+"</h2>"+"<p>"+tr("This game is played by revealing squares of the grid,typically by clicking them with a mouse.If a square containing a mine is revealed,the player loses the game.Otherwise,a digit is revealed in the square,indicating the number of adjacent squares(out of the possible eight)that contain this number is zero then the square appears blank,and the surrounding squares are automatically also revealed.By using logic,the player can in many instances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed,or mine-filled,in which they can be marked as such(which is effected by right-clicking the square and indicated by a flag graphic).")+"</p>"+"<p>"+tr("This program is free software;you can redistribute it and/or under the terms of the GNU General Public License as published by the Software Foundation;either version3of the License,or(at your option)any later version.")+"</p>"+"<p>"+tr("Please see")+"<a href="+tr("for an overview of GPLv3licensing")+"</p>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author:")+g_software_author+"</br>");QMessageBoxmessageBox(QMessageBox::Information,tr("About Mine Sweeper"),introduction,QMessageBox::Ok);();}/*游戏的判断,及所给出的提示做出判断*/void MainWindow::slot_game_over(bool is_win){();QString name;if(is_win){switch(current_standard_){case0:if(time_label->text().toInt()<easy_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){easy_record_time_=time_label->text().toInt();easy_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case1:if(time_label->text().toInt()<middle_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){middle_record_time_=time_label->text().toInt();middle_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case2:if(time_label->text().toInt()<hard_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){hard_record_time_=time_label->text().toInt();hard_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;default:QMessageBox::information(this,tr("Result"),tr("Y ou win"));}}else{QMessageBox::information(this,tr("Result"),tr("You lose"));}}/*定时器的设置*/void MainWindow::slot_timer(){time_label->setText(QString("%1").arg()/1000));}/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/void MainWindow::read_settings(){QSettings settings;("MainWindow");resize("size").toSize());move("pos").toPoint());bool show_game_toolBar=("showGameToolBar").toBool();show_game_toolBar_action->setChecked(show_game_toolBar);slot_show_game_toolBar(show_game_toolBar);bool show_statusBar=("showStatusBar").toBool();show_statusBar_action->setChecked(show_statusBar);slot_show_statusBar(show_statusBar);();("GameSetting");current_standard_=("current_standard").toInt();switch(current_standard_){case0:easy_standard_action->setChecked(true);break;case1:middle_standard_action->setChecked(true);break;case2:hard_standard_action->setChecked(true);break;case3:custom_standard_action->setChecked(true);break;default:;}row_=("row").toInt()==09:("row").toInt();column_=("column").toInt()==09:("column").toInt();mine_number_=("mine_number").toInt()==010:("mine_number" ).toInt();();("Rank");easy_record_time_=("easy_record_time").toInt()==0g_no_re cord_time:("easy_record_time").toInt();middle_record_time_=("middle_record_time").toInt()==0g_n o_record_time:("middle_record_time").toInt();hard_record_time_=("hard_record_time").toInt()==0g_no_re cord_time:("hard_record_time").toInt();easy_record_name_=("easy_record_name").toString()==""g_n o_record_name:("easy_record_name").toString();middle_record_name_=("middle_record_name").toString()==" "g_no_record_name:("middle_record_name").toString();hard_record_name_=("hard_record_name").toString()==""g_n o_record_name:("hard_record_name").toString();();}void MainWindow::write_settings(){QSettings settings;("MainWindow");("size",size());("pos",pos());("showGameToolBar",show_game_toolBar_action->isChecked());("showStatusBar",show_statusBar_action->isChecked());();("GameSetting");("current_standard",current_standard_);("row",row_);("column",column_);("mine_number",mine_number_);();("Rank");("easy_record_time",easy_record_time_);("middle_record_time",middle_record_time_);("hard_record_time",hard_record_time_);("easy_record_name",easy_record_name_);("middle_record_name",middle_record_name_);("hard_record_name",hard_record_name_);();}/*菜单栏里图片的显示*/void MainWindow::create_actions(){new_game_action=new QAction(QIcon(":/images/"),tr("New Game"),this);new_game_action->setShortcut(QKeySequence::New);connect(new_game_action,SIGNAL(triggered()),this,SLOT(sl ot_new_game()));rank_action=newQAction(QIcon(":/images/"),tr("Rank"),this);connect(rank_action,SIGNAL(triggered()),this,SLOT(slot_r ank()));exit_action=newQAction(QIcon(":/images/"),tr("Exit"),this);exit_action->setShortcut(QKeySequence::Quit);connect(exit_action,SIGNAL(triggered()),this,SLOT(close( )));show_game_toolBar_action=new QAction(tr("Show Game Tool Bar"),this);show_game_toolBar_action->setCheckable(true);connect(show_game_toolBar_action,SIGNAL(toggled(bool)),t his,SLOT(slot_show_game_toolBar(bool)));show_statusBar_action=new QAction(tr("Show Status Bar"),this);show_statusBar_action->setCheckable(true);connect(show_statusBar_action,SIGNAL(toggled(bool)),this ,SLOT(slot_show_statusBar(bool)));easy_standard_action=newQAction(QIcon(":/images/"),tr("Easy"),this);easy_standard_action->setCheckable(true);middle_standard_action=newQAction(QIcon(":/images/"),tr("Middle"),this);middle_standard_action->setCheckable(true);hard_standard_action=newQAction(QIcon(":/images/"),tr("Hard"),this);hard_standard_action->setCheckable(true);custom_standard_action=newQAction(QIcon(":/images/"),tr("Custom"),this);custom_standard_action->setCheckable(true);standard_actionGroup=new QActionGroup(this);standard_actionGroup->addAction(easy_standard_action);standard_actionGroup->addAction(middle_standard_action);standard_actionGroup->addAction(hard_standard_action);standard_actionGroup->addAction(custom_standard_action);connect(standard_actionGroup,SIGNAL(triggered(QAction*)) ,this,SLOT(slot_standard(QAction*)));about_game_action=newQAction(QIcon(":/images/"),tr("About Game"),this);connect(about_game_action,SIGNAL(triggered()),this,SLOT( slot_about_game()));about_qt_action=new QAction(QIcon(":/images/"),tr("About Qt"),this);connect(about_qt_action,SIGNAL(triggered()),qApp,SLOT(ab outQt()));}/*菜单栏的创建*/void MainWindow::create_menus(){game_menu=menuBar()->addMenu(tr("Game"));game_menu->addAction(new_game_action);game_menu->addSeparator();game_menu->addAction(rank_action);game_menu->addSeparator();game_menu->addAction(exit_action);setting_menu=menuBar()->addMenu(tr("Setting"));setting_menu->addAction(show_game_toolBar_action);setting_menu->addAction(show_statusBar_action);setting_menu->addSeparator();setting_menu->addAction(easy_standard_action);setting_menu->addAction(middle_standard_action);setting_menu->addAction(hard_standard_action);setting_menu->addAction(custom_standard_action);help_menu=menuBar()->addMenu(tr("Help"));help_menu->addAction(about_game_action);help_menu->addAction(about_qt_action);}void MainWindow::create_game_toolBar(){game_toolBar=addToolBar(tr("Game Tool Bar"));game_toolBar->setFloatable(false);game_toolBar->setMovable(false);game_toolBar->addAction(new_game_action);game_toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); }void MainWindow::create_statusBar(){time_label=new QLabel;statusBar()->addPermanentWidget(time_label);statusBar()->addPermanentWidget(newQLabel(tr("second")));}#include""#include<QApplication>#include<QTranslator>int main(int argc,char*argv[]){QApplication app(argc,argv);QTranslator translator;(":/");(&translator);MainWindow window;();return();}。
VB做扫雷游戏毕业设计
编号毕业论文题目用VB实现一个简单的游戏学生姓名学号学院专业班级指导教师用VB实现一个简单的游戏摘要当前,随着计算机应用及网络的的深入普及,越来越多的政府部门、厂矿企业、学校等单位及个人都拥有了自己的计算机,计算机不仅在工作方面给人们以很大的帮助,还给人们带来了多种的娱乐享受.,操作系统自带的游戏也起到了很大的娱乐作用,而且它的开发成本小,所以小游戏的开发在这种形势下就应运而生,它的开发主要采用VC++ ,VB 等编程语言.本设计采用VB编写,VB一直以来被认为有以下优缺点:优点是上手快、开发效率高;缺点是能力有限,运行效率低.这正是有些软件把VB做为首选语言,而有些软件肯定不会用VB做的原因.而很多VC,DELPHI的程序员都认为VB里搞开发不自由.的确,简单和功能强大这两者本身就是一对矛盾.扫雷游戏是操作系统中的一个主要的益智类游戏. 用户对鼠标进行操作,单击鼠标左键为踩雷,单击鼠标左键在标记,疑问,空白三种状态中循环,同时单击鼠标左右键为踩单击点在内的周围九格内所有没有标记为已标记的所有格子.同时程序从你单击第一次时开始计时,到胜利或引爆地雷终结.结束后单击开始按钮重新开始游戏.这款游戏实现了开发人类大脑思维的目的.关键词VB,扫雷游戏Realizes a simple game with VBAbstractFront, is applied along with the calculator and network of of the thorough universality, more and more governments section, factory mineral business enterprise, school...etc. unit and individual all owned own calculator, the calculator is not only to give people in the aspects of working with the very big help, returning to people brought various amusements enjoys., the operate system also rose from the game that take very big amusement function, and it of the cost of development is small, so the development of the get-away drama under this kind of situation emerge with the tide of the times, it of main adoption in development VC++, VB etc. plait distance language.This design adoption VB plait writes, the VB hases been passing for to have since then below merit and shortcoming:The advantage is to undertakes quick, development the efficiency is high;The weakness is an ability limited, circulate the efficiency low.This exactly some softwares is used as a reason for choose the language, but some softwares is affirmative and do with the VB of VBs.And a lot of VCs, the DELPHI procedure member thinks the VB in make the development not free. Really, it is simple to is strong and big with the function this both oneself is an one rightness antinomy.The mine clearance game is an operate system inside of a grows in wisdom a game primarily. The customer proceeds the operation to the rat mark, the left key in mark in rat in single shot is for trampling the thunder, the single shot rat marks the left key in marking, question, blank three kinds of appearances circulating, the single shot rat marks or so key as to trample at the same time the single shot orders the surroundings in inside nine the marking had in spaceses are already all blanks of the marking.At the same time procedure is from you single shot first time hour starts accounting, arrive victory or set off the land mine end.Ending the single shot in empress starts pressing button restarting the game.This game realizes the purpose of the human brain in development thought.Key words:VB,The mine clearance game目录1引言 (4)2 Visual Basic概况 (5)2.1 VB简介 (5)2.2 本设计运用的技术 (5)3可行性分析 (6)4总体设计 (7)4.1设计构想 (7)4.2流程规划 (7)5详细设计 (9)5.1画面规划 (9)5.2 设计过程 (10)5.2.1 添加菜单 (10)5.2.2 自定义窗体设计 (10)5.2.3 关于窗体设计 (15)5.3 运行界面 (15)6系统测试 (17)结束语 (18)参考文献 (19)附录 (20)1引言本论文研究的是以Visual Basic 6.0为开发环境,设计并开发一款扫雷游戏,其功能类似于Windows操作系统自带的扫雷游戏。论文首先介绍了制作游戏的整体思路及整个游戏设计的流程规划,然后介绍了雷区的布置及地雷随机产生的实现方法;重点介绍了在游戏过程中各事件的处理,其中又以鼠标事件和清除未靠近地雷区方块这两方面最为重要,鼠标事件是利用鼠标所发出的信息了解使用者的意图,进而做出相对应的动作,而清除未靠近地雷区方块由于引进了“递归”这个概念而使其简单化。扫雷游戏是WINDOWS系统自带的一个娱乐性的小游戏,在玩扫雷游戏的过程中也可以很好的培养耐心和细心,同时在无聊得时候可以用以休闲娱乐,如果我们自己也能动手做一个小程序来实现这个功能,自然能乐在其中。
Vb扫雷课程设计报告
Vb扫雷课程设计报告姓名:班级:学号:难度:☆☆☆☆知识点:控件数组绘图菜单文本文件的读写多模块程序设计资源文件的使用1.题目“扫雷”是windows下的经典小游戏。
如下图所示,一个由M行N列小方格组成的雷区影藏着K颗地雷(M N和K可以事先设置)。
用户根据判断确定哪个方格有雷,哪个方格无雷。
使用鼠标左键单击无雷方格将其翻开会在该方格内显示一个数字(1~8),该数字表示这个方格周围相邻的8个方格内共有多少克雷。
如果左键单击方格内未显示数字,表示方格周围没有雷,程序自动将周围的方格翻起直至遇到周围有雷的方格为止。
在方格内数字的提示下,用户判断某个位置有雷,使用鼠标右键单击将有雷方格标记出来(显示小红旗)当所有有雷的方格打上了标志,所有无雷的方格都被翻起,表明扫雷成功。
如果中途使用左键单击了有雷方格,则被“炸死”,扫雷失败。
功能:(1)启动程序后,显示初始界面,游戏共有3种预定义的难度级别,分别是初级(9行9列10颗雷),中级(16行16列40颗雷)和高级(30行16列99颗雷),通过“游戏”菜单中的菜单项可以选择不同难度。
使用自定义菜单命令打开自定义对话框,可以自行定义雷区的行数,列数及雷数。
(2)雷区正上方是一个“小脸”图标,不同状态下,小脸显示出微笑,惊讶,痛苦和胜利等表情。
任何情况下单击小脸,可以重新开始游戏。
笑脸左边以LED(发光二极管)方式显示了当前雷区中未被标记出来的雷数,右面是游戏进行的秒数。
计时是在用户第一次单机雷区的方块时开始的。
(3)是用右键单击一个方格将其标记为有雷(插上小旗)之后,不能再使用左键单击它,但可以再用右键单击。
右键再单机一次方格上显示“?”号(表示暂时不确定),再单击第二次,恢复为普通样子。
使用左键可以单机除了标记为有雷之外的所有未翻开的方格(包括标记为“?”号的)(4)如果一个方格内显示的数字为N(1~8)且其周围已有N个方格被标记为有雷,这时可以同时单击鼠标左键和右键将方格周围所有的其它格子翻开。
VB 扫雷游戏程序
5.2.3 关于窗体设计.......................................................................................... 11 5.4 运行界面.......................................................................................................... 11 6 系统测试................................................................................................................... 13 结束语.......................................................................................................................... 14 参考文献...................................................................................................................... 16 附录.............................................................................................................................. 17
3
唐山学院课程设计 及右键,则 Button 值为 1+2=3. 在本游戏中,利用右键的 MouseDown 事件标示“已标示”方块,利用左键的 MouseDown 事件,标示使用者准备按下的方块,利用左键的 MouseUp 事件确定翻 开的方块。 4. ImageList 组件 利用 Imagelist 可以存储显示方块的图形[2]。将组件布置于窗体上后编辑组 件属性,通过【插入图片】按钮和【删除图片】按钮即可编辑组件内所存放的图 片。如图 1 所示。因为一个 Imagelist 组件内必须存储大小相同的图片,所在这 款游戏设计中,显示的图片共分为三个部分:游戏状态图形、数字计数图形和地 雷区标示图形。
扫雷游戏开发代码
}
public void setIsMark(boolean m) {
isMark=m;
}
}
/*(2)雷区显示*/
import javax.swing.*;
import java.awt.*;
}
else {
int n=block.getAroundMineNumber();
setBackground(new Color(0,0,122));
if(n>=1)
{
isMine=b;
}
public void setMineIcon(ImageIcon icon){
mineIcon=icon;
}
public ImageIcon getMineicon(){
return mineIcon;
if(n==2)////
blockNameOrIcon.setForeground(new Color(0,0,255));////////
if(n==3)
blockNameOrIcon.setForeground(new Color(0,255,0));///
}
}
block[i][j].setIsOpen(false);
block[i][j].setIsMark(false);
block[i][j].setName(""+mineNumber);
blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);
blockCover=new JButton();
Vb扫雷程序代码
Private objMine As New clsWinMinePrivate Sub Form_Load()Set objMine、frmDisplay = MeEnd SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) ' 判断单击的就是哪个区域objMine、BeginHitTest Button, x, yEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) ' 判断当鼠标左键按下的时候鼠标指针在哪个区域objMine、TrackHitTest Button, x, yEnd SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) ' 判断释放鼠标左键的时候鼠标指针在哪个区域objMine、EndHitTest Button, x, yEnd SubPrivate Sub mnuBeginner_Click()mnuBeginner、Checked = TruemnuIntermediate、Checked = FalsemnuExpert、Checked = FalsemnuCustom、Checked = False' 初级模式objMine、SetMineFieldDimension 8, 8, 10, FalseobjMine、mblnNewGame = TrueEnd SubPrivate Sub mnuCustom_Click()mnuBeginner、Checked = FalsemnuIntermediate、Checked = FalsemnuExpert、Checked = FalsemnuCustom、Checked = True' 中级模式objMine、GetMineFieldDimensions frmCustomDlgfrmCustomDlg、Show 1' 如果按ESC键,则退出If frmCustomDlg、mblnEscape Then Exit SubobjMine、SetMineFieldDimension Val(frmCustomDlg、txtRows), Val(frmCustomDlg、txtColumns), Val(frmCustomDlg、txtMines), True' 卸载隐藏的对话框Unload frmCustomDlg' 做好准备开始新游戏objMine、mblnNewGame = TrueEnd SubPrivate Sub mnuExit_Click()' 调用terminate事件Set objMine = Nothing' 退出游戏EndEnd SubPrivate Sub mnuExpert_Click()mnuBeginner、Checked = FalsemnuIntermediate、Checked = FalsemnuExpert、Checked = TruemnuCustom、Checked = False' 高级模式objMine、SetMineFieldDimension 16, 30, 100, False objMine、mblnNewGame = TrueEnd SubPrivate Sub mnuIntermediate_Click()mnuBeginner、Checked = FalsemnuIntermediate、Checked = TruemnuExpert、Checked = FalsemnuCustom、Checked = False' 自定义模式objMine、SetMineFieldDimension 16, 16, 40, FalseobjMine、mblnNewGame = TrueEnd SubPrivate Sub mnuNew_Click()' 开始新游戏objMine、NewGameEnd SubOption Explicit' 判断左键就是否按下Private Const LEFT_BUTTON As Byte = 1' 标记没有地雷的区域Private Const NONE As Byte = 0' 标记就是否触雷Private Const MINE As Byte = 243' 已经清除地雷的区域Private Const BEEN As Byte = 244' 标记确定已经有地雷的区域Private Const FLAGGED As Byte = 2' 标记可疑区域Private Const QUESTION As Byte = 1' 最大、最小行列数Private Const MIN_MINES As Byte = 10Private Const MAX_MINES As Byte = 99Private Const MIN_ROWS As Integer = 8Private Const MAX_ROWS As Integer = 24Private Const MIN_COLS As Integer = 8Private Const MAX_COLS As Integer = 36' 宽Private Const mintButtonWidth As Byte = 16' 高Private Const mintButtonHeight As Byte = 16' 总地雷数Private mbytNumMines As Byte' 尚未标记的地雷数Private mbytCorrectHits As Byte' 已经标记出的雷数(包括错误的)Private mbytTotalHits As Byte' 不同等级游戏的总行列数Private mintRows As IntegerPrivate mintCols As IntegerPrivate mintRow As IntegerPrivate mintCol As Integer' 标记就是否开始新游戏Public mblnNewGame As Boolean' 标记一个鼠标单击事件正在进行Private mblnHitTestBegun As BooleanPrivate mfrmDisplay As FormPrivate mbytMineStatus() As BytePrivate mbytMarked() As BytePrivate mbytMineLocations() As BytePrivate mcolWrongLocations As New CollectionPublic Sub BeginHitTest(intButton As Integer, intX As Single, intY As Single) ' 如果当前游戏结束则开始新的游戏If mblnNewGame ThenNewGameEnd IfmblnHitTestBegun = True' 根据位图计算栅格大小intX = Int(intX / mintButtonWidth)intY = Int(intY / mintButtonHeight)' 退出If intX >= mintCols _Or intY >= mintRows _Or intX < 0 _Or intY < 0 ThenExit SubEnd IfmintCol = intX * mintButtonWidthmintRow = intY * mintButtonHeightIf mbytMineStatus(intY, intX) >= BEEN Then Exit SubDim blnLeftDown As BooleanblnLeftDown = (intButton And LEFT_BUTTON) > 0' 如果左键单击If blnLeftDown Then' 如果该区域已经清除干净,则单击无效If mbytMarked(intY, intX) >= FLAGGED Then Exit SubIf mbytMarked(intY, intX) = QUESTION ThenmfrmDisplay、imgPressed、Visible = FalsemfrmDisplay、imgQsPressed、Visible = FalsemfrmDisplay、imgQsPressed、Left = mintColmfrmDisplay、imgQsPressed、Top = mintRowmfrmDisplay、imgQsPressed、Visible = TrueElsemfrmDisplay、imgQsPressed、Visible = FalsemfrmDisplay、imgPressed、Visible = FalsemfrmDisplay、imgPressed、Left = mintColmfrmDisplay、imgPressed、Top = mintRowmfrmDisplay、imgPressed、Visible = TrueEnd IfElse' 如果右键单击Dim Msg As StringDim CRLF As StringCRLF = Chr$(13) & Chr$(10)Select Case mbytMarked(intY, intX)Case NONE:If mbytTotalHits = mbytNumMines ThenMsg = "不能标记更多的雷!" & CRLFMsg = Msg & "一个或多个雷标记错误。
VB程序设计之扫雷程序
代码:Dim MAP_X As Integer '地图的X轴长Dim MAP_Y As Integer '地图的Y轴长Dim MINE_NUM As Integer '地雷数Dim MINE_NUM_LABEL As Integer '地雷标记数Dim MINE_NUM_CLEAR As Integer '方块清除数Dim TIME_COUNTER As Integer '时间计数Dim isStart As Boolean '游戏是否已进行Dim isDie As Boolean '游戏是否呈结束状态Dim isFinal As Boolean '游戏是否完成Dim mapArr() As Integer '地雷区地雷分布情形Dim mapFaceArr() As Integer '地雷区方块探测情形'窗体加载Private Sub Form_Load()Randomize '初始化随机数产生器'加载为地雷区方块图片Set iMine(0).Picture = ilMap.ListImages(9).Picture'参数初始设定Call setInit(GetSetting("Mine", "Level", "1", "0"))'计数区配置Call setCounterInit'地雷区配置Call setMapInit'设定窗体大小Call setFormSizeEnd Sub'地雷区方块按下事件Private Sub iMine_MouseDown(index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)Dim xxx As Integer '换算组件所代表的坐标值Dim yyy As IntegerIf isFinal = False And isDie = False Thenxxx = index Mod MAP_X '标示动作图片的坐标yyy = index \ MAP_XIf Button = 1 Then '鼠标左键If mapFaceArr(xxx, yyy) = 1 ThenCall setMineImage(index, 10)Set iState.Picture = ilSmile.ListImages(3).Picture End IfElseIf Button = 2 Then '鼠标右键If mapFaceArr(xxx, yyy) = 1 Then '未探测mapFaceArr(xxx, yyy) = 2MINE_NUM_LABEL = MINE_NUM_LABEL + 1Call setMineImage(index, 13)Call setNowMinesElseIf mapFaceArr(xxx, yyy) = 2 Then '标示 mapFaceArr(xxx, yyy) = 3MINE_NUM_LABEL = MINE_NUM_LABEL - 1Call setMineImage(index, 14)Call setNowMinesElseIf mapFaceArr(xxx, yyy) = 3 Then '疑似 mapFaceArr(xxx, yyy) = 1Call setMineImage(index, 9)End IfEnd IfEnd IfEnd Sub'地雷区方块松开事件Private Sub iMine_MouseUp(index As Integer, _Button As Integer, _Shift As Integer, _x As Single, _y As Single) Dim xxx As Integer '换算组件所代表的坐标值Dim yyy As IntegerDim rX As Integer '随机数产生坐标值Dim rY As IntegerDim cMINE_NUM As Integer '地雷数暂存值'判断地雷区是否处于可动作状态If isDie = False And isFinal = False Thenxxx = index Mod MAP_X '标示动作图片的坐标yyy = index \ MAP_X'判断为左键且该方块为未探测状态If Button = 1 And mapFaceArr(xxx, yyy) = 1 Then'判断游戏是否等待启动If isStart = False ThencMINE_NUM = MINE_NUMisStart = TrueTimer1.Enabled = True'布置地雷Do While cMINE_NUM > 0rX = Rnd() * (MAP_X - 1)rY = Rnd() * (MAP_Y - 1)'判断数组中(rX,rY)为非地雷及'非点选的方块坐标时If mapArr(rX, rY) <> 9 And _Not (rX = xxx And rY = yyy) ThencMINE_NUM = cMINE_NUM - 1mapArr(rX, rY) = 9'地雷外围数字各加一For xx = rX - 1 To rX + 1For yy = rY - 1 To rY + 1If xx >= 0 And xx <= MAP_X And _yy >= 0 And yy <= MAP_Y ThenIf mapArr(xx, yy) <> 9 And _Not (xx = rX And yy = rY) Then mapArr(xx, yy) = mapArr(xx, yy) + 1 End IfEnd IfNext yyNext xxEnd IfLoopEnd If'将该方块坐标值设为0,表示为翻开状态mapFaceArr(xxx, yyy) = 0'选择地雷区数组判断该坐标Select Case mapArr(xxx, yyy)Case 0 '空白Call doClear(xxx, yyy, index)Call setMineImage(index, 10)Case 1 To 8 '数字MINE_NUM_CLEAR = MINE_NUM_CLEAR + 1Call setMineImage(index, mapArr(xxx, yyy))Case 9 '地雷isDie = TrueTimer1.Enabled = FalseCall showMineCall setMineImage(index, 12)End Select'检查是否只剩下地雷未探测If MAP_X * MAP_Y - MINE_NUM_CLEAR = MINE_NUM Then isFinal = TrueEnd IfEnd If'设定状态图示Call setStateImageEnd IfEnd Sub'清除未靠近地雷的区块Private Sub doClear(ByVal x As Integer, ByVal y As Integer, ByVal index As Integer)MINE_NUM_CLEAR = MINE_NUM_CLEAR + 1For xx = x - 1 To x + 1For yy = y - 1 To y + 1If xx >= 0 And xx <= MAP_X And _yy >= 0 And yy <= MAP_Y ThenIf Not (xx = x And yy = y) ThenIf mapFaceArr(xx, yy) = 1 And _mapArr(xx, yy) >= 0 And _mapArr(xx, yy) <= 8 ThenmapFaceArr(xx, yy) = 0Select Case mapArr(xx, yy)Case 0Call setMineImage((yy * MAP_X + xx), 10) Call doClear(xx, yy, (yy * MAP_X + xx)) Case 1 To 8MINE_NUM_CLEAR = MINE_NUM_CLEAR + 1Call setMineImage((yy * MAP_X + xx), mapArr(xx, yy))End SelectEnd IfEnd IfEnd IfNext yyNext xxEnd Sub'按下表情图案时Private Sub iState_MouseDown(Button As Integer, Shift As Integer,x As Single, y As Single)If Button = 1 ThenSet iState.Picture = ilSmile.ListImages(2).PictureEnd IfEnd Sub'松开表情图案时Private Sub iState_MouseUp(Button As Integer, Shift As Integer,x As Single, y As Single)If Button = 1 ThenSet iState.Picture = ilSmile.ListImages(1).PictureForm_LoadEnd IfEnd Sub'设定状态图示Private Sub setStateImage()'状态判断If isDie = False ThenIf isFinal = True ThenSet iState.Picture = ilSmile.ListImages(5).PictureCall showFinal '显示过关ElseSet iState.Picture = ilSmile.ListImages(1).PictureEnd IfElseSet iState.Picture = ilSmile.ListImages(4).PictureEnd IfEnd Sub'设定方块图片Private Sub setMineImage(m_index As Integer, il_index As Integer) Set iMine(m_index).Picture = ilMap.ListImages(il_index).Picture End Sub'初始设定Private Sub setInit(index As Integer)isStart = FalseisFinal = FalseisDie = FalseTimer1.Enabled = FalseTIME_COUNTER = 0MINE_NUM_LABEL = 0MINE_NUM_CLEAR = 0Select Case indexCase 0MAP_X = 9MAP_Y = 9MINE_NUM = 10Case 1MAP_X = 16MAP_Y = 16MINE_NUM = 40Case 2MAP_X = 30MAP_Y = 16MINE_NUM = 99Case 3MAP_X = GetSetting("Mine", "SET", "1", "30")MAP_Y = GetSetting("Mine", "SET", "2", "24")MINE_NUM = GetSetting("Mine", "SET", "3", "50") End SelectFor i = 0 To 3muGame_Level(i).Checked = FalseNext imuGame_Level(index).Checked = TrueSaveSetting "Mine", "Level", "1", index '储存选择End Sub'计数区初始化Private Sub setCounterInit()Dim wid As IntegerDim hig As Integer'地雷计数显示Call setNowMines '显示现在地雷数iMC1.Move 0, 0iMC2.Move iMC1.Left + iMC1.Width, 0iMC3.Move iMC2.Left + iMC2.Width, 0'设定地雷计数框大小wid = pMC.Width - pMC.ScaleWidthhig = pMC.Height - pMC.ScaleHeightpMC.Width = iMC1.Width * 3 + widpMC.Height = iMC1.Height + hig'时间计数显示Call setNowTimeriTC1.Move 0, 0iTC2.Move iTC1.Left + iTC1.Width, 0iTC3.Move iTC2.Left + iTC2.Width, 0'设定时间计数框大小wid = pTC.Width - pTC.ScaleWidthhig = pTC.Height - pTC.ScaleHeightpTC.Width = iTC1.Width * 3 + widpTC.Height = iTC1.Height + hig'计数区配置wid = pCounter.Width - pCounter.ScaleWidthhig = pCounter.Height - pCounter.ScaleHeightpCounter.Move 150, _150,_iMine(0).Width * MAP_X + wid,_pMC.Height + 300 + hig'设定组件位置Set iState.Picture = ilSmile.ListImages(1).PictureiState.Move (pCounter.ScaleWidth - iState.Width) / 2, 150 pMC.Move 150, 150pTC.Move pCounter.ScaleWidth - 150 - pTC.Width, 150End Sub'地图初始化Private Sub setMapInit()pMap.Visible = False'重新定义地图数组大小ReDim mapArr(MAP_X, MAP_Y)ReDim mapFaceArr(MAP_X, MAP_Y)'地图中小方块长宽Dim iWid As IntegerDim iHig As IntegeriWid = iMine(0).WidthiHig = iMine(0).Height'设定地图数组数据'并加载表示地图数据之图片For y = 0 To MAP_Y - 1For x = 0 To MAP_X - 1mapArr(x, y) = 0mapFaceArr(x, y) = 1Call setMineImage(y * MAP_X + x, 9)iMine(y * MAP_X + x).Move x * iWid, y * iHigiMine(y * MAP_X + x).Visible = TrueiMine(y * MAP_X + x).ZOrderIf iMine.Count < (MAP_X * MAP_Y) Then Load iMine(iMine.Count)End IfNext xNext y'设定地图区大小位置Dim wid As IntegerDim hig As Integerwid = pMap.Width - pMap.ScaleWidthhig = pMap.Height - pMap.ScaleHeightpMap.Move 150, _pCounter.Height + 300, _MAP_X * iWid + wid, _MAP_Y * iHig + higpMap.Visible = TrueEnd Sub'设定窗体大小Private Sub setFormSize()Dim wid As IntegerDim hig As Integerwid = Me.Width - Me.ScaleWidthhig = Me.Height - Me.ScaleHeightMe.Width = pMap.Width + 300 + widMe.Height = pMap.Height + pCounter.Height + 450 + hig End Sub'显示现在地雷数Private Sub setNowMines()Dim intNM As IntegerintNM = MINE_NUM - MINE_NUM_LABELIf intNM < 0 Then intNM = 0Set iMC1.Picture = _ilCounter.ListImages((intNM Mod 1000) \ 100 + 1).PictureSet iMC2.Picture = _ilCounter.ListImages((intNM Mod 100) \ 10 + 1).PictureSet iMC3.Picture = _ilCounter.ListImages((intNM Mod 10) + 1).PictureEnd Sub'显示现在经过秒数Private Sub setNowTimer()Set iTC1.Picture = _ilCounter.ListImages((TIME_COUNTER Mod 1000) \ 100 + 1).PictureSet iTC2.Picture = _ilCounter.ListImages((TIME_COUNTER Mod 100) \ 10 + 1).Picture Set iTC3.Picture = _ilCounter.ListImages((TIME_COUNTER Mod 10) + 1).PictureEnd Sub'显示地雷Private Sub showMine()For i = 0 To MAP_XFor j = 0 To MAP_Y'显示所有地雷If mapArr(i, j) = 9 ThenCall setMineImage((j * MAP_X + i), 11)End If'检查标示错误之方块If mapFaceArr(i, j) = 2 And mapArr(i, j) <> 9 ThenCall setMineImage((j * MAP_X + i), 15)End IfNext jNext iEnd Sub'显示完成Private Sub showFinal()For x = 0 To MAP_X - 1For y = 0 To MAP_Y - 1If mapArr(x, y) = 9 ThenCall setMineImage((y * MAP_X + x), 13)End IfNext yNext xMINE_NUM_LABEL = MINE_NUMCall setNowMinesMsgBox "您花了 " & TIME_COUNTER & " 秒完成地雷标示。
VB.NET扫雷游戏
目录问题描述 (1)需求分析 (1)程序规划 (3)程序流程图 (3)概要设计 (5)一、界面设计 (5)1、主界面MainForm (5)2、选项框Options (7)3、统计信息HighScore (8)4、帮助框体Help (9)5、关于AboutBox (9)详细设计 (10)1、函数设计 (10)MainForm窗体: (10)MainForm函数调用关系图: (17)Options窗体: (17)2、程序性能分析 (19)测试分析 (20)一、基本功能测试 (20)1、游戏功能 (20)2、设置功能 (24)3、帮助功能 (29)二、白盒测试 (31)输入错误测试 (31)三、测试结果 (34)问题描述编写一个扫雷程序,完成基本的扫雷游戏功能。
需求分析用户打开游戏界面。
游戏界面有两下拉菜单,一个是游戏菜单,另一个是帮助菜单。
界面中间是矩阵方格,下方是计时器和雷计数器,分别显示扫雷用时和未扫出雷的个数。
开始扫雷时,用户无论第一次点击哪个格子都不会是雷。
之后用户鼠标左键单击小格,是雷就扫雷失败,失败就显示所有雷的位置;不是雷且是数字就显示数字,没有数字就打开周围的空格直到周围(横向纵向的四个格子,不包括对角线)是数字。
鼠标右键点击小格标注小红旗,如果不确定就点击两次右键标注问号,再点一次还原。
标注红旗则表示用户标注此处为雷(不管是否正确),问号用于给用户标注不确定的区域,对游戏进程无影响。
标注红旗的格子无法用左键打开,问号格子可以。
左右键同时点中一个数字且数字周围的红旗数等于数字则将其他的小格全部打开,若红旗数大于或小于数字则将其他的小格闪动提示用户无法打开。
如果旗子插错则导致打开含雷的区域,游戏失败。
若将所有雷标出则提示游戏通关,若超过该难度前十记录则提示用户保存记录。
游戏设有计时器,按秒计算,最高显示999秒。
游戏显示剩余雷数,此显示总雷数减去用户插的小旗数。
在游戏菜单中设有“新游戏”、“统计信息”、“选项”、“退出”,四个功能项。
扫雷的课程设计完整代码
扫雷的课程设计完整代码一、课程目标知识目标:1. 学生能理解扫雷游戏的规则及基本算法。
2. 学生能掌握计算机编程中条件语句和循环语句的应用。
3. 学生能了解二维数组在扫雷游戏中的使用。
技能目标:1. 学生能运用所学知识,编写出完整的扫雷游戏代码。
2. 学生通过实践操作,提高问题分析、程序设计和调试的能力。
情感态度价值观目标:1. 学生培养对计算机编程的兴趣,激发学习动力。
2. 学生在合作学习中,培养团队协作精神和沟通能力。
3. 学生通过解决实际问题,体会编程带来的成就感,增强自信心。
课程性质:本课程为信息技术课程,旨在通过实践操作,使学生掌握编程基本技能。
学生特点:六年级学生,具备一定的计算机操作基础,对编程有一定了解,好奇心强,喜欢探索新事物。
教学要求:结合学生特点,注重实践操作,引导学生在探究中发现问题、解决问题,提高编程能力。
将课程目标分解为具体的学习成果,以便于后续教学设计和评估。
二、教学内容1. 扫雷游戏规则介绍:讲解扫雷游戏的基本规则,使学生理解游戏的核心玩法。
2. 编程语言基础:复习巩固编程语言中的基本语法,如变量、数据类型、运算符等。
3. 二维数组的应用:介绍二维数组在扫雷游戏中的使用,包括数组的声明、初始化及访问。
4. 条件语句和循环语句:讲解条件语句(如if-else)和循环语句(如for、while)在扫雷游戏中的具体应用。
5. 游戏逻辑设计:分析扫雷游戏的逻辑,引导学生设计程序流程,编写游戏代码。
6. 编程实践:根据教学进度,组织学生进行编程实践,编写并调试扫雷游戏代码。
教学内容安排和进度:第一课时:扫雷游戏规则介绍,二维数组的应用。
第二课时:条件语句和循环语句在扫雷游戏中的应用。
第三课时:游戏逻辑设计,编写扫雷游戏代码。
第四课时:编程实践,调试并优化扫雷游戏代码。
教材章节关联:教学内容与教材中“数组”、“控制结构”、“程序设计”等章节相关联,通过本课程的学习,使学生能将这些知识运用到实际项目中,提高编程实践能力。
扫雷的课程设计完整代码
扫雷的课程设计完整代码一、教学目标本节课的教学目标是让学生掌握扫雷游戏的规则和技巧,培养学生的逻辑思维能力和决策能力。
具体目标如下:1.了解扫雷游戏的基本规则和术语。
2.掌握扫雷游戏的策略和技巧。
3.能够独立完成简单的扫雷游戏。
4.能够分析并解决扫雷游戏中遇到的问题。
情感态度价值观目标:1.培养学生的团队合作意识和沟通能力。
2.培养学生面对挑战和克服困难的勇气和毅力。
二、教学内容本节课的教学内容主要包括扫雷游戏的基本规则、术语、策略和技巧。
具体内容包括:1.扫雷游戏的基本规则和术语,如雷区、雷数、安全区等。
2.扫雷游戏的策略和技巧,如搜索方法、标记方法、排除方法等。
3.扫雷游戏的实例分析和解决方法。
三、教学方法本节课采用多种教学方法相结合的方式,以激发学生的学习兴趣和主动性。
具体方法包括:1.讲授法:讲解扫雷游戏的基本规则、术语、策略和技巧。
2.案例分析法:分析扫雷游戏实例,引导学生思考和解决问题。
3.实验法:让学生亲身体验扫雷游戏,提高操作能力和解决问题的能力。
四、教学资源本节课的教学资源包括教材、参考书、多媒体资料和实验设备。
具体包括:1.教材:提供扫雷游戏的基本规则、术语、策略和技巧的讲解。
2.参考书:提供更深入的扫雷游戏知识和技巧。
3.多媒体资料:提供扫雷游戏的图像和视频,帮助学生更好地理解游戏规则和技巧。
4.实验设备:提供计算机或其他设备,让学生亲身体验扫雷游戏。
五、教学评估本节课的评估方式包括平时表现、作业和考试。
评估方式应客观、公正,能够全面反映学生的学习成果。
具体评估方式如下:1.平时表现:通过观察学生在课堂上的参与程度、提问回答和团队合作等方面的表现,评估学生的学习态度和积极性。
2.作业:布置相关的扫雷游戏练习题,评估学生对基本规则、术语、策略和技巧的掌握程度。
3.考试:设计一份关于扫雷游戏的考试,包括选择题、填空题和解答题,全面评估学生的知识水平和应用能力。
六、教学安排本节课的教学安排如下:1.教学进度:按照教材和教学大纲的安排,有序进行教学内容的讲解和实践。
vb扫雷游戏代码
Public Class formDim chks(8, 8) As CheckBoxDim o As IntegerDim i, j As IntegerDim P, U As IntegerDim arr(9, 0) As ObjectPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clicko = 0For i = 0 To 8For j = 0 To 8If chks(i, j) Is Nothing Thenchks(i, j) = New CheckBox()End IfAddHandler chks(i, j).MouseUp, AddressOf Chk_MouseUpchks(i, j).Appearance =Appearance.Buttonchks(i, j).height = 25chks(i, j).width = 25chks(i, j).Top = i * 25 + 50chks(i, j).left = j * 25chks(i, j).text = ""chks(i, j).tag = 0chks(i, j).BackColor = Me.BackColorchks(i, j).checked = Falsechks(i, j).Enabled = Truechks(i, j).Font = New Font("Arial", 14, FontStyle.Bold)Me.Controls.Add(chks(i, j))NextNextDim n As Integer = 0Dim h As Integer = 0Randomize()Do While h < 10P = Math.Floor(Rnd() * 9)U = Math.Floor(Rnd() * 9)If chks(P, U).Tag = 0 Thenarr(n, 0) = chks(P, U)chks(P, U).Tag = 1h = h + 1n += 1End IfLoopTimer1.Enabled = TrueEnd SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)End SubPrivate Sub Chk_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)Dim chk As CheckBox = CType(sender, CheckBox)Dim m As IntegerIf chk.Tag = 1 ThenFor m = 0 To 9arr(m, 0).BackColor = Color.BlueNextMsgBox("over ")EndEnd IfCall autoopen(chk)End SubSub autoopen(ByVal chk As CheckBox)Dim row As Integer = chk.Top / 25Dim col As Integer = chk.Left / 25Dim i, j As IntegerDim sum As Integer = 0For i = row - 1 To row + 1For j = col - 1 To col + 1If (i >= 0) And (i < 9) And (j >= 0) And (j < 9) Thensum += chks(i, j).TagEnd IfNextNextchk.Text = sumchk.Enabled = FalseIf sum = 0 ThenFor i = row - 1 To row + 1For j = row - 1 To row + 1If (i >= 1) And (i <= 8) And (j >= 1) And (j <= 8) ThenIf chks(i, j).Text = "" ThenCallautoopen(chks(i, j))End IfEnd IfNextNextEnd IfEnd SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadEnd SubPrivate Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Ticko = o + 1TextBox1.Text = oEnd SubPrivate Sub Button1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) HandlesButton1.MouseDoubleClickEnd SubPrivate Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChangedEnd SubEnd Class。
简易vb扫雷代码
简易vb扫雷代码Dim X As Integer, total%Private Sub Command1_Click()RandomizeDim k As Integer, m As Integerw = Form1.Widthh = Form1.Height * 0.9X = Val(InputBox("请输入行列数"))For i = 1 To XFor j = 1 To Xk = (i - 1) * X + jLoad Label1(k)Label1(k).Caption = ""Label1(k).Visible = TrueLabel1(k).Width = w \ XLabel1(k).Height = h \ XLabel1(k).Top = h / X * (i - 1)Label1(k).Left = w / X * (j - 1)If (i + j) Mod 2 = 0 ThenLabel1(k).BackColor = QBColor(12)ElseLabel1(k).BackColor = QBColor(15)End IfLabel1(k).FontSize = 260 \ XLabel1(k).Tag = IIf(Int(Rnd * 20 + 1) Mod 19 = 1, "○", " ") If Label1(k).Tag = "○" Then total = total + 1Label1(k).Alignment = 2Next jNext iLabel1(0).BackColor = QBColor(0)For i = 1 To XFor j = 1 To XIf j + 1 <= X And i > 1 ThenIf Label1((i - 2) * X + j + 1).Tag = "○" Then m = m + 1 End If If j + 1 <= X And i + 1 < X ThenIf Label1(i * X + j + 1).Tag = "○" Then m = m + 1End IfIf i + 1 <= X And j > 1 ThenIf Label1(i * X + j - 1).Tag = "○" Then m = m + 1End IfIf i > 1 And j > 1 ThenIf Label1((i - 2) * X + j - 1).Tag = "○" Then m = m + 1End IfIf j + 1 <= X ThenIf Label1((i - 1) * X + j + 1).Tag = "○" Then m = m + 1End IfIf i + 1 <= X ThenIf Label1(i * X + j).T ag = "○" Then m = m + 1End IfIf j > 1 ThenIf Label1((i - 1) * X + j - 1).Tag = "○" Then m = m + 1End IfIf i > 1 ThenIf Label1((i - 2) * X + j).Tag = "○" Then m = m + 1End IfLabel1((i - 1) * X + j).ToolTipText = mm = 0NextNextEnd SubPrivate Sub Form_Load()Label1(0).Visible = FalseMe.Width = 8000Me.Height = Me.WidthEnd SubPrivate Sub Label1_Mousedown(Index As Integer, Button As Integer, Shift As Integer, p As Single, q As Single)Static gross%If Button = 2 Then La bel1(Index).Caption = "※"If Button = 1 ThenIf Label1(Index).Tag = "○" ThenMsgBox "你挖到雷了"gross = 0For i = 1 To X * XLabel1(i).Caption = Label1(i).TagNext iFor i = 1 To X * X '延时Label1(i).BackColor = vbWhiteNextFor i = 1 To 1000000DoEventsNextFor i = 1 To X * XUnload Label1(i)NextElseLabel1(Index).BackColor = vbGreenLabel1(Index).Caption = Label1(Index).ToolTipTextIf Label1(Index).ToolTipText = "0" ThenIf Index Mod X = 0 Then i = Index / X Else i = Int(Index / X) + 1If Index Mod X = 0 Then j = X Else j = Index Mod XIf j + 1 <= X And i > 1 ThenLabel1((i - 2) * X + j + 1).Caption = Label1((i - 2) * X + j + 1).ToolTipText End IfIf j + 1 <= X And i + 1 < X ThenLabel1(i * X + j + 1).Caption = Label1(i * X + j + 1).ToolTipText End IfIf i + 1 <= X And j > 1 ThenLabel1(i * X + j - 1).Caption = Label1(i * X + j - 1).ToolTipText End IfIf i > 1 And j > 1 ThenLabel1((i - 2) * X + j - 1).Caption = Label1((i - 2) * X + j - 1).ToolTipText End IfIf j + 1 <= X ThenLabel1((i - 1) * X + j + 1).Caption = Label1((i - 1) * X + j + 1).ToolTipText End IfIf i + 1 <= X ThenLabel1(i * X + j).Caption = Label1(i * X + j).ToolTipT extEnd IfIf j > 1 ThenLabel1((i - 1) * X + j - 1).Caption = Label1((i - 1) * X + j - 1).ToolTipText End IfIf i > 1 ThenLabel1((i - 2) * X + j).Caption = Label1((i - 2) * X + j).ToolTipText End IfEnd IfEnd IfEnd Ifgross = 0If Label1.Count > 1 ThenFor i = 1 To X * XIf Label1(i).Caption = "※" Then gross = gross + 1Next iEnd Ifs = 0If Label1.Count > 1 ThenFor i = 1 To X * XIf Label1(i).BackColor = vbGreen Then s = s + 1Next iEnd IfDebug.Print "s的值是"; sDebug.Print "gross"; grossDebug.Print "total"; totalIf s + gross = X * X Then '确保探测完所有方块msg = MsgBox("恭喜你清完了所有雷,是否重新开始?", vbYesNo) If msg = vbYes ThenFor i = 1 To X * XUnload Label1(i)NextEnd Ifgross = 0End IfEnd Sub。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
扫雷游戏VB设计实现最终界面如下:显然只需完成三个窗体的设计即可,具体设计界面和代码如下:一、主窗体(form10)设计界面:主窗体(form10)的代码:Dim d(11, 11) As IntegerDim k As IntegerDim v(100) As Integer '定义全局变量'Private Sub Command1_Click(Index As Integer)Timer1.Enabled = True '当点击任意一个命令按钮时(即开始游戏),则启动计时器' i = Index \ 10 + 1j = Index Mod 10 + 1 '将二维数组的元素与命令按钮一一对应'If d(i, j) = 1 Then '判断是否点到地雷'Timer1.Enabled = False '关闭计时器,游戏结束'For i = 1 To 10For j = 1 To 10n = 10 * (i - 1)m = j - 1If d(i, j) = 1 ThenCommand1.Item(m + n).Picture = LoadPicture(App.Path & "\2.jpg") '在按钮上显示地雷图片'End IfForm1.Show '弹出子窗体1(判断输赢)'Next jNext iElseIf v(Index) = Index + 1 Then '判断是否插上红旗或是问号图片'Command1.Item(Index).Picture = LoadPicture() '清除图片'Command1.Item(Index).Caption = f(i, j) '调用函数,显示周围地雷数'Command1.Item(Index).Enabled = False '将按钮设为不可用'Label7.Caption = Val(Label7.Caption) + 1v(Index) = 0End Ifh = s(i, j) '调用函数,显示周围的情况(边界)'For Y = 0 To 99If v(Y) = Y + 1 ThenCommand1.Item(Y).Enabled = True '如果是按钮插上了红旗则将按钮设置为可用'End IfNext YEnd IfFor k = 0 To 99If Command1.Item(k).Enabled = False Thenp = p + 1 '统计扫过的按钮个数'If p = 90 ThenFor Y = 0 To 100v(Y) = 0Next Y '将数组v的元素重新置0(此步目的为实现重玩而设)'Form2.Show '如果等于90个则结束游戏,弹出子窗体2(赢了)'End IfEnd IfNext kEnd SubPrivate Sub Command1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 Then '右击鼠标'If v(Index) <> Index + 1 Then '判断是否已插上红旗'Command1.Item(Index).Picture = LoadPicture(App.Path & "\1.jpg") '插上红旗'v(Index) = Index + 1 '给数组元素赋值,以此实现按钮图片的切换或显示状态'Label7.Caption = Val(Label7.Caption) - 1 '在标签7中显示插上的红旗个数'ElseCommand1.Item(Index).Picture = LoadPicture(App.Path & "\3.jpg") '将问号图片替换红旗'Label7.Caption = Val(Label7.Caption) + 1v(Index) = 0End IfEnd IfFor i = 1 To 10For j = 1 To 10If d(i, j) = 1 Thenn = 10 * (i - 1)m = j - 1If v(m + n) = m + n + 1 Then c = c + 1If c = 10 ThenFor Y = 0 To 100v(Y) = 0Next Y '将数组v的元素重新置0(此步目的为实现重玩而设)'Form2.Show '如果等于90个则结束游戏,弹出子窗体2(赢了)'End IfEnd IfNext jNext iEnd SubPrivate Sub Form_Load()k = 0For i = 1 To 10For j = 1 To 10d(i, j) = 0 '将数组元素置0'Next jNext iDo While n <> 10Randomizei = Int(10 * Rnd + 1)j = Int(10 * Rnd + 1)If d(i, j) = 0 Then d(i, j) = 1: n = n + d(i, j) '产生十个随机数,即相当于十个地雷'Loopbel7.Caption = 10bel2.Caption = 0bel3.Caption = 0End SubFunction f(i, j) '定义一个函数求一个区域周围地雷的个数'f = f + d(i - 1, j - 1) + d(i - 1, j) + d(i - 1, j + 1) + d(i, j - 1)f = f + d(i, j + 1) + d(i + 1, j - 1) + d(i + 1, j) + d(i + 1, j + 1)End FunctionFunction s(i, j) '定义一个函数显示点击区域周围的情况(即边界)' For Y = 0 To 100If v(Y) = Y + 1 ThenCommand1.Item(Y).Enabled = False '如插上了红旗,则将按钮先设为不可用'End IfNext YIf f(i, j) <> 0 Then '函数嵌套,调用函数判断周围是否无地雷'n = 10 * (i - 1)m = j - 1Command1.Item(m + n).Picture = LoadPicture()Command1.Item(n + m).Caption = f(i, j)Command1.Item(n + m).Enabled = False '清除图片,显示地雷数,设置按钮不可用'ElseFor a = i - 1 To i + 1For b = j - 1 To j + 1If a <> 0 And b <> 11 And a <> 11 And b <> 0 Thenn = 10 * (a - 1)m = b - 1If Command1.Item(n + m).Enabled = True ThenCommand1.Item(m + n).Picture = LoadPicture()Command1.Item(n + m).Caption = f(a, b)Command1.Item(n + m).Enabled = False '清除图片,显示地雷数,设置按钮不可用'h = s(a, b) '调用函数本身,即实现递归'End IfEnd IfNext bNext aEnd IfEnd FunctionPrivate Sub Timer1_Timer() '设计一个计时器'Label2.Caption = Val(Label2.Caption) + 1Label3.Caption = Val(Label2.Caption) \ 60 + Val(Label3.Caption) Label2.Caption = Val(Label2.Caption) Mod 60End Sub二、子窗体一(form1)界面如下:子窗体一(form1)的代码:Private Sub Command1_Click()Unload Form10Unload form1Form10.ShowEnd SubPrivate Sub Command2_Click()Unload Form10Unload form1End SubPrivate Sub Command3_Click()For i = 0 To 99mand1.Item(i).Picture = LoadPicture() mand1.Item(i).Caption = ""mand1.Item(i).Enabled = TrueNext ibel7.Caption = 10bel2.Caption = 0bel3.Caption = 0Unload form1End Sub三、子窗体(form2)界面如下:子窗体二(form2)的代码:Private Sub Command1_Click() Unload Form10Form10.ShowUnload form2End SubPrivate Sub Command2_Click() Unload Form10Unload form2End SubPrivate Sub Command3_Click() For i = 0 To 99mand1.Item(i).Picture = LoadPicture()mand1.Item(i).Caption = ""mand1.Item(i).Enabled = TrueNext iUnload form2bel7.Caption = 10bel2.Caption = 0bel3.Caption = 0End Sub注释:共三个窗体(这里是form10、form1、form2)、三个标签(这里是label2、label3、label7)设计时,根据具体的情况对应修改即可。