-
Jun 10th, 2017, 12:00 PM
#1
Thread Starter
Member
PictureBoxes Overlapping
Hi!
I am creating a game where there are trucks moving right (TrucksR()) and trucks moving left (TrucksL()). After going off the screen, they reappear on the other side of the screen as a randomly selected truck. There are different trucks with different widths and when I try randomizing the images, eventually, the trucks start to overlap. The trucks pile on each other. How do I avoid this?
Thanks in advance.
Code:
Private Sub movetruck()
For i = 0 To 9
If TrucksR(i).Left < 900 Then
TrucksR(i).Left += 5
Else
Randomize()
TrucksR(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\R\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp")
TrucksR(i).Left = -1 * TrucksR(i).Width
End If
If TrucksL(i).Left > -1 * TrucksR(i).Width Then
TrucksL(i).Left -= 5
Else
Randomize()
TrucksL(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\L\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp")
TrucksL(i).Left = 900
End If
Next
End Sub
-
Jun 10th, 2017, 02:47 PM
#2
Hyperactive Member
Re: PictureBoxes Overlapping
Try this.
vb.net Code:
Private Sub movetruck() For i = 0 To 9 If TrucksR(i).Left < 900 Then TrucksR(i).Left += 5 Else Randomize() TrucksR(i).Image = Nothing TrucksR(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\R\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp") TrucksR(i).Left = -1 * TrucksR(i).Width End If If TrucksL(i).Left > -1 * TrucksR(i).Width Then TrucksL(i).Left -= 5 Else Randomize() TrucksL(i).Image = Nothing TrucksL(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\L\" & Convert.ToString(CInt(5 * Rnd()).ToString) & ".bmp") TrucksL(i).Left = 900 End If Next End Sub
-
Jun 10th, 2017, 03:11 PM
#3
Re: PictureBoxes Overlapping
Maybe the bug is in this line
Code:
If TrucksL(i).Left > -1 * TrucksR(i).Width Then
Shouldn't you check against TrucksL(i).Width not TrucksR(i).Width?
-
Jun 10th, 2017, 03:48 PM
#4
Re: PictureBoxes Overlapping
You definitely need to get those Randomize calls out of there. Rnd and Randomize are holdovers from VB6, but calling Randomize inside a loop causes bad things to happen. What Randomize does is seeds the random number generator behind Rnd with the current system time down to the second. Two random number generators that get the same seed produce identical sequences. By calling Randomize in a tight loop, as you are doing, you will almost certainly be calling it twice in the same second, which will mean that you'll be getting the same number over and over from Rnd, rather than a new random number.
If you use Randomize and Rnd, then you have to be sure that you don't call Randomize more than once in any given second. The easiest way to do that is to only call it once...ever....in the program. Better would be to switch to using the Random object, which replaced Randomize and Rnd in .NET, and is more intuitive to use. However, even with Random, you need to only create one Random object and use it everywhere, because creating a Random object has the same effect as calling Randomize, so you need to ensure that you don't create more than one a second, and you never need more than one anyways, so just create one.
My usual boring signature: Nothing
-
Jun 10th, 2017, 07:41 PM
#5
Thread Starter
Member
Re: PictureBoxes Overlapping
While all your suggestions have helped the trucks from not colliding, there are still a few instances of trucks on top of each other. Here is my code now.
Code:
Private Sub movetruck(ByVal Rand As Integer)
For i = 0 To 9
If TrucksR(i).Left < 864 Then
TrucksR(i).Left += 5
Else
TrucksR(i).Image = Nothing
TrucksR(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\R\" & Convert.ToString(Rand) & ".bmp")
TrucksR(i).Left = -1 * TrucksR(i).Width
End If
If TrucksL(i).Left > -1 * 50 Then
TrucksL(i).Left -= 5
Else
TrucksL(i).Image = Nothing
TrucksL(i).Image = Image.FromFile(ResourceFilePathPrefix & "Pictures\Numbered\L\" & Convert.ToString(Rand) & ".bmp")
TrucksL(i).Left = 864
End If
Next
End Sub
-
Jun 12th, 2017, 10:18 AM
#6
Re: PictureBoxes Overlapping
I would think instead of placing a truck to start at the edge of the screen when you generate a new truck, you should start it some random distance from the end of the truck in front of it, so that it won't overlap. You can double check the distance calculated to ensure it is off screen so it doesn't pop partially on the screen initially and fall back to positioning at the screen edge in that case.
Last edited by passel; Jun 12th, 2017 at 10:24 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|