Search code examples
.netvb.netvisual-studio-2010organization

How do I shorten my code?


I currently have a huge code for regenerating some things in a game a made. The code is very big and I think it can be compressed but I don't know how to do it. This is the code 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

As you can see it is a bunch of code.


Solution

  • Create a function and pass the coins into it:

    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
    

    then call it from elsewhere

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

    You may also want to read about arrays...