如何缩短我的代码?

时间:2013-03-27 00:20:02

标签: .net vb.net visual-studio-2010 organization

我目前有一个巨大的代码可以重新制作游戏中的一些内容。代码非常大,我认为它可以压缩,但我不知道该怎么做。这是代码     Private Sub regen()

    'regen coins
    z = coin1



     z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()


    z = coin2


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin3


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin4


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin5


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin6


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin7


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin8


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin9

    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin10


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin11


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin12


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin13


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin14


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin15


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin16


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin17


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin18


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin19


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin20


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin21


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin22


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin23


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin24


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin25


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin26


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin27


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin28


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin29


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin30


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin31


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    z = coin32


    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

    'regen medcoins

    p = medcoin1


    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()

    p = medcoin2

    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()

    p = medcoin3


    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()

    p = medcoin4


    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()

    p = medcoin5


    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()

    p = medcoin6


    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()

    p = medcoin7


    p.Location = ploc
    p.Hide()
    ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If ploc.Y > 595 Then
        ploc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    p.Location = ploc
    p.Show()



    'regens hcoins

    h = hcoin1


    h.Location = hloc
    h.Hide()
    hloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If hloc.Y > 595 Then
        hloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    h.Location = hloc
    h.Show()

    h = hcoin2



    h.Location = hloc
    h.Hide()
    hloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If hloc.Y > 595 Then
        hloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    h.Location = hloc
    h.Show()

    h = hcoin3


    h.Location = hloc
    h.Hide()
    hloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If hloc.Y > 595 Then
        hloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    h.Location = hloc
    h.Show()



End Sub

正如您所看到的,它是一堆代码。

1 个答案:

答案 0 :(得分:0)

创建一个函数并将硬币传递给它:

Sub DoStuff(z as coin)
     z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()
End Sub

然后从其他地方调用

DoStuff (coin1)
DoStuff (coin2)
DoStuff (coin3)

您可能还想了解数组......