幫你改了一下
請自行加一個 Shape物件
程式是先跑水平再跑垂直
語法:
Dim XEnd, YEnd As Integer
Dim ShapeSpeedX, ShapeSpeedY As Integer 'X Y 移動值可配合方向指標做 ShapeSpeedX=-ShapeSpeedX 變化
Dim OverX, OverY As Boolean
'Dim Direction(1 To 4) As Boolean '方向指標: **請配合障礙判斷式使用**
Private Sub Form_Load()
Form1.AutoRedraw = True
Timer1.Interval = 100
XEnd = 0
YEnd = 0
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If XEnd = 0 And YEnd = 0 Then
XEnd = X
YEnd = Y
OverX = False
OverY = False
ShapeSpeedX = InputBox("請輸入移動速度", "老鼠移動速度", Shape1.Width / 2)
ShapeSpeedY = ShapeSpeedX
If XEnd < Shape1.Left Then ShapeSpeedX = -ShapeSpeedX
If YEnd < Shape1.Top Then ShapeSpeedY = -ShapeSpeedY
Form1.DrawWidth = 3
Me.PSet (XEnd, YEnd)
Me.Print " <=終點"
Form1.DrawWidth = 1
Timer1.Enabled = True
Exit Sub
End If
If Button = 2 Then
Timer1.Enabled = False
Me.Cls
XEnd = 0
YEnd = 0
End If
End Sub
'移動 Shape1
Private Sub Timer1_Timer()
Dim XOld, YOld As Integer '未移動前座標
Dim NewPosX, NewPosY As Integer '預測用
XOld = Shape1.Left + Shape1.Width / 2
YOld = Shape1.Top + Shape1.Height / 2
'**請加上遇到障礙時的判斷式**
If Not OverX Then
NewPosX = Shape1.Left + ShapeSpeedX '新的位址
If ShapeSpeedX > 0 Then If NewPosX >= XEnd Then OverX = True '水平移動終止條件
If Not OverX Then Shape1.Left = NewPosX '移動
If ShapeSpeedX < 0 Then If NewPosX <= XEnd Then OverX = True '水平移動終止條件
'畫出水平移動軌跡
Line (XOld, Shape1.Top + Shape1.Height / 2)-(Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2)
Exit Sub '**請配合障礙判斷式修改**
End If
'**請加上遇到障礙時的判斷式**
If Not OverY Then
NewPosY = Shape1.Top + ShapeSpeedY '新的位址
If ShapeSpeedY > 0 Then If NewPosY >= YEnd Then OverY = True '垂直移動終止條件
If Not OverY Then Shape1.Top = NewPosY '移動
If ShapeSpeedY < 0 Then If NewPosY <= YEnd Then OverY = True '垂直移動終止條件
'畫出垂直移動軌跡
Line (Shape1.Left + Shape1.Width / 2, YOld)-(Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2)
Exit Sub '**請配合障礙判斷式修改**
End If
'到達後停止
Timer1.Enabled = False
End Sub