Answers for "move player with keyboard with boundaries vb.net"

VBA
0

move player with keyboard with boundaries vb.net

'Note: before I did the code, I added 2 labels, 4 timers 
'(names are "tmrRight", "tmrleft", "tmrUp", and "tmrDown") 
'and 3 pictureboxes (one of which is named "pbPlayer")

'the 2 labels and 2 pictureboxes (unnamed) are the boundaries of the player

Public Class Form1
    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode
            Case Keys.Right
                If pbPlayer.Bounds.IntersectsWith(PictureBox2.Bounds) Then
                    tmrRight.Stop()
                Else
                    tmrRight.Start()
                End If
            Case Keys.Left
                If pbPlayer.Bounds.IntersectsWith(PictureBox1.Bounds) Then
                    tmrLeft.Stop()
                Else
                    tmrLeft.Start()
                End If
            Case Keys.Up
                If pbPlayer.Bounds.IntersectsWith(Label1.Bounds) Then
                    tmrUp.Stop()
                Else
                    tmrUp.Start()
                End If
            Case Keys.Down
                If pbPlayer.Bounds.IntersectsWith(Label2.Bounds) Then
                    tmrDown.Stop()
                Else
                    tmrDown.Start()
                End If
        End Select
    End Sub

    Private Sub tmrRight_Tick(sender As Object, e As EventArgs) Handles tmrRight.Tick
        pbPlayer.Left += 10
    End Sub

    Private Sub tmrLeft_Tick(sender As Object, e As EventArgs) Handles tmrLeft.Tick
        pbPlayer.Left -= 10
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
        tmrLeft.Stop()
        tmrRight.Stop()
        tmrUp.Stop()
        tmrDown.Stop()
    End Sub

    Private Sub tmrUp_Tick(sender As Object, e As EventArgs) Handles tmrUp.Tick
        pbPlayer.Top -= 10
    End Sub

    Private Sub tmrDown_Tick(sender As Object, e As EventArgs) Handles tmrDown.Tick
        pbPlayer.Top += 10
    End Sub
End Class
Posted by: Guest on September-21-2020

Code answers related to "move player with keyboard with boundaries vb.net"

Code answers related to "VBA"

Browse Popular Code Answers by Language