2015-12-18 83 views
0

因此,我正在学习visual basic,并且试图制作一个移动椭圆形的计时器,并检查是否触及任何其他椭圆形,它会将一个整数。所以我有这个代码,这有效,但我想知道如果我可以缩短它。如果你可以解释你的代码,因为我很新,这将是伟大的!提前致谢!如何检查椭圆形是否触及任何其他椭圆形VB

Private Sub Timer4_Tick(sender As System.Object, e As System.EventArgs) Handles Timer4.Tick, Timer4.Tick 
    Dim Pos As Integer 
    If bb.Bounds.IntersectsWith(OvalShape1.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape2.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape3.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape4.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape5.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape6.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape7.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape8.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape9.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape10.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape11.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape12.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape13.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape14.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape15.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape16.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape17.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape18.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape19.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape20.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape21.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape22.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape23.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape24.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape25.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    If bb.Bounds.IntersectsWith(OvalShape26.Bounds) Then 
     Pos = -20 
    Else 
     Pos = 20 
    End If 
    bb.Top -= Pos 
End Sub 
+0

缩短它的一种可能的方法是将所有的OvalShapes放入数组或List中并使用循环。 –

+0

编程提示:*从不*复制和粘贴代码。把它放在一个函数或变量中。 – buffjape

回答

1

你应该重构重码出到一个方法,并为您的常量,以便更好地解释他们的意思是:

Shared Function GetIntersection(Shape first, Shape second) 
    Const IntersectionValue = -20 
    Const NonIntersectionValue = 20 

    If first.Bounds.IntersectsWith(second.Bounds) Then 
     return IntersectionValue 
    Else 
     return NonIntersectionValue 
    End If 
End Function 

然后你会这样称呼它:

Dim Pos As Integer = GetIntersection(bb, OvalShape1) 

这是良好软件设计的重要原则之一:Don't Repeat Yourself (D.R.Y)

尽管你的代码存在问题 - 不管你是按照你的方式还是按照我的建议,你都不会对值做任何事情 - 你用每个值覆盖值随后的操作。例如:

If bb.Bounds.IntersectsWith(OvalShape1.Bounds) Then 
    Pos = -20 
Else 
    Pos = 20 
End If 

' you should at least do something with `Pos` assigned in the 
' above if-else block before you overwrite it with the following if-else: 

If bb.Bounds.IntersectsWith(OvalShape2.Bounds) Then 
    Pos = -20 
Else 
    Pos = 20 
End If 

事情是这样的,但是,它只是以往任何时候都采取价值的Pos最后的if-else块

If bb.Bounds.IntersectsWith(OvalShape26.Bounds) Then '... 

,那么什么是点在它之前的其他块?它们只是浪费在头顶上。

+0

我意识到这一点,就在我问完这个问题时,它会被覆盖,你有什么想法做到这一点吗? – gomilksolgo

+0

数组呢? – charliefox2

+0

什么是数组?对不起,我真的很新。 – gomilksolgo