Collision detection in vb.net

落花浮王杯 提交于 2019-12-11 07:11:37

问题


I am trying to recreate Copter in visual basic, so far I have the player, roof and helicopter but I can't seem to get the game to end when the player touches the floor or roof. Sorry for posting so much, this is my first time on here and I didn't know what to paste. Any help is appreciated :D

Public Class Form1
    Dim pb_field As PictureBox

    Private Sub create_field()
        pb_field = New PictureBox
        With pb_field
            .Top = 20
            .Left = 20
            .Width = 500
            .Height = 300
            .BackColor = Color.Black
        End With
        Me.Controls.Add(pb_field)
        pb_field.BringToFront()
    End Sub

    Dim pb_player As PictureBox

    Private Sub create_player()
        pb_player = New PictureBox
        With pb_player
            .Width = 20
            .Height = 20
            .BackColor = Color.Red
            .Top = pb_field.Top + pb_field.Bottom / 2
            .Left = pb_field.Left + 20
        End With
        Me.Controls.Add(pb_player)
        pb_player.BringToFront()
    End Sub

#Region "Roof Stuff"
    Dim roof(10000) As PictureBox
    Dim num_of_roof As Integer = -1
    Dim r As New Random

    Private Sub create_roof()
        num_of_roof += 1
        roof(num_of_roof) = New PictureBox
        With roof(num_of_roof)
            .Top = pb_field.Top
            .Left = pb_field.Right
            .Height = r.Next(20, 40)
            .Width = 20
            .BackColor = Color.RoyalBlue
        End With
        Me.Controls.Add(roof(num_of_roof))
        roof(num_of_roof).BringToFront()
    End Sub
#End Region

#Region "floor Stuff"
    Dim floor(10000) As PictureBox
    Dim num_of_floor As Integer = -1

    Private Sub create_floor()
        num_of_floor += 1
        floor(num_of_floor) = New PictureBox
        With floor(num_of_floor)
            .Left = pb_field.Right
            .Height = r.Next(20, 40)
            .Width = 20
            .Top = pb_field.Bottom - floor(num_of_floor).Height
            .BackColor = Color.YellowGreen
        End With
        Me.Controls.Add(floor(num_of_floor))
        floor(num_of_floor).BringToFront()
    End Sub
#End Region

    Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
        Me.Text = e.KeyChar
        If e.KeyChar = "w" Then
            pb_player.Top -= 10
        End If
        **Dim collision As Boolean
        For Each PictureBox In Me.Controls
            If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
                collision = True
            Exit For
            Else : collision = False
            End If
            If collision = True Then
                MessageBox.Show("Unlucky,better luck next time!")
            End If**
        Next
    End Sub


    Private Sub form1_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        create_field()
        create_roof()
        create_player()
        tm_background.Start()
        tm_gravity.Start()
    End Sub

    Private Sub tm_background_Tick(sender As Object, e As EventArgs) Handles tm_background.Tick
        For i = 0 To num_of_roof
            roof(i).Left -= 20
            If roof(i).Left < pb_field.Left Then
                Me.Controls.Remove(roof(i))
            End If
        Next
        create_roof()
        For i = 0 To num_of_floor
            floor(i).Left -= 20
            If floor(i).Left < pb_field.Left Then
                Me.Controls.Remove(floor(i))
            End If
        Next
        create_floor()
    End Sub

    Private Sub tm_gravity_Tick(sender As Object, e As EventArgs) Handles tm_gravity.Tick
        pb_player.Top += 5
    End Sub

This is the code I was attempting to use after looking online at possible solutions

Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) HandlesMe.KeyPress
    Me.Text = e.KeyChar
    If e.KeyChar = "w" Then
        pb_player.Top -= 10
    End If
    Dim collision As Boolean
    For Each PictureBox In Me.Controls
        If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
            collision = True
        Exit For
        Else : collision = False
        End If
        If collision = True Then
            MessageBox.Show("Unlucky,better luck next time!")

        End If
    Next
End Sub

回答1:


Your problem is where you exit the for loop before displaying the message:

   For Each PictureBox In Me.Controls
        If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
            collision = True
            Exit For ' Note that exiting skips your check after the End If below
        Else : collision = False
        End If
        ' Whenever this is true you have already exited your 'for' loop
        If collision = True Then 
            MessageBox.Show("Unlucky,better luck next time!")
        End If
   Next

Instead you need something like this where you evaluate the condition after the loop:

   For Each PictureBox In Me.Controls
        If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
            collision = True
        Exit For
        Else : collision = False
        End If
    Next

   If collision = True Then 
      MessageBox.Show("Unlucky,better luck next time!")
   End If



回答2:


First you need to set tag's in the floor and ceiling regions

    With floor(num_of_floor)
        .Tag = "boundaries"

Then you can refer to each picturebox in your controls

 For Each box As PictureBox In Me.Controls
        If box.Tag <> "boundaries" Then Continue For
        If pb_player.Bounds.IntersectsWith(box.Bounds) Then
            collision = True
            Exit For
        Else : collision = False
        End If
    Next

However, you are still going to have a problem that when it hits the floor it will not pass as a collision, because all this code is going on in the key press,

If a user lets the coptor fall, it will only lose the next time they click on the keyboard



来源:https://stackoverflow.com/questions/23299125/collision-detection-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!