0

Im trying to make a maze-like game and I need to simplify the code for when my player(picture box) collides with the walls of the maze(label). The code I have so far is

if pictureBox1.Bounds.IntersectsWith(label1.bounds) or _ pictureBox1.Bounds.IntersectsWith(label2.bounds) then
(pretend code is here)

end if

and basically repeat that for 68 different walls. would there be any way to simplify that to make it shorter?

Vaillancourt
  • 16,325
  • 17
  • 55
  • 61
user30934
  • 1
  • 1

1 Answers1

2

Anytime you start appending numbers to your variable names, it's a good sign that what you really want is an array. Something like:

Dim pictureBoxes(1 to 50) as Object
Dim pictureCount as Integer

Dim labels(1 to 50) as Object Dim labelCount as Integer

' Assign your picture boxes and labels to those arrays

Dim i as Integer Dim j as Integer

For i = 1 To pictureCount For j = 1 to labelCount If (pictureBoxes(i).Bounds.IntersectsWith(labels(j).bounds) Then ' Handle the collision End If Next j Next i

This block of code handles checking the bounds of any number of picture boxes against any number of labels.

DMGregory
  • 134,153
  • 22
  • 242
  • 357