【问题标题】:Add points when PictureBox1 hits PictureBox2当 PictureBox1 碰到 PictureBox2 时加点
【发布时间】:2020-06-07 07:37:33
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 最好使用单个绘图画布(如PictureBox)并处理Paint 事件来绘制所有内容。类似this.

标签: vb.net timer location picturebox


【解决方案1】:

欢迎来到本站!我相信 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

【讨论】:

  • 如果我有很多人至少要赶上 3 个呢?这种情况一直持续到我没有抓住其中一个?
  • 是的,如果您要添加更多内容,我建议您不要使用布尔值并将 .IntersectsWith 检查在 1 个位置,无论哪种方式。如果它在每个刻度上运行两次,它会显着减慢。
  • 现在可以使用了。谢啦。每次它与捕手相匹配时,我都会更改鸡蛋的位置,这样它就不会继续增加分数。
  • @Kimmy 没问题,也谢谢你!整洁的小项目:)
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 2012-10-29
  • 2023-03-05
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多