高手帮忙!一个点在图片筐内移动,当它移动到图片框的边界时能够反射回来,该怎么办?比如该点有x,y方向的速度,经过t时间后移动到边界,然后反弹回来。是不是跟坐标系有关呀?这段程序我想了好久就是不行,这个点要么还没到边界就反弹,要么到了边界也不反弹,气死我了!!帮帮忙!!!

解决方案 »

  1.   

    Option Explicit
    Dim xx As Long
    Dim yy As Long
    Dim tx As Long
    Dim ty As Long
    Private Sub Command1_Click()
        Timer1.Enabled = True
        Timer1.Interval = 50
        Me.Line (0, 0)-(4500, 3000), vbRed, B
        Me.DrawMode = 10
        xx = 0  '起始坐标
        yy = 0
        tx = 60 '移动偏差
        ty = 45
        Me.Circle (xx, yy), 30, vbBlue  '先画一个小球
    End SubPrivate Sub Timer1_Timer()
        Me.Circle (xx, yy), 30, vbBlue
        xx = xx + tx
        yy = yy + ty
        If xx >= (4500 - 15) Or xx <= (0 - 15) Then '减去15是小球的半径,如果需要控制的更加严格,可以修改15为别的数值,例如60
            tx = -tx
        End If
        If yy >= (3000 - 15) Or yy <= (0 - 15) Then
            ty = -ty
        End If
        Me.Circle (xx, yy), 30, vbBlue
    End Sub
      

  2.   

    Option Explicit
    Dim xx As Long
    Dim yy As Long
    Dim tx As Long
    Dim ty As Long
    Private Sub Command1_Click()
        Timer1.Enabled = True
        Timer1.Interval = 50
        Me.Line (0, 0)-(4500, 3000), vbRed, B
        Me.DrawMode = 10
        xx = 0  '起始坐标
        yy = 0
        tx = 60 '移动偏差
        ty = 45
        Me.Circle (xx, yy), 30, vbBlue  '先画一个小球
    End SubPrivate Sub Timer1_Timer()
        Me.Circle (xx, yy), 30, vbBlue
        xx = xx + tx
        yy = yy + ty
        If xx > (4500 - 60) Or xx < 60 Then '这里的第二个条件应该是正的,不是负数
            '减去15是小球的半径,如果需要控制的更加严格,可以修改15为别的数值,例如60
            tx = -tx
        End If
        If yy > (3000 - 45) Or yy < 45 Then
            ty = -ty
        End If
        Me.Circle (xx, yy), 30, vbBlue
    End Sub