game1.frm

来自「vb精彩编程希望大家有用」· FRM 代码 · 共 207 行

FRM
207
字号
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "拼图游戏"
   ClientHeight    =   4215
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3555
   LinkTopic       =   "Form1"
   ScaleHeight     =   4215
   ScaleWidth      =   3555
   StartUpPosition =   3  '窗口缺省
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   8
      Left            =   2240
      Picture         =   "game1.frx":0000
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   9
      Top             =   2840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   7
      Left            =   1240
      Picture         =   "game1.frx":1FF8
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   8
      Top             =   2840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   6
      Left            =   240
      Picture         =   "game1.frx":54CF
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   7
      Top             =   2840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   5
      Left            =   2240
      Picture         =   "game1.frx":80A9
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   6
      Top             =   1840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   4
      Left            =   1240
      Picture         =   "game1.frx":B523
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   5
      Top             =   1840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   3
      Left            =   240
      Picture         =   "game1.frx":E4DF
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   4
      Top             =   1840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   2
      Left            =   2240
      Picture         =   "game1.frx":10D5D
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   3
      Top             =   840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      AutoSize        =   -1  'True
      Height          =   1035
      Index           =   1
      Left            =   1240
      Picture         =   "game1.frx":1325A
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   2
      Top             =   840
      Width           =   1035
   End
   Begin VB.PictureBox Picture1 
      Height          =   1035
      Index           =   0
      Left            =   240
      Picture         =   "game1.frx":15DCE
      ScaleHeight     =   975
      ScaleWidth      =   975
      TabIndex        =   1
      Top             =   840
      Width           =   1035
   End
   Begin VB.CommandButton Command1 
      Caption         =   "开局"
      Height          =   495
      Left            =   1200
      TabIndex        =   0
      Top             =   120
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim ImageID(8) As Integer '记录图片排列顺序的数组
Dim Position As Integer '被点击的图片位置
Dim XPosition As Integer '空图片的位置
Dim MoveTimes As Integer '记录移动次数
Dim CalPosition As Integer '用于计算位置
Dim Started As Boolean
Dim Win As Boolean
Dim pic(8) As Picture

'双击Form1,加入代码:
Private Sub Form_Load()
    Dim i As Integer
        For i = 0 To 8
        Set pic(i) = Picture1(i).Picture
    Next
End Sub

'“开局” 按钮被点击后,要随机重排九张图片的位置。双击“开局”按钮,加入代码:
Private Sub Command1_Click()
    Dim i As Integer
    Dim Temp(8) As Integer
    Dim ChosenNumber As Integer
    For i = 0 To 8
        Temp(i) = i
    Next
    Randomize (Timer)
    For i = 8 To 0 Step -1
        ChosenNumber = Int(i * Rnd)
        ImageID(8 - i) = Temp(ChosenNumber)
        Temp(ChosenNumber) = Temp(i)
    Next
    For i = 0 To 8
        Picture1(i).Picture = pic(ImageID(i))
        If ImageID(i) = 8 Then XPosition = i
    Next
    Started = True
    MoveTimes = 0
End Sub

'最后,是游戏中对图片点击事件的处理:根据游戏者点击的位置判断是否要重画图片,是否已经成功重排所有图片:
Private Sub Picture1_Click(Index As Integer)
    Dim i As Integer
    Dim Response As Integer
    Position = Index
    If Started = False Then Exit Sub
    If ((Position <> 0) And (Position <> 3) And (Position <> 6)) Then
        CalPosition = Position - 1
        If (CalPosition = XPosition) Then CVale
    End If
    If ((Position <> 2) And (Position <> 5) And (Position <> 8)) Then
        CalPosition = Position + 1
        If (CalPosition = XPosition) Then CVale
    End If
    If (Position >= 3) Then
        CalPosition = Position - 3
        If (CalPosition = XPosition) Then CVale
    End If
    If (Position <= 5) Then
        CalPosition = Position + 3
        If (CalPosition = XPosition) Then CVale
    End If
    Win = True
    For i = 0 To 7
        If (ImageID(i) <> i) Then Win = False
        If Win = False Then Exit For
    Next
    If (Win = True) Then
        MsgBox "恭喜恭喜!你很聪明。" & vbCrLf & "(共移动" & MoveTimes & "次)", vbOKOnly, "恭喜!"
        Started = False
    End If
End Sub

Sub CVale()
    Picture1(CalPosition).Picture = Picture1(Position).Picture
    ImageID(CalPosition) = ImageID(Position)
    Picture1(Position).Picture = pic(8)
    ImageID(Position) = 8
    XPosition = Position
    MoveTimes = MoveTimes + 1
End Sub

⌨️ 快捷键说明

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