简体   繁体   中英

refering to many objects under one name vb.net

I'm trying to make a frogger clone in Vb. Basically when you complete a new level it generates a new map (Roads bushes and cars move around) Cars and roads work fine. I don't want the bushes to generate on top of roads, so I tried to make a loop which corrects this issue. How do I refer to the many picture boxes as one name, without having to use the mess of code I made.
Thanks :)

 Randomize()
    Do
        Bush1.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

    Do
        Bush2.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

    Do
        Bush3.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

    Do
        Bush4.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

    Do
        Bush5.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

    Do
        Bush6.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

    Do
        Bush7.Location = New Point(((Me.Size.Width - 0 + 1) * Rnd() + 1), ((Me.Size.Height - 0 + 1) * Rnd() + 1))
    Loop Until Bush1.Bounds.IntersectsWith(PicRoad1.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad2.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad3.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad4.Bounds) Or Bush1.Bounds.IntersectsWith(PicRoad5.Bounds)

Try something like this out:

Public Class Form1

    Private R As New Random
    Private Roads() As PictureBox
    Private Bushes() As PictureBox

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Roads = {PicRoad1, PicRoad2, PicRoad3, PicRoad4, PicRoad5}
        Bushes = {Bush1, Bush2, Bush3, Bush4, Bush5, Bush6, Bush7}
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim failed As Boolean = False
        For Each bush As PictureBox In Bushes
            Do
                failed = False
                bush.Location = New Point(R.Next(Me.ClientSize.Width), R.Next(Me.ClientSize.Height))
                For Each road As PictureBox In Roads
                    If bush.Bounds.IntersectsWith(road.Bounds) Then
                        failed = True
                        Exit For
                    End If
                Next
            Loop While failed
        Next
    End Sub

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM