Обнаружение столкновений редактирует Spawn Location в Picture Box - PullRequest
0 голосов
/ 21 апреля 2020

Я делаю двумерную игру, в которой пользователь может отстреливать врагов и набирать очки. До сих пор я добавил границы к уровню, а также обнаружение столкновений между игроком и указанными границами. Когда игрок нажимает «w», пользователь стреляет пулей. Без меня, устанавливающего границы, пуля появляется в центре спрайта игроков. С установленными границами пули, кажется, увеличивают высоту от игрока, когда порождены; когда игрок движется влево от экрана. Наоборот, когда игрок двигается вправо.

Первый класс:

Public Class Bullet
Inherits PictureBox 'this class is a variation of a picture box
Public Sub New() 'every time bullet is accessed it will access this sub as well
    With Me 'refers back to anything in the class
        .Height = 5
        .Width = 2
        .Location = PlayScreen.PlayerShip.Location 'sets the bullets start point to the ship
        .BackColor = Color.White
        .SetBounds(x:=PlayScreen.PlayerShip.Left, y:=PlayScreen.PlayerShip.Left, height:=5, width:=2)
    End With
End Sub

Public Sub ShootUp()
    Me.Top -= 10 'Males the bullet move upwards
End Sub
End Class

Остальная часть моего кода:

