none
大家帮帮忙啊``` RRS feed

  • 问题

  • 要求是设置一个拼图游戏,代码如下,紫色的部分出错了,

    Option Explicit

    Dim i As Integer '定义循环变量

    Dim j As Integer

    Dim step As Integer

    Dim x(8) As Integer '定义一个数组,用来存放随机编号

    Private Sub command1_click()
    init '初始化游戏
    Command1.Enabled = False '使开始按钮失效
    End Sub
    Private Sub Form_Load()
    Randomize '起始随机数产生器
    End Sub

    Sub init() '初始化游戏子程序

    For i = 0 To 7 '随机产生不重复的8个编号(从0到7)

    a0: x(i) = Int(Rnd * 8)

    For j = 0 To i - 1

    If x(i) = x(j) Then GoTo a0

    Next

    Next

    For i = 0 To 7

    Image1(i).Enabled = True '使Image1响应鼠标事件

    Image1(x(i)).Picture = PictureClip1.GraphicCell(i) '让图片的8个单元分别赋予 8个Image控件

    Next

    Image1(8).Picture = LoadPicture("") '空位用于移动图片

    End Sub

    Private Sub Image1_Click(Index As Integer) '注意索引值的运用

    Select Case Index Mod 3 '把9个Image控件分成三列

    Case 0 '当鼠标点击第1列时

    If Image1(Index + 1).Picture = LoadPicture("") Then '判断右边是否为空

    Image1(Index + 1).Picture = Image1(Index).Picture '交换图片

    Image1(Index).Picture = LoadPicture("") '测试是否拼图成功
    win
    End If

    Case 1 '当鼠标点击第2列时

    If Image1(Index - 1).Picture = LoadPicture("") Then '判断左边是否为空

    Image1(Index - 1).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    If Image1(Index + 1).Picture = LoadPicture("") Then '判断右边是否为空
    Image1(Index + 1).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    Case 2 '当鼠标点击第3列时

    If Image1(Index - 1).Picture = LoadPicture("") Then '判断左边是否为空

    Image1(Index - 1).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    End Select

    Select Case Index \ 3 '把9个Image控件分成三行

    Case 0 '当鼠标点击第1行时

    If Image1(Index + 3).Picture = LoadPicture("") Then '判断下边是否为空

    Image1(Index + 3).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    Case 1 '当鼠标点击第2行时

    If Image1(Index - 3).Picture = LoadPicture("") Then '判断上边是否为空

    Image1(Index - 3).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    If Image1(Index + 3).Picture = LoadPicture("") Then '判断下边是否为空

    Image1(Index + 3).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    Case 2 '当鼠标点击第3行时

    If Image1(Index - 3).Picture = LoadPicture("") Then '判断上边是否为空

    Image1(Index - 3).Picture = Image1(Index).Picture

    Image1(Index).Picture = LoadPicture("")

    win

    End If

    End Select

    End Sub

    Sub win() '测试是否拼图成功的子程序

    Dim num As Integer

    step = step + 1 '步数加1

    Label2.Caption = step

    For i = 0 To 8 '判断图块是否全部就位

    If Image1(i).Picture = PictureClip1.GraphicCell(i) Then num = num + 1

    Next

    If num = 8 Then

    MsgBox "你真棒!"

    For i = 0 To 8

    Image1(i).Enabled = False '使之不响应鼠标事件

    Next

    Command1.Enabled = True '恢复命令按钮

    End If

    End Sub

    2008年6月13日 1:12

答案

全部回复