⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 vb实现的扫雷程序
💻 VB
字号:
Public Class Form1
    Public m_x As Integer
    Public m_y As Integer
    Public m_n As Integer
    Public m_board(,) As Byte
    Public m_map(,) As Byte
    Public m_bitmap As Bitmap
    Dim m_point() As Point
    Dim g As Graphics
    Dim notover As Boolean
    Dim dx() = {-1, -1, 0, 1, 1, 1, 0, -1}
    Dim dy() = {0, 1, 1, 1, 0, -1, -1, -1}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_x = 0
        m_y = 0
        m_n = 0
    End Sub

    Public Sub init()
        notover = True
        m_bitmap = New Bitmap(m_x * 16, m_y * 16)
        g = Graphics.FromImage(m_bitmap)
        For i = 0 To m_x
            For j = 0 To m_y
                m_board(i, j) = 0
                m_map(i, j) = 0
                g.DrawImage(ImageList1.Images(9), i * 16, j * 16)
            Next
        Next

        Dim x, y, tx, ty As Integer
        Randomize()
        For i = 1 To m_n
            Do
                x = (100 * Rnd()) Mod m_x
                y = (100 * Rnd()) Mod m_y
            Loop While m_board(x, y) = 10
            m_board(x, y) = 10
            m_point(i).X = x
            m_point(i).Y = y
            For j = 0 To 7
                tx = x + dx(j)
                ty = y + dy(j)
                If tx >= 0 And tx < m_x And ty >= 0 And ty < m_y Then
                    If m_board(tx, ty) <> 10 Then
                        m_board(tx, ty) += 1
                    End If
                End If
            Next
        Next
        Refresh()
    End Sub

    Public Sub ld(ByVal x As Integer, ByVal y As Integer)
        If m_map(x, y) = 0 Then
            If m_board(x, y) = 0 Then
                fun(x, y)
            ElseIf m_board(x, y) = 10 Then
                gameOver()
            Else
                m_map(x, y) = 1
                g.DrawImage(ImageList1.Images(m_board(x, y)), x * 16, y * 16)
            End If
        End If
    End Sub

    Public Sub fun(ByVal x As Integer, ByVal y As Integer)
        Dim tempx, tempy As Integer
        For i = 0 To 7
            tempx = x + dx(i)
            tempy = y + dy(i)
            If tempx >= 0 And tempx < m_x And tempy >= 0 And tempy < m_y Then
                If m_map(tempx, tempy) = 0 Then
                    m_map(tempx, tempy) = 1
                    g.DrawImage(ImageList1.Images(m_board(tempx, tempy)), tempx * 16, tempy * 16)
                    If m_board(tempx, tempy) = 0 Then
                        fun(tempx, tempy)
                    End If
                End If
            End If
        Next
    End Sub

    Public Sub rd(ByVal x As Integer, ByVal y As Integer)
        If m_map(x, y) = 0 Then
            m_map(x, y) = 2
            g.DrawImage(ImageList1.Images(10), x * 16, y * 16)
        ElseIf m_map(x, y) = 2 Then
            m_map(x, y) = 3
            g.DrawImage(ImageList1.Images(11), x * 16, y * 16)
        ElseIf m_map(x, y) = 3 Then
            m_map(x, y) = 0
            g.DrawImage(ImageList1.Images(9), x * 16, y * 16)
        End If
    End Sub

    Public Sub md(ByVal x As Integer, ByVal y As Integer)

    End Sub

    Public Sub gameOver()
        For i = 0 To m_n
            g.DrawImage(ImageList1.Images(12), m_point(i).X * 16, m_point(i).Y * 16)
        Next
        notover = False
        Refresh()
    End Sub

    Private Sub SetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetToolStripMenuItem.Click
        Dim dlg = New Dialog1
        If Windows.Forms.DialogResult.OK = dlg.ShowDialog() Then
            m_x = CInt(dlg.kd.Text)
            m_y = CInt(dlg.gd.Text)
            m_n = CInt(dlg.ls.Text)

            ReDim m_board(m_x, m_y)
            ReDim m_map(m_x, m_y)
            ReDim m_point(m_n)
            init()
            Me.Size = New Size(m_x * 16 + 6, m_y * 16 + 55)
        End If
    End Sub

    Private Sub RestartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestartToolStripMenuItem.Click
        If m_x = 0 Or m_y = 0 Or m_n = 0 Then
            SetToolStripMenuItem_Click(sender, e)
        End If
        init()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If notover Then
            If e.Button = Windows.Forms.MouseButtons.Left Then
                ld((e.X - 6) / 16, (e.Y - 27) / 16)
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
                rd((e.X - 6) / 16, (e.Y - 27) / 16)
            End If
            Refresh()
        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If m_bitmap Is Nothing Then
        Else
            e.Graphics.DrawImage(m_bitmap, 0, 24)
        End If
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -