其他分享
首页 > 其他分享> > VBA炸弹游戏

VBA炸弹游戏

作者:互联网

Option Explicit
Sub RePlay()
    Dim rng As Range, i As Long, j As Long
    Set rng = Worksheets(1).Range("B2:J11")
    rng.ClearContents
    rng.Interior.Color = xlNone
    For i = 2 To 11
        For j = 2 To 10
            If Rnd() > 0.9 Then Cells(i, j) = 1
        Next j
    Next i
End Sub

Sub SinglePlay()
    Dim i As Long, j As Long, rng As Range
    i = Round(2 + 9 * Rnd())
    j = Round(2 + 8 * Rnd())
    Cells(i, j).Select
    Set rng = Union(Range(Cells(i, 2), Cells(i, 10)), Range(Cells(2, j), Cells(11, j)))
    rng.Interior.Color = vbRed
    rng.Value = ""
        
End Sub

Sub Play()
    Dim rng As Range, num As Long
    Set rng = Worksheets(1).Range("B2:J11")
    
    Do While Application.WorksheetFunction.Sum(rng) <> 0
        Application.Wait Now + TimeValue("00:00:01")
        Call SinglePlay
        num = num + 1
    Loop
    Debug.Print "总共发射了" & num & "枚炸弹!"
End Sub


标签:VBA,num,游戏,Cells,rng,Long,Range,炸弹,Sub
来源: https://blog.csdn.net/piao3701/article/details/122328757