我正在尝试使用vb.net制作一个简单的游戏,它的概念就像抓鸡蛋。
我的期望是,当鸡蛋掉落时(picturebox1),捕手(picturebox2)抓到鸡蛋,每次抓取1分。我的想法是,当鸡蛋的位置与捕手的位置相匹配时,它会增加点数。但是它没有用。有什么建议吗?
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
这是游戏结束的代码
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
欢迎来到网站!我相信Bounds属性应对此起作用。另外,可能只想对碰撞使用布尔值(或引发事件),这样您就不必重复两次工作。
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