vb.net中的碰撞检测 [英] Collision detection in vb.net

查看:151
本文介绍了vb.net中的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用视觉基础重新创建Copter,到目前为止,我已经拥有了玩家,屋顶和直升机,但是当玩家触摸地板或屋顶时,似乎无法结束游戏。很抱歉张贴了这么多,这是我第一次来这里,我不知道要粘贴什么。感谢任何帮助:D

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


推荐答案

首先,您需要在地板和天花板区域设置标签

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

这篇关于vb.net中的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