VB 扫雷游戏程序
VB课程设计报告
课程设计题目扫雷游戏姓名:学号:难度:☆☆☆☆。
知识点:(1)控件数组;(2)绘图(3)菜单(4)文本文件的读写;(5)多模块程序设计;(6)资源文件的使用一、功能a.功能要求(1)启动程序后,显示初始界面,游戏共有3种预定义的难度级别,分别是初级(9行9列10颗雷)、中级(16行16列40颗雷)和高级(30行16列99颗雷)。
通过“游戏”菜单中的菜单项可以选择不同难度。
使用“自定义”菜单命令打开自定义对话框,可以自行定义雷区的行数、列数和雷数。
(2)雷区正上方是一个“小脸”图标,不同状态下,小脸显示出微笑、惊讶、痛苦和胜利等表情。
任何情况下单击小脸,可以重新开始游戏。
小脸左边以LED(发光二级管)方式显示了当前雷区中未被标记出来的雷数,右边是游戏进行的秒数。
计时是在用户第一次单击雷区的方块时开始。
(3)使用右键单击一个方格将其标记为雷之后,不能再使用左键单击它,但可以再右键单击。
右键再单击一次方格上显示“?”号(表示暂时不能确定),再单击第二次,恢复为普通样子。
使用左键可以单击除了标记为有雷之外的所有未翻开的方格(包括标记为“?”号的)(4)如果一个方格内显示的数字为N(1~8)且其周围已有N个方块被标记为有雷,这样便可以同时单击鼠标左键和右键将该方格周围所有的其他方格翻开,这样就不必一个一个地去单击了。
不过如果标记有雷的方格中有标记错了的,这时会被“炸死”。
(5)扫雷失败时,程序用图标炸弹105显示所有雷位置,并将标记错了的和“炸了的”用炸弹104和炸弹103显示。
(6)扫雷成功时,如果没有打破该难度级别的记录,显示“胜利”消息框;如果破了纪录,显示输入框提示输入用户名。
通过“扫雷英雄榜”菜单命令,可以显示“扫雷英雄榜”对话框,其中列出了预设的3个难度级别的纪录保持者。
因为自定义级别难度差别很大,所以不提供该级别的成绩记录。
b.程序设计的思想和方法本游戏的功能类似于Windows操作系统自带扫雷游戏,设计之前,先了解这款游戏的规则。
vb_扫雷程序源码
buttons(8, 7) = Button80
buttons(8, 8) = Button81
For i = 0 To 8
For j = 0 To 8
buttons(i, j).Enabled = True
buttons(7, 0) = Button64
buttons(7, 1) = Button65
buttons(7, 2) = Button66
buttons(7, 3) = Button67
buttons(7, 4) = Button68
End Sub
Private Sub Button82_Click() Handles Button82.Click
Me.Form1_Load()
End Sub
Private Sub Form1_Load() Handles MyBase.Load
buttons(0, 3) = Button4
buttons(0, 4) = Button5
buttons(0, 5) = Button6
buttons(0, 6) = Button7
buttons(0, 7) = Button8
buttons(1, 4) = Button14
buttons(1, 5) = Button15
buttons(1, 6) = Button16
buttons(1, 7) = Button17
buttons(1, 8) = Button18
clear_bomb += 1
End If
Next
Next
winXP扫雷游戏vb
winXP扫雷游戏内存查看代码左图是获取的图形结果,右图是扫雷结果。
黑色圆点对应地雷Public Class Form1Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtrPrivate Declare Auto Function GetWindowThreadProcessId Lib"user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessId As Integer) As IntPtrPrivate Declare Auto Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As IntPtrPublic Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByVal lpBuffer() As Byte, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As IntegerPublic Enum PROCESS_ACCESS As Integer' Specifies all possible access flags for the process object. PROCESS_ALL_ACCESS = &H1F0FFF' Enables using the process handle in the CreateRemoteThread function' to create a thread in the process.PROCESS_CREATE_THREAD = &H2' Enables using the process handle as either the source or' target process in the DuplicateHandle function to duplicate a handle.PROCESS_DUP_HANDLE = &H40' Enables using the process handle in the GetExitCodeProcess and ' GetPriorityClass functions to read information from the process object.PROCESS_QUERY_INFORMATION = &H400' Enables using the process handle in the SetPriorityClass function to' set the priority class of the process.PROCESS_SET_INFORMATION = &H200' Enables using the process handle in the TerminateProcess function to' terminate the process.PROCESS_TERMINATE = &H1' Enables using the process handle in the VirtualProtectEx and ' WriteProcessMemory functions to modify the virtual memory of the process.PROCESS_VM_OPERATION = &H8' Enables using the process handle in the ReadProcessMemory function to' read from the virtual memory of the process.PROCESS_VM_READ = &H10' Enables using the process handle in the WriteProcessMemory function to' write to the virtual memory of the process.PROCESS_VM_WRITE = &H20' Enables using the process handle in any of the wait functions to wait' for the process to terminate.SYNCHRONIZE = &H100000End EnumPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim buf(0) As ByteDim hWnd As IntegerDim pid As IntegerDim bytesRead As IntegerDim ph As IntegerDim X(0) As ByteDim Y(0) As ByteDim curProcess As Process = Process.GetCurrentProcess()Me.TextBox2.Text = ""hWnd = FindWindow(vbNullString, "扫雷")GetWindowThreadProcessId(hWnd, pid)ph = OpenProcess(PROCESS_ACCESS.PROCESS_VM_READ, False, pid) ReadProcessMemory(ph, &H10056A8, X, 1, bytesRead)ReadProcessMemory(ph, &H10056A8, Y, 1, bytesRead)For k As Integer = 0 To (X(0) - 1) * 32 Step 32For j As Integer = 0 To Y(0) - 1ReadProcessMemory(ph, &H1005361 + k + j, buf, 1, bytesRead)If buf(0) = 143 ThenMe.TextBox2.Text += "●" + " "ElseMe.TextBox2.Text += "○" + " "End IfNextMe.TextBox2.Text += vbCrLfNextEnd SubEnd Class本文介绍了用Visual 开发聊天通信软件的实现过程。
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设计实现最终界面如下:显然只需完成三个窗体的设计即可,具体设计界面和代码如下:一、主窗体(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开发扫雷游戏
、
初 始 化
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。
扫雷程序设计思路_Visual Basic程序设计基础_[共4页]
133
ElseIf a = 1 Then Text1.Text = date_one - Val(Text1.Text)
ElseIf a = 2 Then Text1.Text = date_one * Val(Text1.Text)
ElseIf a = 3 Then If val(Text1.Text) = 0 Then MsgBox "除数不能为零", 50, "调试" Else Text1.Text = date_one / Val(Text1.Text) End If
第5章 数 组
Text1.Text = "" a = Index End Sub Private Sub Command3_Click() Text1.Text = -1 * Val(Text1.Text) End Sub Private Sub Command4_Click()
If a = 0 Then Text1.Text = date_one + Val(Text1.Text)
End If End Sub Private Sub Command5_Click()
If val(Text1.Text )= 0 Then MsgBox "除数不能为零", 50, "调试"
Else Text1.Text = 1 / Val(Text1.Text)
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.游戏功能分析扫雷小游戏需要做哪些功能呢?在开始游戏后,程序需要根据难度系数自动生成一片雷区,雷区包含少数地雷和多数空地,在规定时间内找出雷区中的所有地雷即可胜利。
左键点击地雷会导致游戏失败,并给出所有地雷的位置;右键点击地雷或者空地都会在上面标记一面小旗,但右键点击地雷时,剩余地雷数会-1,而点击空地时不会。
每次右键标记地雷后,都要判断剩余地雷数是否为0,是的话就胜利了。
2.控件分析通过以上的游戏功能分析可以得到我们需要用到的控件。
在游戏提示方面,我们需要1个标签lblbomb来显示雷数,1个标签lbltime来显示剩余时间,一个timer1来计时,1个按钮cmdstart作为开始游戏的按钮。
并且把以上控件全放如一个框架frame1中。
在雷区这里,我们需要1个按钮来表示空地,并设置index为0;1个按钮来表示地雷,并设置index为0;一个标签来显示周围地雷数,并设置index为0.在游戏开始后,需要根据游戏难度加载出空地按钮和地雷按钮以及显示周围地雷数得标签按钮下面给出主要控件的名称及主要参数【说明】【名称】【index】空地按钮Cmd0 0地雷按钮Cmd1 0标签按钮lblnum 0在调试过程中可以将空地按钮的caption设置为0,地雷按钮的caption 设置为1,便于调试。
3.变量参数分析主要游戏参数:Public level As Integer ' 游戏难度Public cmdnum As Integer ' 雷区所有按钮数目Public bombnum As Integer ' 地雷数目Public timerest As Integer ' 剩余时间Public matrix() As Integer ' 雷区矩阵,0代表空地,1代表地雷Public bombrest As Integer ' 剩余地雷数这里我们设置雷区为正方形,此正方形的一条边上有几个按钮,难度就是几,所以有cmdnum=level^2。
简易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。
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。
扫雷游戏(HTML游戏使用JavaScript开发)
扫雷游戏(HTML游戏使用JavaScript开发)随着科技的不断进步,计算机游戏成为人们休闲娱乐的重要方式之一。
扫雷游戏作为一款经典的益智游戏,深受广大玩家喜爱。
本文将介绍如何使用JavaScript语言来开发一个简单的扫雷游戏。
一、游戏规则扫雷游戏的基本规则是在一个由方块组成的方阵中,隐藏着若干雷。
玩家需要依靠已翻开的方块上的数字提示,来判断其他方块是否有雷,并逐步扫除没有雷的方块。
如果玩家踩到雷,则游戏结束。
二、HTML布局在开始使用JavaScript开发扫雷游戏之前,我们首先需要搭建游戏的HTML布局。
可以创建一个div容器,设置其class为"mine-field",并在其中使用嵌套的div元素来表示方块。
三、CSS样式为了美化游戏界面,我们可以使用CSS样式来设置方块的背景颜色、边框样式等。
同时,还可以添加一些动画效果,增加游戏的趣味性。
四、JavaScript逻辑1. 初始化游戏界面使用JavaScript的DOM操作,可以在HTML布局中动态生成方块,并给每个方块添加点击事件监听器。
当方块被点击时,我们可以通过修改方块的背景颜色来实现方块的翻开效果。
2. 随机生成雷使用Math.random()函数来生成随机数,根据预设的雷密度来决定每个方块是否设为雷。
可以将雷的信息存储在一个二维数组中,用0表示无雷,用1表示有雷。
3. 计算方块周围雷的数量遍历每个方块,在周围的8个方向上检查相邻方块是否为雷,并统计雷的数量。
将这个数量作为方块上的数字提示。
4. 判断胜利条件每次点击方块后,我们需要判断游戏是否胜利。
只有剩余未翻开的方块中不含有雷,才表示游戏胜利。
5. 判断游戏结束如果玩家点击到了雷,游戏即结束。
此时可以显示一个弹窗,告知玩家游戏失败,并显示当前游戏界面。
6. 扩散算法当玩家点击的方块周围没有雷时,我们可以通过扩散算法自动翻开周围的方块。
可以使用递归的方式来实现扩散操作。
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秒。
游戏显示剩余雷数,此显示总雷数减去用户插的小旗数。
在游戏菜单中设有“新游戏”、“统计信息”、“选项”、“退出”,四个功能项。
计算机
基于VB的扫雷游戏设计摘要:扫雷游戏是Windows操作系统自带的一款小游戏,在过去的几年里,Windows操作系统历经数次换代更新,变得越来越庞大、复杂,功能也越来越强大,但是这款小游戏依然保持原来的容貌,可见这款小游戏受到越来越多人的喜爱。
本次的毕业设计我将利用Visual Basic6.0作为开发工具,开发一款类似的“扫雷游戏”。
解决了扫雷游戏初始化问题,包括随机产生本局游戏中雷的总数,雷的分布对于每一个小区域如何获取周围地区分布的雷数等,以及对鼠标事件的处理问题,既在游戏过程中对鼠标事件应如何做出反应。
关键词:扫雷游戏;Windows编程;Visual Basic6.0目录1 引言 (1)1.1课题背景及意义 (1)1.2开发工具的选用及介绍 (1)2 游戏的总体分析与设计 (3)2.1设计构想 (3)2.2流程规划 (4)2.3画面规划 (5)3 游戏的详细设计 (6)3.1游戏初始化 (6)3.2雷区的布置 (9)3.3游戏中主要模块的介绍与使用 (10)3.3.1鼠标事件 (10)3.3.2地雷及雷区表面探测情况 (12)3.3.3清除未靠近地雷的方块 (12)3.3.4游戏难度的选择 (14)3.4游戏的判断 (14)3.4.1游戏成功完成 (14)3.4.2游戏失败 (14)4 游戏测试结果 (15)结论 (16)参考文献 (17)致谢 (18)1引言1.1课题背景及意义在计算机逐步渗入社会生活各个层面的今天,计算机已经成为人们日常生的一部分,越来越多的人使用计算机办公、娱乐等等。
扫雷游戏是Windows操作系统自带的一款小游戏,在过去的几年里,Windows操作系统历经数次换代更新,变得越来越庞大、复杂,功能也越来越强大,但是这款小游戏依然保持原来的容貌,可见这款小游戏受到越来越多人的喜爱。
本次的毕业设计我将利用Visual Basic6.0作为开发工具,开发一款类似的“扫雷游戏”。
visual studio 2010 扫雷游戏API的实现方法
经典扫雷游戏C++API函数的实现方法扫雷对大家来说应该是非常熟悉和经典的游戏之一了,但是这样一个看似简单的小游戏是如何实现的呢?其实方法有很多种,相信我们大家都学过C++,我就先提供一种利用C++中的API函数实现扫雷的方法。
代码使用说明:游戏实现后的功能,鼠标左键单击掀开未知区域,鼠标右键单击设置标记,鼠标中间滑轮单击实现自动翻开功能。
1首先打开Visual Studio 2010软件;2点击文件->新建->项目3选择Win32中的Win32项目:4确定后点击“下一步”:如下图中勾选,点击完成即可:5点击视图主菜单(View)->解决方案资源管理器:6在右侧的解决方案资源管理器中的头文件文件夹中添加下面的头文件(.h),在源文件文件夹下添加下面的源文件(.cpp文件)以下是实现的各部分代码:首先我们来写程序的入口程序WinMain函数:文档命名为WinMain.cpp#include"proc.h"#include"caculate.h"extern char co[xk][yk];extern char th[xk][yk];int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow){creat(th);num(th);cover(th,co);MSG Msg ;if(!InitWindowsClass(hInstance))return FALSE;if(!InitWindows(hInstance,nCmdShow))return FALSE;//-----------------消息循环----------------------while( GetMessage(&Msg, NULL, 0, 0)){TranslateMessage( &Msg) ;DispatchMessage( &Msg) ;}return Msg.wParam;//消息循环结束即程序终止时将信息返回系统}由于Windows是基于消息的传递机制,所以接下来编写过程处理函数:命名为Proc.cpp#include"proc.h"#include"caculate.h"extern int record[xk][yk]={0}; //记录窗口的图形状态0为关闭、1为开启、2为占旗extern char co[xk][yk];extern char th[xk][yk];extern int xs,ys;extern int tn;void print(char co[xk][yk],HWND hWnd,int ini_x,int ini_y,int width,int height,int cir_len);long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam){intwidth=d_width,height=d_height,ini_x=d_ini_x,ini_y=d_ini_y,cir_len=8,mine_x=ys,mine_y=xs,cyc_ x,cyc_y;int move_x,move_y,record_i,record_j,flag;int test_x,test_y;WORD mouse_x,mouse_y;HDC hDC; //定义指向设备的句柄HBRUSH hBrush,hB; //定义指向画刷的句柄HPEN hPen;//定义指向画笔的句柄PAINTSTRUCT PtStr;//定义指向包含绘图信息的结构体变量WORD x,y;HCURSOR hCursor;TEXTMETRIC tm;wchar_t lpsz1[]=L"重新开局",lpsz2[]=L"重试本局",lpsz[3],lpsz_text[]=L"剩余标记数:";int i,j; //揭开时的坐标switch(iMessage) //处理消息{case WM_MOUSEMOVE:x=LOWORD(lParam);y=HIWORD(lParam);if(x>=ini_x&&x<=ini_x+width*mine_x&&y>=ini_y&&y<=ini_y+height*mine_y){hCursor=LoadCursor(NULL,IDC_CROSS);SetCursor(hCursor);}/*if(x>=20+2*d_ini_x+ys*d_width&&x<=20+2*d_ini_x+ys*d_width+70&&y>=10&&y<=10+hei ght){hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(DKGRAY_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,20+2*d_ini_x+ys*d_width,10,20+2*d_ini_x+ys*d_width+70,10+height,cir_len,cir _len);EndPaint(hWnd,&PtStr); //结束绘图hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式SetTextColor(hDC,RGB(255,233,0));GetTextMetrics(hDC,&tm);TextOut(hDC,20+2*d_ini_x+ys*d_width+2,10+2,lpsz1,4);EndPaint(hWnd,&PtStr); //结束绘图}*/return 0;case WM_PAINT: //处理绘图消息hDC=BeginPaint(hWnd,&PtStr);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(DKGRAY_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔for(cyc_x=0;cyc_x<mine_y;cyc_x++){for(cyc_y=0;cyc_y<mine_x;cyc_y++){RoundRect(hDC,ini_x+cyc_y*width,ini_y+cyc_x*height,ini_x+cyc_y*width+width,ini_y+cyc_x*hei ght+height,cir_len,cir_len); //绘制圆角矩形}}print(co,hWnd,ini_x,ini_y,width,height,cir_len);//按钮EndPaint(hWnd,&PtStr); //结束绘图hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(WHITE_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,20+2*d_ini_x+ys*d_width,10,20+2*d_ini_x+ys*d_width+70,10+height,cir_len,cir _len);RoundRect(hDC,20+2*d_ini_x+ys*d_width,50,20+2*d_ini_x+ys*d_width+70,50+height,cir_len,cir _len);EndPaint(hWnd,&PtStr); //结束绘图//按钮hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式SetTextColor(hDC,RGB(255,233,0));GetTextMetrics(hDC,&tm);TextOut(hDC,20+2*d_ini_x+ys*d_width+2,10+2,lpsz1,4);TextOut(hDC,20+2*d_ini_x+ys*d_width+2,50+2,lpsz2,4);EndPaint(hWnd,&PtStr); //结束绘图*/return 0;case WM_LBUTTONUP:mouse_x=LOWORD(lParam);mouse_y=HIWORD(lParam);test_x=(mouse_x+ini_x)/25-1;test_y=(mouse_y+ini_y)/25-1;move_x=((mouse_x+ini_x)/25-1)*width+ini_x;move_y=((mouse_y+ini_y)/25-1)*height+ini_y;if(mouse_x>=20+2*d_ini_x+ys*d_width&&mouse_x<=20+2*d_ini_x+ys*d_width+70&&mouse_y >=50&&mouse_y<=50+height){cover(th,co);for(record_i=0;record_i<mine_y;record_i++)for(record_j=0;record_j<mine_x;record_j++)record[record_i][record_j]=0;hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(DKGRAY_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔for(cyc_x=0;cyc_x<mine_y;cyc_x++){for(cyc_y=0;cyc_y<mine_x;cyc_y++){RoundRect(hDC,ini_x+cyc_y*width,ini_y+cyc_x*height,ini_x+cyc_y*width+width,ini_y+cyc_x*hei ght+height,cir_len,cir_len); //绘制圆角矩形}}}if(mouse_x>=20+2*d_ini_x+ys*d_width&&mouse_x<=20+2*d_ini_x+ys*d_width+70&&mouse_y >=10&&mouse_y<=10+height){creat(th);num(th);cover(th,co);for(record_i=0;record_i<mine_y;record_i++)for(record_j=0;record_j<mine_x;record_j++)record[record_i][record_j]=0;hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(DKGRAY_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔for(cyc_x=0;cyc_x<mine_y;cyc_x++){for(cyc_y=0;cyc_y<mine_x;cyc_y++){RoundRect(hDC,ini_x+cyc_y*width,ini_y+cyc_x*height,ini_x+cyc_y*width+width,ini_y+cyc_x*hei ght+height,cir_len,cir_len); //绘制圆角矩形}}}if(mouse_x>=ini_x&&mouse_x<=ini_x+width*mine_x&&mouse_y>=ini_y&&mouse_y<=ini_y+hei ght*mine_y){if(record[test_y][test_x]==0){//record[test_y][test_x]=1;hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(WHITE_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,move_x,move_y,move_x+width,move_y+height,cir_len,cir_len);EndPaint(hWnd,&PtStr); //结束绘图//揭开//cover(th,co);i=test_y;j=test_x; //获取揭开坐标if(th[i][j]=='*'){copy(th,co);print(co,hWnd,ini_x,ini_y,width,height,cir_len);for(record_i=0;record_i<mine_y;record_i++)for(record_j=0;record_j<mine_x;record_j++)record[record_i][record_j]=1;}else{if(th[i][j]==0){open(th,co,i,j);print(co,hWnd,ini_x,ini_y,width,height,cir_len);}else{co[i][j]=th[i][j];print(co,hWnd,ini_x,ini_y,width,height,cir_len);}}//揭开*/}/*else if(record[test_y][test_x]==2){record[test_y][test_x]=0;hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式左键取消标记代码hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(DKGRAY_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,ini_x+width*test_x,ini_y+height*test_y,ini_x+width*test_x+width,ini_y+height*t est_y+height,cir_len,cir_len);EndPaint(hWnd,&PtStr);}*/}return 0;case WM_RBUTTONDOWN:mouse_x=LOWORD(lParam);mouse_y=HIWORD(lParam);test_x=(mouse_x+ini_x)/25-1;test_y=(mouse_y+ini_y)/25-1;move_x=((mouse_x+ini_x)/25-1)*width+ini_x;move_y=((mouse_y+ini_y)/25-1)*height+ini_y;if(mouse_x>=ini_x&&mouse_x<=ini_x+width*mine_x&&mouse_y>=ini_y&&mouse_y<=ini_y+hei ght*mine_y){if(record[test_y][test_x]==0){hDC=GetDC(hWnd);record[test_y][test_x]=2;SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(WHITE_BRUSH);SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔Pie(hDC,move_x+2,move_y+2,move_x+25,move_y+25,move_x+2,move_y+2,move_x+2,move_y+ 2);EndPaint(hWnd,&PtStr);}else if(record[test_y][test_x]==2){record[test_y][test_x]=0;hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(DKGRAY_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,ini_x+width*test_x,ini_y+height*test_y,ini_x+width*test_x+width,ini_y+height*t est_y+height,cir_len,cir_len);EndPaint(hWnd,&PtStr);}}flag=0;for(record_i=0;record_i<mine_y;record_i++)for(record_j=0;record_j<mine_x;record_j++){if(record[record_i][record_j]==2) flag++;}flag=tn-flag;lpsz[0]=flag/100+48;lpsz[1]=(flag%100)/10+48;lpsz[2]=flag%10+48;hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式SetTextColor(hDC,RGB(0,0,234));GetTextMetrics(hDC,&tm);TextOut(hDC,20+2*d_ini_x+ys*d_width+2,80+2,lpsz_text,5);TextOut(hDC,20+2*d_ini_x+ys*d_width+2,120+2,lpsz,3);return 0;case WM_MBUTTONDOWN:mouse_x=LOWORD(lParam);mouse_y=HIWORD(lParam);test_x=(mouse_x+ini_x)/25-1;test_y=(mouse_y+ini_y)/25-1;move_x=((mouse_x+ini_x)/25-1)*width+ini_x;move_y=((mouse_y+ini_y)/25-1)*height+ini_y;if(record[test_y][test_x]==1){if(intelligence(record,th,test_x,test_y)){for(i=test_y-1,j=test_x-1;j<=test_x+1;j++){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]!=1&&record[i][j]!=2){co[i][j]=th[i][j];if(th[i][j]==0)open(th,co,i,j);print(co,hWnd,ini_x,ini_y,width,height,cir_len);}}for(j--,i++;i<=test_y+1;i++){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]!=1&&record[i][j]!=2){co[i][j]=th[i][j];if(th[i][j]==0)open(th,co,i,j);print(co,hWnd,ini_x,ini_y,width,height,cir_len);}}for(i--,j--;j>=test_x-1;j--){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]!=1&&record[i][j]!=2){co[i][j]=th[i][j];if(th[i][j]==0)open(th,co,i,j);print(co,hWnd,ini_x,ini_y,width,height,cir_len);}}for(j++,i--;i>=test_y;i--){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]!=1&&record[i][j]!=2){co[i][j]=th[i][j];if(th[i][j]==0)open(th,co,i,j);print(co,hWnd,ini_x,ini_y,width,height,cir_len);}}}}return 0;case WM_DESTROY: //结束应用程序PostQuitMessage(0); return 0;default://其他消息处理程序return(DefWindowProc(hWnd,iMessage,wParam,lParam)) ;}}void print(char co[xk][yk],HWND hWnd,int ini_x,int ini_y,int width,int height,int cir_len){TEXTMETRIC tm;HBRUSH hBrush,hB; //定义指向画刷的句柄HPEN hPen;//定义指向画笔的句柄HDC hDC; //定义指向设备的句柄PAINTSTRUCT PtStr;//定义指向包含绘图信息的结构体变量wchar_t lpsz[1];int i,j;for(i=0;i<xs;i++){for(j=0;j<ys;j++){lpsz[0]=co[i][j]+48;if(lpsz[0]>=48&&lpsz[0]<=57&&record[i][j]!=1){hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=CreateHatchBrush(NULL,RGB(204,255,0)); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,ini_x+j*width,ini_y+i*height,ini_x+j*width+width,ini_y+i*height+height,cir_len,c ir_len);EndPaint(hWnd,&PtStr); //结束绘图if(lpsz[0]!=48){hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式if(lpsz[0]==49)SetTextColor(hDC,RGB(0,0,234));else if(lpsz[0]==50)SetTextColor(hDC,RGB(0,123,0));elseSetTextColor(hDC,RGB(245,0,0));GetTextMetrics(hDC,&tm);TextOut(hDC,ini_x+j*width+2,ini_y+i*height+2,lpsz,1);}record[i][j]=1;}else if(lpsz[0]==90&&record[i][j]!=1){hDC=GetDC(hWnd);SetMapMode(hDC,MM_ANISOTROPIC); //设置映像模式hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(WHITE_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔RoundRect(hDC,ini_x+j*width,ini_y+i*height,ini_x+j*width+width,ini_y+i*height+height,cir_len,c ir_len);EndPaint(hWnd,&PtStr); //结束绘图hDC=GetDC(hWnd);hPen=(HPEN)GetStockObject(BLACK_PEN);//黑色画笔hB=(HBRUSH)GetStockObject(BLACK_BRUSH); //画刷SelectObject(hDC,hB); //选择画刷SelectObject(hDC,hPen); //选择画笔Pie(hDC,ini_x+j*width,ini_y+i*height,ini_x+j*width+width,ini_y+i*height+height,ini_x+j*width,ini _y+i*height,ini_x+j*width,ini_y+i*height);EndPaint(hWnd,&PtStr); //结束绘图record[i][j]=1;}else continue;}}}接下来是游戏软件Windows界面的初始化函数:命名为initwindow.cpp #include"proc.h"#include"caculate.h"#include"resource2.h"extern int xs,ys;BOOL InitWindowsClass(HINSTANCE hInstance){WNDCLASS wndclass ;wchar_t lpszClassName[]=L"窗口";//窗口类名wchar_t lpszTitle[]=L"miner_sweep";//窗口标题名wchar_t lpszMenuName[]=L"IDR_MENU1";//窗口类的定义wndclass.style=0;//窗口类型为缺省类型wndclass.lpfnWndProc=WndProc;//定义窗口处理函数wndclass.cbClsExtra=0;//窗口类无扩展wndclass.cbWndExtra=0;//窗口实例无扩展wndclass.hInstance=hInstance;//当前实例句柄wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);//窗口的最小化图标为缺省图标wndclass.hCursor=LoadCursor(NULL,IDC_ARROW) ;//窗口采用箭头光标wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);//窗口背景为白色wndclass.lpszMenuName=lpszMenuName;//窗口中无菜单wndclass.lpszClassName=lpszClassName ;//窗口类名为"窗口"//---------------以下进行窗口类的注册-------return RegisterClass( &wndclass);}BOOL InitWindows(HINSTANCE hInstance,int nCmdShow){HWND hwnd ;wchar_t lpszClassName[]=L"窗口";//窗口类名wchar_t lpszTitle[]=L"miner_sweep";//窗口标题名hwnd=CreateWindowW(lpszClassName,//窗口类名lpszTitle,//窗口实例的标题名WS_OVERLAPPEDWINDOW,//窗口的风格100,100,//窗口左上角坐标为缺省值20+2*d_ini_x+ys*d_width+130,40+2*d_ini_y+xs*d_height,//窗口的高和宽为缺省值NULL,//此窗口无父窗口NULL,//此窗口无主菜单hInstance,//创建此窗口的应用程序的当前句柄NULL //不使用该值);if(!hwnd)return FALSE;ShowWindow( hwnd, nCmdShow) ;UpdateWindow(hwnd);return TRUE;}接下来是核心部分是扫雷游戏的算法代码:命名为caculate.cpp#include"caculate.h" //定义覆盖层普通标记;extern char co[xk][yk]={0};extern char th[xk][yk]={0};extern int xs=20,ys=20;extern int tn=50;Z *top,*bottom;void crzh(Z **top,Z **bottom) //创建两个空头节点;{Z *tem;*top=*bottom=(Z*)malloc(sizeof(Z));(*top)->i=1;(*top)->j=1;tem=(Z*)malloc(sizeof(Z));(*top)->next=tem;tem->piror=*top;*top=tem;(*top)->next=NULL;}void pop(Z **top) //建立出栈函数;{*top=(*top)->piror;free((*top)->next);(*top)->next=NULL;}void push(Z **top,int i,int j) //建立压栈函数;{Z *temp;temp=(Z*)malloc(sizeof(Z));temp->i=i;temp->j=j;(*top)->next=temp;temp->piror=*top;(*top)=temp;(*top)->next=NULL;}void creat(char th[xk][yk]) //创建地图;{int x,y,i,j;for(i=0;i<xs;i++)for(j=0;j<ys;j++)th[i][j]=0;for(i=1;i<=tn;){x=(int)((rand()+90)*(xs)/(32767+90)); // 把xs-1改为了xsy=(int)((rand()+90)*(ys)/(32767+90));if(th[x][y]!=0) continue;th[x][y]='*';i++;}}void num(char th[xk][yk]) //标记数字;{int i,j,flag,ti,tj;for(i=0;i<xs;i++)for(j=0;j<ys;j++){if(th[i][j]=='*') continue;flag=0;ti=i;tj=j;for(--tj;ti<=i+1;ti++) {if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)&&(th[ti][tj]=='*')) flag++;else continue;}for(--ti,++tj;tj<=j+1;tj++) {if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)&&(th[ti][tj]=='*')) flag++;else continue;}for(--tj,--ti;ti>=i-1;ti--) {if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)&&(th[ti][tj]=='*')) flag++;else continue;}for(++ti,--tj;tj>=j-1;tj--) {if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)&&(th[ti][tj]=='*')) flag++;else continue;}th[i][j]=flag;}}void display(char th[xk][yk]) //显示创建的底盘结果;{int i,j;for(i=0;i<xs;i++){for(j=0;j<ys;j++)if(th[i][j]=='*') printf("%3c",th[i][j]);else printf("%3d",th[i][j]);putchar('\n');}}void cover(char th[xk][yk],char co[xk][yk]) //添加覆盖层;{int i,j;for(i=0;i<xs;i++)for(j=0;j<ys;j++)co[i][j]=buf;}void copy(char th[xk][yk],char co[xk][yk]){int i,j;for(i=0;i<xs;i++)for(j=0;j<ys;j++)if(th[i][j]=='*') co[i][j]=th[i][j];}void result(char co[xk][yk]) //显示游戏结果;{int i,j;for(i=0;i<xs;i++){for(j=0;j<ys;j++){if(co[i][j]=='*'||co[i][j]==buf) printf("%3c",co[i][j]);else printf("%3d",co[i][j]);}putchar('\n');}}void open(char th[xk][yk],char co[xk][yk],int i,int j) //定义打开区域函数;{int ti,tj;crzh(&top,&bottom); //创建栈的首节点;do{pop(&top);cout<<'*'<<endl;co[i][j]=th[i][j];ti=i;tj=j;for(--tj;ti<=i+1;ti++){if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)){if(th[ti][tj]==0){if(co[ti][tj]!=0) push(&top,ti,tj);cout<<'*'<<endl;}co[ti][tj]=th[ti][tj];}else continue;}for(--ti,++tj;tj<=j+1;tj++){if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)){if(th[ti][tj]==0){if(co[ti][tj]!=0) push(&top,ti,tj);}co[ti][tj]=th[ti][tj];}else continue;}for(--tj,--ti;ti>=i-1;ti--){if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)){if(th[ti][tj]==0){if(co[ti][tj]!=0) push(&top,ti,tj);}co[ti][tj]=th[ti][tj];}else continue;}for(++ti,--tj;tj>=j-1;tj--){if((ti>=0)&&(tj>=0)&&(ti<xs)&&(tj<ys)){if(th[ti][tj]==0){if(co[ti][tj]!=0) push(&top,ti,tj);}co[ti][tj]=th[ti][tj];}else continue;}i=top->i;j=top->j;}while(top!=bottom); //因为有空节点;}int intelligence(int record[xk][yk],char th[xk][yk],int move_x,int move_y) {int i,j,count=0;for(i=move_y-1,j=move_x-1;j<=move_x+1;j++){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]==2&&th[i][j]=='*')count++;}for(j--,i++;i<=move_y+1;i++){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]==2&&th[i][j]=='*')count++;}for(i--,j--;j>=move_x-1;j--){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]==2&&th[i][j]=='*')count++;}for(j++,i--;i>=move_y;i--){if(i<0||j<0||i>=xs|j>=ys) continue;if(record[i][j]==2&&th[i][j]=='*')count++;}if(count==th[move_y][move_x])return 1;elsereturn 0;}最后是运行程序需要使用的头文件为别为:(1)caculate.h#include<windows.h>#include<stdlib.h>#include<string.h>#include<stdio.h>#include<iostream>#include<conio.h>using namespace std;#define xk 100#define yk 100//#define xs 20 //横向尺寸;//#define ys 30 //纵向尺寸;//#define tn 100 //雷数;#define buf 12typedef struct zhan{int i,j;struct zhan *piror,*next;}Z;//定义覆盖层普通标记;void crzh(Z **top,Z **bottom); //创建两个空头节点;void pop(Z **top); //建立出栈函数;void push(Z **top,int i,int j); //建立压栈函数;void creat(char th[xk][yk]); //创建地图;void num(char th[xk][yk]); //标记数字;void display(char th[xk][yk]); //显示创建的底盘结果;void cover(char th[xk][yk],char co[xk][yk]); //添加覆盖层;void copy(char th[xk][yk],char co[xk][yk]);void result(char co[xk][yk]); //显示游戏结果;void open(char th[xk][yk],char co[xk][yk],int i,int j); //定义打开区域函数;int intelligence(int record[xk][yk],char th[xk][yk],int move_x,int move_y);(2)windo.h#include<windows.h>//包含应用程序中所需的数据类型和数据结构的定义#include<stdlib.h>#include<string.h>#include<stdio.h>#include<iostream>#include<conio.h>using namespace std;#define d_width 25 //雷块尺寸#define d_height 25#define d_ini_x 10 //雷区起始坐标#define d_ini_y 10BOOL InitWindowsClass(HINSTANCE hInstance);BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);(3)proc.h#include"windo.h"LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); //窗口函数说明完成以上操作,运行即可:以下为运行效果截图由于本人知识有限,仅提供以上代码供大家学习、参考,希望大家批评指正。
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, FalseobjMine.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 = 10 Private Const MAX_MINES As Byte = 99 Private Const MIN_ROWS As Integer = 8 Private Const MAX_ROWS As Integer = 24 Private Const MIN_COLS As Integer = 8 Private 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 & "一个或多个雷标记错误。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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 组件内必须存储大小相同的图片,所在这 款游戏设计中,显示的图片共分为三个部分:游戏状态图形、数字计数图形和地 雷区标示图形。
详细说明如下: 1. For...Next 循环应用 在游戏设计上, 利用了两个二维数组分别记录地雷的分布状况及地雷区及表 面探测情形。利用数据循环的对比,不但速度快,而且在设计阶段,程序的架构 比较清楚。 2.Load 及 Unload 动态增加或删除组件 利用 Load 及 Unload 指令,可动态新增及删除组件。在游戏的雷区,依照使 用者自定义的地图类型,布置未探测的地雷方块。加载组件后,通过 Image 组件 的 Move 属性,将组件布置于预定的位置。Move 函数的原型如下: 组件. Move X 坐标,[Y 坐标],[组件宽度][组件高度] 再配合双循环的变量值,可以控制组件布置于窗体的方向及方式。 3.MouseDown 与 MouseUp 事件 在游戏过程中, 利用鼠标所发出的信息了解使用者的意图,进而做出相应的 动作。 在 MouseDown 与 MouseUp 事件中, 可以捕捉鼠标所按下或放开的按键为何、 鼠标光标在该组件上的坐标及是否同时按下辅助键等。用 Button 参数值表示鼠 标上的按钮数值。其意义如下: 1:左键 2:右键 4:中央键 若同时按下不止一个按键时,则传回数值为按键数相加。例如同时按下左键
2.2 本设计运用的技术
这个游戏里运用了 VB 里的组件及语法作为游戏运算和显示,其相关语法及 组件如下表 1 所示:
2
唐山学院课程设计
表 1 使用的语法及组件 组件及语法 For...Next 说明 利用循环布置地雷区、按下时检查非地雷方 块 Load(Unload) 动态加载或删除指令产生表示地雷方块图案 的 Image 组件 Mousedown 与 Mouseup 事件 Imagelist 组件 Image 组件 Timer 组件 PictureBox 组件 递归 判断按下或放开方块时的按键为左键或右键 存储游戏过程中所需的图案 利用此显示地雷方块 设定触发时间,来控制方块游戏时间 利用此组件作为地雷方块的布置区域 利用递归展开非地雷的方块
4
唐山学院课程设计 7. PictureBox 组件 PictureBox 为容器类型的组件,可以加入其它组件在内[3]。在游戏中利用了 PictureBox 的两个特性:容器特性和立体框架。分述如下: ●容器特性: 只要容器本身显示的行为改变, 在容器内的组件也会跟着改变。 当游戏初始时,地雷区的显示组件会循环执行,逐一布置在画面上,但由于执行 循环时多个 Image 组件要显示图形, 造成分时系统执行频率显示上会出现不流畅 的情形,若在循环之前,先将容器本身的 Visible 属性设为 False,在循环执行 完成后再将 Visible 属性设为 True,则显示就会变得流畅许多。 ●立体框架: 通过组件本身立体框线的属性设定,可以为游戏画面的显示上 营造出层次感。 8.递归 递归就是一个程序能被自己所定义的语句调用。在游戏过程中,当使用者按 下非地雷方块时, 方块会沿四周八个方向向外翻开非地雷的方块。程序执行时必 须判断:以按下方块为中心。检查周围八个方块是否为非地雷方块,若其中一个 方块是非地雷方块时, 则又以其为中心,向外检查周围八个方块是否为非地雷方 块,如此反复的行为即构成了递归的使用条件。当判断方块内容为数字时(即在 非地雷方块周围),即停止递归执行。
图 1 ImageList 组件属性
利用 ListImage 集合对象的 Picture 方法, 即可将 ListImage 组件内的图片 指定给图形显示组件,语法如下: Set 图形组件. Picture=Imagelist. ListImage(图片索引). Picture 5.Image 组件 利用 Image 组件作为游戏执行阶段所有图形的显示。在游戏画面中,计数数 字、游戏状态表情及地雷区方块图形等,都利用 Image 组件作为显示组件,再搭 配 ImageList 组件所存储的图形, 便可在执行阶段在同一个 Image 组件上变换不 同的图片。 6.Timer 组件 在游戏中, 地雷引爆的可能有 2 中, 第一种为使用者翻开 (踩到) 地雷方块, 第二种为设定时间内未探测完所有地雷而引爆。其中第二种的行为,可以通过 Timer 组件来加以控制。利用 Timer 组件在固定时间即会触发事件的特性,在事 件触发时便将计数值加 1,直到判断计数值超过限定值时,即引爆地雷。
目 录
1 引言............................................................................................................................. 1 2 Visual Basic 概况...................................................................................................2 2.1 VB 简介............................................................................................................... 2 2.2 本设计运用的技术............................................................................................ 2 3 可行性分析................................................................................................................. 6 4 总体设计..................................................................................................................... 7 4.1 设计构想............................................................................................................. 7 4.2 流程规划............................................................................................................. 7 5 详细设计..................................................................................................................... 9 5.1 画面规划............................................................................................................. 9 5.2 设计过程.......................................................................................................... 10 5.2.1 5.2.2 添加菜单................................................................................................. 10 自定义窗体设计..................................................................................... 10