Personally I would not recommend using multiple pictureboxes for this. It will be very inefficient especially as you throw more items on the screen. The better approach would probably be to create just a regular custom control that manages a list of images (or whatever data you really need) and their location. Then you can change the position of the image and refresh to "move" things around. To do hit detection you normally have a bounding box around each object (irrelevant of what or the shape). When an object is moved you check to see if its bounding box intersects any other object's bounding box. If so then they have collided. It is a pretty standard and straightforward algorithm to implement. To "remove" an item just remove the image from your control's list and refresh. The hit detection logic doesn't need to change.
' My VB is rusty...
Public Class MyImageControl Inherits Control
'On paint, draw the images
'Add an image
Public Sub Add ( image As Image )
_images.Add(image)
Refresh()
End Sub
' Remove an image
Public Sub Remove ( image As Image )
_images.Remove(image)
Refresh()
End Sub
' Hit detection doesn't need to change as it should enumerate _images only
Private List(Of Image) _images As New List(Of Image)
End Class
If you want to continue using pictureboxes for now then you can. If you want to keep the PBs around then modify your hit detection logic to check to see if the PB's Visible
property is true or not. If it isn't then it is hidden. I'm assuming here that is the property you're toggling to show/hide them.
If YellowBlock6.Visible AndAlso Ball.Bounds.IntersectsWith(YellowBlock6.Bounds) Then
...
However if you were building a shooting game where you are going to need to have lots of temporary images on the screen then you will end up creating more and more PBs and just hiding them. This will be expensive and slow down as your game progresses. In this case you should probably just get rid of the PB when you're done with it. So instead of setting it hidden, remove it from the parent control's Controls
property. Then dispose of it and any image it contained. Your hit detection logic doesn't need to change.
Private Sub RemoveImage ( target As PictureBox )
' Remove from parent controls so it disappears
Me.Controls.Remove(target)
' Clean it up, probably should consider cleaning up the image as well but it depends on how you are using images
target.Dispose()
End Sub
' No changes to hit detection needed