I'm trying to make a simple game using VB.Net and its concept is like egg catching.
My expectation is that when the egg is falling (PictureBox1
), the catcher (PictureBox2
) catches the egg and it gets 1 point every catch. My idea is when the location of egg matches the location of the catcher it adds point. But it didnt work. Any suggestion here?
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Top += 5
If PictureBox1.Location.Y = PictureBox2.Location.Y Then
score += 1
Label1.Text = score
End If
End Sub
And this is the code for game over:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If PictureBox1.Location.Y > 400 Then
Me.Dispose()
MsgBox("game over")
End If
End Sub
Welcome to the site! I believe the Bounds property should work for this. Also, may want to just use a boolean (or raise an event) for the collision, so this way you're not doing the work twice.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Top += 5
Dim itHappened as Boolean
Dim other_boxes as New List(Of PictureBox) from {PictureBox2} ', PictureBox3, PictureBox4}
For each box in other_boxes
If PictureBox1.Bounds.IntersectsWith(box.Bounds) Then
score += 1
Label1.Text = score
itHappened = True
else
itHappened = False 'depending on your logic, may not be the best place
End If
Next
If PictureBox1.Location.Y > 400 Then
Me.Dispose()
MsgBox("game over")
End If
End Sub