Public Class PlayScreen
    Public Shared PlayerShip As New PictureBox 'defines the ship as a picture box
    Dim WallNorth As New PictureBox
    Dim WallSouth As New PictureBox
    Dim WallEast As New PictureBox 'these will be given collision detection to check if the player is trying to exit the boundires of the level
    Dim WallWest As New PictureBox

    Dim Bullets(-1) As Bullet 'makes the array have nothing in it
    Dim BulletCounter As Integer 'used to make new bullets


    Dim MoveRight As Boolean = False
    Dim MoveLeft As Boolean = False 'Making movement based upon boolean factors allows for more user friendly controls
    Dim MoveUp As Boolean = False
    Dim MoveDown As Boolean = False

    Dim PUHealth As New PictureBox

    Dim Health As Integer
    Dim Score As Integer

    Private Sub PlayScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        MovementTimer.Start()
        PUHealthTimer.Start()

        Health = 3
        HealthLbl.Text = "Health: " & Health

        Score = 0
        ScoreLbl.Text = "Score: " & Score

        Me.Controls.Add(PUHealth)
        PUHealth.Width = 25
        PUHealth.Height = 25
        PUHealth.BorderStyle = BorderStyle.FixedSingle
        PUHealth.BackColor = Color.Yellow
        PUHealth.Top = Me.Height / 2 - 100
        PUHealth.Left = Me.Width / 2 - 100
        PUHealth.SetBounds(x:=Me.Height / 2 - 100, y:=Me.Width / 2 - 100, height:=25, width:=25)


        Me.Controls.Add(PlayerShip) 'imports the picture box onto the PlayScreen
        PlayerShip.Width = 40
        PlayerShip.Height = 40 'Dimensions of the player ship
        PlayerShip.BorderStyle = BorderStyle.FixedSingle 'adds a border to the picturebox
        PlayerShip.BackColor = Color.White 'adds colour to the ship background
        PlayerShip.SetBounds(x:=Me.Left, y:=Me.Top, height:=40, width:=40)





        Me.Controls.Add(WallNorth)
        WallNorth.Width = 750
        WallNorth.Height = 5
        WallNorth.BorderStyle = BorderStyle.FixedSingle 'this is defining the wall at the top of the screen, setting its positions as well as its bounds
        WallNorth.BackColor = Color.Green
        WallNorth.Top = 1
        WallNorth.Left = 1
        WallNorth.SetBounds(x:=1, y:=1, height:=5, width:=750)

        Me.Controls.Add(WallEast)
        WallEast.Width = 5
        WallEast.Height = 750
        WallEast.BorderStyle = BorderStyle.FixedSingle 'This defines the wall at the right of the screen, setting its position as well as its bounds
        WallEast.BackColor = Color.Green
        WallEast.Top = 1
        WallEast.Left = 545
        WallEast.SetBounds(x:=545, y:=1, width:=5, height:=750)

        Me.Controls.Add(WallSouth)
        WallSouth.Width = 750
        WallSouth.Height = 5
        WallSouth.BorderStyle = BorderStyle.FixedSingle
        WallSouth.BackColor = Color.Green
        WallSouth.Top = 574
        WallSouth.Left = 1
        WallSouth.SetBounds(x:=1, y:=573, width:=750, height:=5)

        Me.Controls.Add(WallWest)
        WallWest.Width = 5
        WallWest.Height = 750
        WallWest.BorderStyle = BorderStyle.FixedSingle
        WallWest.BackColor = Color.Green
        WallWest.Top = 1
        WallWest.Left = 1



    End Sub

    Private Sub PlayScreen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyValue
            Case Keys.Right
                MoveRight = True
            Case Keys.Left
                MoveLeft = True  'This edits the boolean value of the varibales when the correct key is pressed
            Case Keys.Up
                MoveUp = True
            Case Keys.Down
                MoveDown = True
            Case Keys.W
                ReDim Preserve Bullets(BulletCounter) 'allows the array to expand more efficiently
                Dim Bullet1 As New Bullet 'creates new bullet
                Controls.Add(Bullet1) 'adds bullet to the screen
                Bullets(BulletCounter) = Bullet1 'the new space created in the array is saved as the new bullet made
                BulletCounter += 1
                ShootUpTimer.Start() 'starts the shoot timer



        End Select

        While WallNorth.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Top += 5
        End While

        While WallEast.Bounds.IntersectsWith(PlayerShip.Bounds) 'I have to check if the player has collided with the wall when the key is down
            PlayerShip.Left -= 5
        End While

        While WallSouth.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Top -= 5
        End While

        While WallWest.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Left += 5
        End While


        If PUHealth.Bounds.IntersectsWith(PlayerShip.Bounds) Then 'checks if the player has collided with the the power up
            If PUHealth.Visible() Then 'This stops a bug where the player could go to the spot where the power up use to be and collect health
                Health += 1 'increaeses the players health by one
                HealthLbl.Text = "Health: " & Health 'displays the new player health
                PUHealth.Hide() 'hides the power up
            End If
        End If
    End Sub

    Private Sub PlayScreen_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyValue
            Case Keys.Right
                MoveRight = False
            Case Keys.Left
                MoveLeft = False 'This edits the boolean value when the key has been lifted
            Case Keys.Up
                MoveUp = False
            Case Keys.Down
                MoveDown = False
        End Select

        While WallNorth.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Top += 5
        End While

        While WallEast.Bounds.IntersectsWith(PlayerShip.Bounds) 'i have to check if the player collides with a wall while the key is down
            PlayerShip.Left -= 5
        End While

        While WallSouth.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Top -= 5
        End While


        If PUHealth.Bounds.IntersectsWith(PlayerShip.Bounds) Then
            If PUHealth.Visible() Then
                Health += 1
                HealthLbl.Text = "Health: " & Health
                PUHealth.Hide()
            End If
        End If
    End Sub

    Private Sub MovementTimer_Tick(sender As Object, e As EventArgs) Handles MovementTimer.Tick


        If MoveRight = True Then
            PlayerShip.Left += 5
        End If
        If MoveLeft = True Then
            PlayerShip.Left -= 5
        End If
        If MoveUp = True Then
            PlayerShip.Top -= 5 ' I use a timer to tick every 10 milliseconds ato check the states of each key, this statement controlls the execution of the direction
        End If
        If MoveDown = True Then
            PlayerShip.Top += 5
        End If

        While WallNorth.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Top += 5
        End While

        While WallEast.Bounds.IntersectsWith(PlayerShip.Bounds) 'i check if the player is collided with the wall whilst the timer ticks because the player could not collide with the wall every 20 milliseconds
            PlayerShip.Left -= 5
        End While

        While WallSouth.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Top -= 5
        End While

        While WallWest.Bounds.IntersectsWith(PlayerShip.Bounds)
            PlayerShip.Left += 5
        End While

        If PUHealth.Bounds.IntersectsWith(PlayerShip.Bounds) Then
            If PUHealth.Visible() Then
                Health += 1
                HealthLbl.Text = "Health: " & Health
                PUHealth.Hide()
            End If
        End If
    End Sub

    Private Sub PUHealthTimer_Tick(sender As Object, e As EventArgs) Handles PUHealthTimer.Tick
        If PUHealth.Visible() Then 'checks if the health power up i visible
        Else 'if not the coordinates are randomised and then displayerd
            PUHealth.Top = ((500 * Rnd()) + 10)
            PUHealth.Left = ((500 * Rnd()) + 10)
            PUHealth.Show()
        End If
    End Sub

    Private Sub ShootTimerUp_Tick(sender As Object, e As EventArgs) Handles ShootUpTimer.Tick
        For x = 0 To Bullets.Length - 1 'to check every position within the array apart from the newest
            Bullets(x).ShootUp()
        Next
    End Sub

    End Class

Что мне нужно изменить чтобы пули стреляли с середины, с вершины корабля.

1 Ответ

0 голосов
/ 22 апреля 2020

В Publi c Класс I объявил местоположение X и Y как местоположение x

...