我正试图在Vb中制作一个蛙人克隆。基本上当你完成一个新的水平它会产生一个新的地图(道路灌木和汽车四处移动)汽车和道路工作正常。我不希望灌木丛在道路上生成,所以我试图制作一个纠正这个问题的循环。如何将多个图片框称为一个名称,而不必使用我制作的一堆代码 谢谢:))
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)
答案 0 :(得分:2)
尝试这样的事情:
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