?? winmine.cls
字號:
VERSION 1.0 CLASS
BEGIN
MultiUse = 0 'False
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsWinMine"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
' 設(shè)置鼠標(biāo)左鍵常數(shù)
Private Const LEFT_BUTTON As Byte = 1
' 標(biāo)明空格的標(biāo)記
Private Const NONE As Byte = 0
' 標(biāo)明帶有地雷的方格的標(biāo)記
Private Const MINE As Byte = 243
' 標(biāo)明已經(jīng)被打開的方格的標(biāo)記
Private Const BEEN As Byte = 244
' 標(biāo)明已經(jīng)被標(biāo)定為有地雷的方格的標(biāo)記
Private Const FLAGGED As Byte = 2
' 標(biāo)明不明確方格的標(biāo)記
Private Const QUESTION As Byte = 1
' 最大、最小的地雷數(shù)、行數(shù)、列數(shù)
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
' 現(xiàn)在游戲難度的地雷總數(shù)
Private mbytNumMines As Byte
' 被正確標(biāo)明為帶有地雷的方格的數(shù)目
Private mbytCorrectHits As Byte
' 已被標(biāo)明的空格總數(shù)(包括錯誤標(biāo)記的)
Private mbytTotalHits As Byte
' 現(xiàn)在游戲難度的行數(shù)和列數(shù)
Private mintRows As Integer
Private mintCols As Integer
' 正在被執(zhí)行的行數(shù)和列數(shù)
Private mintRow As Integer
Private mintCol As Integer
' 表明一個新游戲的時間
Public mblnNewGame As Boolean
' 表明一個鼠標(biāo)點擊動作正在執(zhí)行
Private mblnHitTestBegun As Boolean
' 控制總顯示格數(shù)的變量
Private mfrmDisplay As Form
Private mbytMineStatus() As Byte
' 動態(tài)的2D隊列,用來跟蹤一個方格的現(xiàn)在狀態(tài)
' 是否被標(biāo)記,或者是否被標(biāo)記正確
Private mbytMarked() As Byte
' 動態(tài)的2D隊列,用來跟蹤一個含有地雷的方格的行數(shù)和列數(shù)
Private mbytMineLocations() As Byte
Private mcolWrongLocations As New Collection
Public Sub BeginHitTest(intButton As Integer, intX As Single, intY As Single)
' 如果現(xiàn)在的游戲結(jié)束,開始新的游戲
' 當(dāng)?shù)乩讌^(qū)被點擊
If mblnNewGame Then
NewGame
End If
' 表明鼠標(biāo)點擊動作正在執(zhí)行
mblnHitTestBegun = True
' 通過鼠標(biāo)的坐標(biāo)確定實際方格的坐標(biāo)
intX = Int(intX / mintButtonWidth)
intY = Int(intY / mintButtonHeight)
' 如果坐標(biāo)位于游戲界面外邊,則取消
If intX >= mintCols Or intY >= mintRows Or intX < 0 Or intY < 0 Then
Exit Sub
End If
' 通過柵格坐標(biāo)確定空格坐標(biāo)
mintCol = intX * mintButtonWidth
mintRow = intY * mintButtonHeight
' 如果方格已點開,則取消
If mbytMineStatus(intY, intX) >= BEEN Then Exit Sub
Dim blnLeftDown As Boolean
blnLeftDown = (intButton And LEFT_BUTTON) > 0
' 如果鼠標(biāo)左鍵點擊...
If blnLeftDown Then
' 如果方格已經(jīng)標(biāo)記了,不能打開,則取消
If mbytMarked(intY, intX) >= FLAGGED Then Exit Sub
' 臨時圖形顯示控制
If mbytMarked(intY, intX) = QUESTION Then
mfrmDisplay.imgPressed.Visible = False
mfrmDisplay.imgQsPressed.Visible = False
mfrmDisplay.imgQsPressed.Left = mintCol
mfrmDisplay.imgQsPressed.Top = mintRow
mfrmDisplay.imgQsPressed.Visible = True
Else
mfrmDisplay.imgQsPressed.Visible = False
mfrmDisplay.imgPressed.Visible = False
mfrmDisplay.imgPressed.Left = mintCol
mfrmDisplay.imgPressed.Top = mintRow
mfrmDisplay.imgPressed.Visible = True
End If
Else '如果鼠標(biāo)右鍵點擊....
Dim Msg As String
Dim CRLF As String
CRLF = Chr$(13) & Chr$(10)
Select Case mbytMarked(intY, intX)
Case NONE: ' 如果所有的空格都被標(biāo)注...
If mbytTotalHits = mbytNumMines Then
Msg = "Can't Mark Any More Mines!" & CRLF
Msg = Msg & "One or more Mines have been wrongly marked." & CRLF
Msg = Msg & "UnMark one or more mines with the right mouse button."
MsgBox Msg, vbCritical, "WinMine: Error!"
Exit Sub
End If
' 如果沒有標(biāo)注,顯示一個標(biāo)記表示標(biāo)注
mfrmDisplay.PaintPicture mfrmDisplay.imgFlag, mintCol, mintRow
' 增加已被標(biāo)注的方格數(shù)量
mbytTotalHits = mbytTotalHits + 1
' 更新剩余地雷的顯示
mfrmDisplay.lblMinesLeft = "Mines Left : " & mbytNumMines - mbytTotalHits
' 如果正確標(biāo)記
If mbytMineStatus(intY, intX) = MINE Then
mbytCorrectHits = mbytCorrectHits + 1
mbytMarked(intY, intX) = FLAGGED
Else ' 如果錯誤標(biāo)記
Dim objCoords As New clsCoords
' 在新文件中存儲錯誤標(biāo)記的坐標(biāo)
objCoords.mintX = intX
objCoords.mintY = intY
' 將它加入到收集
mcolWrongLocations.Add objCoords
' 存儲被錯誤標(biāo)注的方格的總數(shù)
mbytMarked(intY, intX) = mbytTotalHits - mbytCorrectHits + 2
End If
' 如果所有的地雷都被正確標(biāo)注...
If mbytCorrectHits = mbytNumMines Then
Msg = "Congratulations!" & CRLF
Msg = Msg & "You have won." & CRLF
MsgBox Msg, vbInformation, "WinMine"
' 準(zhǔn)備新游戲
mblnNewGame = True
End If
Case QUESTION: ' 如果模糊標(biāo)注,則不標(biāo)注
mbytMarked(intY, intX) = NONE
' 顯示原始方格
mfrmDisplay.PaintPicture mfrmDisplay.imgButton, mintCol, mintRow
Case Else: ' 如果以前用“紅旗”標(biāo)注,則更改為空標(biāo)注
' 顯示用“?”標(biāo)注的方格
mfrmDisplay.PaintPicture mfrmDisplay.imgQuestion, mintCol, mintRow
' 減少已標(biāo)注方格的總數(shù)
mbytTotalHits = mbytTotalHits - 1
' 更新剩余地雷的顯示
mfrmDisplay.lblMinesLeft = "Mines Left : " & mbytNumMines - mbytTotalHits
' 如果以前標(biāo)注的方格含有地雷
If mbytMineStatus(intY, intX) = MINE Then
' 同時減少正確標(biāo)注的方格數(shù)目
mbytCorrectHits = mbytCorrectHits - 1
Else ' 如果是錯誤的標(biāo)注方格
' 取消這個錯誤的坐標(biāo)
mcolWrongLocations.Remove mbytMarked(intY, intX) - 2
Dim intXwm As Integer ' 錯誤地點的X坐標(biāo)
Dim intYwm As Integer ' 錯誤地點的Y坐標(biāo)
Dim i As Integer ' 循環(huán)計算
' 更新其它錯誤標(biāo)注的坐標(biāo)指數(shù)
For i = mbytMarked(intY, intX) - 2 To mcolWrongLocations.Count
intXwm = mcolWrongLocations(i).mintX
intYwm = mcolWrongLocations(i).mintY
mbytMarked(intYwm, intXwm) = mbytMarked(intYwm, intXwm) - 1
Next
End If
mbytMarked(intY, intX) = QUESTION
End Select
End If
End Sub
Public Sub EndHitTest(intButton As Integer, intX As Single, intY As Single)
' 如果正在執(zhí)行一個鼠標(biāo)的點擊動作.
If mblnHitTestBegun Then
' 重新設(shè)置標(biāo)記
mblnHitTestBegun = False
Else
' 如果不是,則取消
' 當(dāng)鼠標(biāo)被點擊
Exit Sub
End If
Dim blnLeftDown As Boolean
blnLeftDown = (intButton And LEFT_BUTTON) > 0
' 如果鼠標(biāo)左鍵被點擊
If blnLeftDown Then
'通過鼠標(biāo)的坐標(biāo)確定方格的坐標(biāo)
intX = Int(intX / mintButtonWidth)
intY = Int(intY / mintButtonHeight)
' 如果坐標(biāo)位于游戲界面外,則取消
If intX >= mintCols Or intY >= mintRows Or intX < 0 Or intY < 0 Then
Exit Sub
End If
' 如果當(dāng)前點擊的方格已被標(biāo)注,則取消
If mbytMarked(intY, intX) >= FLAGGED Then Exit Sub
' 通過鼠標(biāo)坐標(biāo)確定方格坐標(biāo)
intX = mintCol \ mintButtonWidth
intY = mintRow \ mintButtonHeight
If mbytMarked(intY, intX) = QUESTION Then
mfrmDisplay.imgQsPressed.Visible = False
Else
mfrmDisplay.imgPressed.Visible = False
End If
Select Case mbytMineStatus(intY, intX)
Case Is >= BEEN: ' 如果當(dāng)前的方格已被打開,則取消
Exit Sub
Case NONE: ' 如果當(dāng)前的方格是空的,則打開周圍所有的空方格
OpenBlanks intX, intY
Case MINE: ' 如果當(dāng)前的空格含有地雷,則引爆地雷
Dim intXm As Integer ' 地雷的X坐標(biāo)
Dim intYm As Integer ' 地雷的Y坐標(biāo)
Dim vntCoord As Variant ' 每個循壞使用的變量
Dim i As Integer ' 循環(huán)計數(shù)
' 展示所有包含地雷的方格
For i = 0 To mbytNumMines - 1
intYm = mbytMineLocations(i, 0)
intXm = mbytMineLocations(i, 1)
If mbytMarked(intYm, intXm) < FLAGGED Then
mfrmDisplay.PaintPicture mfrmDisplay.imgMine, intXm * mintButtonWidth, intYm * mintButtonHeight
End If
Next
' 用一個爆炸的地雷顯示現(xiàn)在的方格
mfrmDisplay.PaintPicture mfrmDisplay.imgBlown, mintCol, mintRow
' 顯示所有被錯誤的確定為地雷的方格
For Each vntCoord In mcolWrongLocations
intYm = vntCoord.mintY
intXm = vntCoord.mintX
mfrmDisplay.PaintPicture mfrmDisplay.imgWrongMine, intXm * mintButtonWidth, intYm * mintButtonHeight
Next
' 準(zhǔn)備新游戲
mblnNewGame = True
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -