【VB开源代码栏目提醒】:以下是网学会员为您推荐的VB开源代码-PUZZLE.FRM,希望本篇文章对您学习有所帮助。
VERSION 5.00
Begin VB.Form frmPuzzle
BackColor = &H00C0FFFF&
BorderStyle = 1 'Fixed Single
Caption = "Puzzle"
ClientHeight = 5040
ClientLeft = 1350
ClientTop = 1875
ClientWidth = 4170
ClipControls = 0 'False
Icon = "PUZZLE.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 336
ScaleMode = 3 'Pixel
ScaleWidth = 278
StartUpPosition = 2 '屏幕中心
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 1000
Left = 1440
Top = 2400
End
Begin VB.CommandButton cmdButton
Caption = "cmdButton"
Height = 855
Index = 0
Left = 1200
TabIndex = 3
Top = 840
Visible = 0 'False
Width = 975
End
Begin VB.ComboBox cmbSize
Height = 315
ItemData = "PUZZLE.frx":030A
Left = 720
List = "PUZZLE.frx":030C
Style = 2 'Dropdown List
TabIndex = 1
Top = 4440
Width = 975
End
Begin VB.CommandButton cmdShuffle
Caption = "Shuffle"
Height = 615
Left = 2280
TabIndex = 0
Top = 4200
Width = 1215
End
Begin VB.Label lblSize
BackColor = &H00C0FFFF&
Caption = "Size:"
Height = 255
Left = 720
TabIndex = 2
Top = 4200
Width = 495
End
Begin
VB.Menu mnuGame
Caption = "&游戏"
Begin VB.Menu mnuHighScore
Caption = "&最高记录"
End
Begin VB.Menu mnuSound
Caption = "&声效"
Checked = -1 'True
End
Begin VB.Menu mnuExit
Caption = "&退出游戏"
End
End
End
Attribute VB_Name = "frmPuzzle"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private mbPuzzleSolved As Boolean
Private miEmptyIndex As Integer
Private miSize As Integer
Private mlTime As Long
Private Const MIN_SIZE As Byte = 3
Private Const MAX_SIZE As Byte = 7
'Text constants
Private Const TEXT_SHUFFLE As String = "Shuffle"
Private Const TEXT_NEW_GAME As String = "New Game"
Private Const TEXT_PUZZLE As String = "Puzzle"
Private Const TEXT_TIME As String = "Time:"
Private Const TEXT_HIGH_SCORE As String = "High score"
Private Const TEXT_SIZE As String = "Size"
Private Const TEXT_TIME_S As String = "Time"
Private Const TEXT_PLAYER As String = "Player"
Private Const TEXT_INPUT_PLAYER As String = "Write your name!"
Private Const TEXT_ANDERS_GAMES As String = "Anders Franssons Made In Home Games"
Private Static Sub Form_Load()
Dim i%
'Initialize random number generator
Randomize
'Load buttons
For i = 1 To MAX_SIZE ^ 2 - 1
Load cmdButton(i)
Next
'Add combo box items
For i = MIN_SIZE To MAX_SIZE
cmbSize.AddItem i
Next
'Auto click in combo
cmbSize.ListIndex = 1
miSize = cmbSize.Text
End Sub
Private Static Sub Form_Resize()
Dim bTimerWasOn As Boolean
cmdShuffle.SetFocus
'Stop timer when game is minimized and start it when normalized
If Me.WindowState = vbMinimized Then
If Timer1.Enabled Then bTimerWasOn = True Else bTimerWasOn = False
Timer1.Enabled = False
Else
If bTimerWasOn Then Timer1.Enabled = True
End If
End Sub
Private Static Sub cmdButton_MouseDown(Index As Integer, Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Dim i%, xEmpty%, yEmpty%, xClicked%, yClicked%
'Calculate coordinates for buttons
xEmpty = (miEmptyIndex) Mod miSize
yEmpty = (miEmptyIndex) \ miSize
xClicked = (Index) Mod miSize
yClicked = (Index) \ miSize
'Change buttons if empty is near
If (xClicked = xEmpty + 1 And yClicked = yEmpty) Or _
(xClicked = xEmpty - 1 And yClicked = yEmpty) Or _
(yClicked = yEmpty + 1 And xClicked = xEmpty) Or _
(yClicked = yEmpty - 1 And xClicked = xEmpty) Then
ChangeButtons (Index)
If mnuSound.Checked Then PlaySound App.Path & "\Move.wav"
End If
'Check if puzzle's solved
For i = 0 To miSize ^ 2 - 2
If Val(cmdButton(i).Caption) = i + 1 Then
mbPuzzleSolved = True
Else
mbPuzzleSolved = False
Exit For
End If
Next i
If mbPuzzleSolved Then
If (Timer1.Enabled And mnuSound.Checked) Then PlaySound App.Path & "\Applause.wav"
Timer1.Enabled = False
WriteHighScore
mlTime = 0
cmdShuffle.Caption = TEXT_SHUFFLE
cmdShuffle.SetFocus
Else
cmdShuffle.Caption = TEXT_NEW_GAME
End If
End Sub
Private Sub cmdShuffle_Click()
If mbPuzzleSolved Then
Shuffle
Else
NewGame
End If
If mnuSound.Checked Then PlaySound App.Path & "\Shuffle.wav"
End Sub
Private Sub cmbSize_Click()
If Not (miSize = cmbSize.Text) Then
miSize = cmbSize.Text
NewGame
End If
End Sub
Private Sub mnuExit_Click()
Unload Me
End Sub
Private Static Sub mnuHighScore_Click()
Dim strHighScore As String
Dim i%
strHighScore = TEXT_SIZE & vbTab & TEXT_TIME_S & vbTab & TEXT_PLAYER & vbNewLine
'Get high score from registry
For i = MIN_SIZE To MAX_SIZE
strHighScore = strHighScore &