none
Collision detection between picturebox and rectangle RRS feed

  • Question

  • I have a maze procedurally generated using rectangles and need my player (a picturebox) to be unable to pass through them.

    I can't find any guidance re how to do collision detection between a rectangle and a picturebox Most maze games use labels or pictureboxes and collision detection works between these, but for some reason doesn't between rectangles and picturebox. Not asking someone to do it for me but a simple example of how this works, if it can, would really help

     
    • Edited by CrazyMum123 Thursday, November 16, 2017 12:56 PM
    Thursday, November 16, 2017 12:54 PM

Answers

  • Hi

    Here is some code to show a collision between a PictureBox and a drawn rectangle. All this example does is to change the border colour of the rectangle when there is a collision - you would need to decide what action you need to take for your scenario.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim pb As New PictureBox()
        Dim r As New Rectangle(100, 100, 50, 50)
        Dim myPen As Pen
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With pb
                .Image = Image.FromFile(Application.StartupPath & "\Data\42.png")
                .Size = New Size(50, 50)
                .SizeMode = PictureBoxSizeMode.Zoom
                .Location = New Point(50, 50)
                .BorderStyle = BorderStyle.FixedSingle
            End With
            myPen = Pens.Black
            Controls.Add(pb)
            DoubleBuffered = True
        End Sub
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(myPen, r)
        End Sub
        Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            pb.Location = PointToClient(Cursor.Position)
            If pb.Bounds.IntersectsWith(r) Then
                myPen = Pens.Red
            Else
                myPen = Pens.Black
            End If
            Invalidate()
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by CrazyMum123 Saturday, November 18, 2017 8:23 PM
    Thursday, November 16, 2017 2:12 PM
  • Hi

    Here is a hurried example you might like to check out. It uses a list of walls with their dimensions/locations/fill color/border color. It also has an amended collision detection.

    See the Class definition at the bottom of the code.

    This example only uses 3 walls,but of course, you could have any number.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Walls As New List(Of Wall)
        ' This subroutine positions the maze panel on the screen
        Sub Dolayout()
            With Panel1
                .Top = 50
                .Left = 50
                .Width = 330
                .Height = 330
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
            End With
    
            player.Size = New Size(30, 30)
            player.Location = New Point(5, 5)
            player.BorderStyle = BorderStyle.FixedSingle
    
        End Sub
        'Loads form1 and runs sub routine Dolayout
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim wall1, wall2, wall3 As New Wall
            With wall1
                .x = 150
                .y = 150
                .Height = 200
                .Width = 50
                .FillCol = Brushes.Blue
                .BorderCol = Pens.Red
            End With
            With wall2
                .x = 50
                .y = 0
                .Height = 200
                .Width = 50
                .FillCol = Brushes.Red
                .BorderCol = Pens.Blue
            End With
            With wall3
                .x = 260
                .y = 80
                .Height = 100
                .Width = 100
                .FillCol = Brushes.Green
                .BorderCol = Pens.Black
            End With
            Walls.AddRange({wall1, wall2, wall3})
    
            Dolayout()
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    
            For Each w As Wall In Walls
                Dim r As New Rectangle(w.x, w.y, w.Width, w.Height)
                e.Graphics.FillRectangle(w.FillCol, r)
                e.Graphics.DrawRectangle(w.BorderCol, r)
            Next
    
        End Sub
    
        Sub bordercheck()
            If player.Left < 0 Then player.Location = New Point(0, player.Top)
            If player.Top < 0 Then player.Location = New Point(player.Left, 0)
            If (player.Left + player.Width) > Panel1.Width Then player.Location = New Point(Panel1.Width - player.Width, player.Top)
            If (player.Top + player.Height) > Panel1.Height Then player.Location = New Point(player.Left, Panel1.Height - player.Height)
        End Sub
    
        Function collisioncheck() As Boolean
            Dim iscollision As Boolean = False
            For Each w As Wall In Walls
                Dim r As New Rectangle(w.x, w.y, w.Width, w.Height)
                If player.Bounds.IntersectsWith(r) Then
                    Return True
                End If
            Next
            Return False
        End Function
    
    
        Private Sub panel1_keydown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    
            'UP
            If e.KeyCode = Keys.Up Then
                player.Top = player.Top - 10
                bordercheck()
                If collisioncheck() Then player.Top += 10
            End If
            'Down
            If e.KeyCode = Keys.Down Then
                player.Top = player.Top + 10
                bordercheck()
                If collisioncheck() Then player.Top -= 10
            End If
            'Left
            If e.KeyCode = Keys.Left Then
                player.Left = player.Left - 10
                bordercheck()
                If collisioncheck() Then player.Left += 10
            End If
    
            'right
            If e.KeyCode = Keys.Right Then
                player.Left = player.Left + 10
                bordercheck()
                If collisioncheck() Then player.Left -= 10
            End If
    
        End Sub
        Class Wall
            Property x As Integer
            Property y As Integer
            Property Height As Integer
            Property Width As Integer
            Property FillCol As Brush
            Property BorderCol As Pen
        End Class
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, November 20, 2017 4:00 PM amended colors
    • Marked as answer by CrazyMum123 Monday, November 27, 2017 1:34 PM
    Monday, November 20, 2017 3:54 PM
  • Hi

    If you find any post answers your question, even partially, then you can mark as answer, and any post which helps you to arrive at your answer may merit  a Vote. You can mark as answer as many posts as you feel answered your question and likewise for the Votes.

    You should start a new thread once your thread has been answered.


    Regards Les, Livingston, Scotland

    • Marked as answer by CrazyMum123 Monday, November 27, 2017 1:34 PM
    Monday, November 27, 2017 1:28 PM

All replies

  • Please show us the code you use to make the "rectangles".

    For example do you draw them in the paint event? Or where do the rectangles come from?

    Thursday, November 16, 2017 1:15 PM
  • Hi

    Here is some code to show a collision between a PictureBox and a drawn rectangle. All this example does is to change the border colour of the rectangle when there is a collision - you would need to decide what action you need to take for your scenario.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim pb As New PictureBox()
        Dim r As New Rectangle(100, 100, 50, 50)
        Dim myPen As Pen
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With pb
                .Image = Image.FromFile(Application.StartupPath & "\Data\42.png")
                .Size = New Size(50, 50)
                .SizeMode = PictureBoxSizeMode.Zoom
                .Location = New Point(50, 50)
                .BorderStyle = BorderStyle.FixedSingle
            End With
            myPen = Pens.Black
            Controls.Add(pb)
            DoubleBuffered = True
        End Sub
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(myPen, r)
        End Sub
        Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            pb.Location = PointToClient(Cursor.Position)
            If pb.Bounds.IntersectsWith(r) Then
                myPen = Pens.Red
            Else
                myPen = Pens.Black
            End If
            Invalidate()
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by CrazyMum123 Saturday, November 18, 2017 8:23 PM
    Thursday, November 16, 2017 2:12 PM
  • here is my rectangle code 

        Sub panel1_paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            dolayout()
            centrelinev = (mazewidth - cellw) / 2
            centrelineh = (mazeheight - cellh) / 2

            'create Pen
            Dim bluebrush As New SolidBrush(Color.Blue)
            Dim redbrush As New SolidBrush(Color.Red)
            ' spec rectangles
            Dim Orectangle1 As New Rectangle(centrelinev, 0, cellw, mazeheight)
            Dim Orectangle2 As New Rectangle(0, centrelineh, mazewidth, cellh)

            'draw rectangles
            e.Graphics.FillRectangle(bluebrush, Orectangle1)
            e.Graphics.FillRectangle(redbrush, Orectangle2)


        End Sub

    The picture box is created in the panel using the toolbox

    thank you for looking at this for me



    • Edited by CrazyMum123 Thursday, November 16, 2017 7:24 PM
    Thursday, November 16, 2017 6:47 PM
  • Hi

    Please edit your post for readability, and, use the code block tool from the toolbar to post  code.


    Regards Les, Livingston, Scotland

    Thursday, November 16, 2017 7:14 PM
  • thank you so much - I have used this to work with key controls  and form the base of a maze game where the walls (rectangles) are procedurally generated. This has got me past a sticking point I have been on for days and I can now see how I can finish my game
    Thursday, November 16, 2017 7:58 PM
  • thank you so much - I have used this to work with key controls  and form the base of a maze game where the walls (rectangles) are procedurally generated. This has got me past a sticking point I have been on for days and I can now see how I can finish my game

    Mum,

    You should mark leshay's post as the answer using the link below the post if it answered your question.

    Thursday, November 16, 2017 10:13 PM
  • This works in a form but not in a panel inside a form - can you help me understand what I'm doing wrong please ?

    Thanks - the code is below

    Public Class Form1
        Dim iscollision As Boolean
        Private wall As Rectangle
    
        ' This subroutine positions the maze panel on the screen
        Sub Dolayout()
            Panel1.Top = 50
            Panel1.Left = 50
            Panel1.Width = 330
            Panel1.Height = 330
    
            player.Size = New Size(25, 25)
            player.Location = New Point(50, 50)
            player.BorderStyle = BorderStyle.FixedSingle
    
        End Sub
        'Loads form1 and runs sub routine Dolayout
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dolayout()
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    
            Dim wall As New Rectangle(150, 150, 50, 200)
            Dim mypen As Pen
            mypen = Pens.Black
            e.Graphics.DrawRectangle(mypen, wall)
    
        End Sub
    
        Sub bordercheck()
            If player.Left < 0 Then player.Location = New Point(0, player.Top)
            If player.Top < 0 Then player.Location = New Point(player.Left, 0)
            If (player.Left + player.Width) > Panel1.Width Then player.Location = New Point(Panel1.Width - player.Width, player.Top)
            If (player.Top + player.Height) > Panel1.Height Then player.Location = New Point(player.Left, Panel1.Height - player.Height)
        End Sub
    
        Sub collisioncheck()
            If player.Bounds.IntersectsWith(wall) Then
                iscollision = True
            Else
    
                iscollision = False
            End If
    
    
        End Sub
    
    
        Private Sub panel1_keydown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            ' Dim flag As Boolean
            'UP
            If e.KeyCode = Keys.Up Then
                player.Top = player.Top - 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Top = player.Top + 10
                End If
            End If
            'Down
            If e.KeyCode = Keys.Down Then
                player.Top = player.Top + 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Top = player.Top - 10
                End If
            End If
            'Left
            If e.KeyCode = Keys.Left Then
                player.Left = player.Left - 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Left = player.Left + 10
                End If
    
            End If
    
            'right
            If e.KeyCode = Keys.Right Then
                player.Left = player.Left + 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Left = player.Left - 10
                End If
            End If
    
        End Sub
    End Class

    Sunday, November 19, 2017 7:07 PM
  • Hi         COPIED OVER TO YOUR OTHER THREAD

    Try this:

    Move the line

        Dim wall As New Rectangle(150, 150, 50, 200)

    out of the Paint routine and delete/replace the other 'wall' declaration with it.

    What, btw, is 'player' not declared anywhere. I just added a Panel for that.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim iscollision As Boolean
        Private player As New Panel
        Dim wall As New Rectangle(150, 150, 50, 200)
        ' This subroutine positions the maze panel on the screen
        Sub Dolayout()
            Panel1.Top = 50
            Panel1.Left = 50
            Panel1.Width = 330
            Panel1.Height = 330
    
            player.Size = New Size(25, 25)
            player.Location = New Point(10, 10)
            player.BorderStyle = BorderStyle.FixedSingle
            Panel1.Controls.Add(player)
        End Sub
        'Loads form1 and runs sub routine Dolayout
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dolayout()
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    
            Dim mypen As Pen
            mypen = Pens.Black
            e.Graphics.DrawRectangle(mypen, wall)
    
        End Sub
    
        Sub bordercheck()
            If player.Left < 0 Then player.Location = New Point(0, player.Top)
            If player.Top < 0 Then player.Location = New Point(player.Left, 0)
            If (player.Left + player.Width) > Panel1.Width Then player.Location = New Point(Panel1.Width - player.Width, player.Top)
            If (player.Top + player.Height) > Panel1.Height Then player.Location = New Point(player.Left, Panel1.Height - player.Height)
        End Sub
    
        Sub collisioncheck()
            If player.Bounds.IntersectsWith(wall) Then
                iscollision = True
            Else
    
                iscollision = False
            End If
    
    
        End Sub
    
    
        Private Sub panel1_keydown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            ' Dim flag As Boolean
            'UP
            If e.KeyCode = Keys.Up Then
                player.Top = player.Top - 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Top = player.Top + 10
                End If
            End If
            'Down
            If e.KeyCode = Keys.Down Then
                player.Top = player.Top + 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Top = player.Top - 10
                End If
            End If
            'Left
            If e.KeyCode = Keys.Left Then
                player.Left = player.Left - 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Left = player.Left + 10
                End If
    
            End If
    
            'right
            If e.KeyCode = Keys.Right Then
                player.Left = player.Left + 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Left = player.Left - 10
                End If
            End If
    
        End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, November 19, 2017 9:01 PM
    Sunday, November 19, 2017 8:55 PM
  • Apologies, the player was a picture box drawn in the panel using the toolbox. Moving the line works, but the reason I put it in the paint event initially was I was planning to make the rectangle parameters randomly generated and have multiple walls.

    To test this I moved the wall declaration as you said - it worked, then I tried amending the wall1 declaration to 

    Dim wall1 As New Rectangle(x, y, wallwidth, wallheight)
        Dim x As Integer
        Dim y As Integer
        Dim wallwidth As Integer
        Dim wallheight As Integer

     and then specifying these parameters in the paint subroutine but then the rectangle didn't print at all. Hints please?  

    Monday, November 20, 2017 10:39 AM
  • Hi

    Well.if that code is exactly as it appears in your code editor then if you look closely, you will see that some variables have been declared AFTER they have been used - which of course,won't work.mIfmthat is the issue, then just move the Dim Wall1 line below the others.


    Regards Les, Livingston, Scotland

    Monday, November 20, 2017 1:45 PM
  • The following code works as long as I specify the values outside the paint event - once I try and put them in the paint event my wall does not appear. Do I need to use a function? Sorry to be so dim, but trying to teach myself, while off long-term sick,  and struggling as you can see

     
    Public Class Form1
        Dim iscollision As Boolean
        'Dim wall1 As New Rectangle''150, 150, 50, 200)
    
        Dim xcoord As Integer = 150
        Dim ycoord As Integer = 150
        Dim wallwidth As Integer = 50
        Dim wallheight As Integer = 200
        Dim wall1 As New Rectangle(xcoord, ycoord, wallwidth, wallheight)
    
        ' This subroutine positions the maze panel on the screen
        Sub Dolayout()
            Panel1.Top = 50
            Panel1.Left = 50
            Panel1.Width = 330
            Panel1.Height = 330
    
            player.Size = New Size(30, 30)
            player.Location = New Point(50, 50)
            player.BorderStyle = BorderStyle.FixedSingle
    
        End Sub
        'Loads form1 and runs sub routine Dolayout
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dolayout()
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    
            '  xcoord = CInt(150)
            ' ycoord = CInt(100)
    
            'wallheight = 200
            ' wallwidth = 50
    
            Dim mypen As Pen
            mypen = Pens.Black
            e.Graphics.DrawRectangle(mypen, wall1)
            Dim redbrush As New SolidBrush(Color.Red)
            e.Graphics.FillRectangle(redbrush, wall1)
    
        End Sub
    
        Sub bordercheck()
            If player.Left < 0 Then player.Location = New Point(0, player.Top)
            If player.Top < 0 Then player.Location = New Point(player.Left, 0)
            If (player.Left + player.Width) > Panel1.Width Then player.Location = New Point(Panel1.Width - player.Width, player.Top)
            If (player.Top + player.Height) > Panel1.Height Then player.Location = New Point(player.Left, Panel1.Height - player.Height)
        End Sub
    
        Sub collisioncheck()
            If player.Bounds.IntersectsWith(wall1) Then
                iscollision = True
            Else
                iscollision = False
            End If
    
    
        End Sub
    
    
        Private Sub panel1_keydown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    
            'UP
            If e.KeyCode = Keys.Up Then
                player.Top = player.Top - 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Top = player.Top + 10
                End If
            End If
            'Down
            If e.KeyCode = Keys.Down Then
                player.Top = player.Top + 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Top = player.Top - 10
                End If
            End If
            'Left
            If e.KeyCode = Keys.Left Then
                player.Left = player.Left - 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Left = player.Left + 10
                End If
    
            End If
    
            'right
            If e.KeyCode = Keys.Right Then
                player.Left = player.Left + 10
                bordercheck()
                collisioncheck()
                If iscollision = True Then
                    player.Left = player.Left - 10
                End If
            End If
    
        End Sub
    End Class

    Monday, November 20, 2017 2:04 PM
  • The following code works as long as I specify the values outside the paint event - once I try and put them in the paint event my wall does not appear. Do I need to use a function? Sorry to be so dim, but trying to teach myself, while off long-term sick,  and struggling as you can see


    Hi

    The code does work (as you said), but I am confused where you say 'try and put them in the paint event'.

    Do you mean that you want to adjust the Wall size and Location from within the PaintEvent?

    Here is another variation that might be what you are asking about:the Wall is sized/located from within the PaintEvent.

    Partial Code

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim iscollision As Boolean
        'Dim wall1 As New Rectangle''150, 150, 50, 200)
    
        Dim xcoord As Integer = 150
        Dim ycoord As Integer = 150
        Dim wallwidth As Integer = 50
        Dim wallheight As Integer = 200
        Dim wall1 As New Rectangle
    
        ' This subroutine positions the maze panel on the screen
        Sub Dolayout()
            Panel1.Top = 50
            Panel1.Left = 50
            Panel1.Width = 330
            Panel1.Height = 330
    
            player.Size = New Size(30, 30)
            player.Location = New Point(50, 50)
            player.BorderStyle = BorderStyle.FixedSingle
    
        End Sub
        'Loads form1 and runs sub routine Dolayout
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dolayout()
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    
            wall1 = New Rectangle(xcoord, ycoord, wallwidth, wallheight)
    
            Dim mypen As Pen
            mypen = Pens.Black
            e.Graphics.DrawRectangle(mypen, wall1)
            Dim redbrush As New SolidBrush(Color.Red)
            e.Graphics.FillRectangle(redbrush, wall1)
    
        End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, November 20, 2017 2:24 PM
    Monday, November 20, 2017 2:19 PM
  • Yes, as I want to draw several walls and so reset the coordinates and width and height several times 

    Monday, November 20, 2017 2:22 PM
  • Hi

    With the code I added to my last post, the variables making up the Wall can be changed anywhere outside the PaintEvent and will be reflected immediately.

    So, for example,changing any/all of

    (xcoord, ycoord, wallwidth, wallheight

    somewhere else will redraw the wallwith the new values when next the Paint event is fired. You can force an update by using Panel1.Invalidate which will immediatelycause the PaintEvent to be called.


    Regards Les, Livingston, Scotland

    Monday, November 20, 2017 2:47 PM
  • Hi

    Here is a hurried example you might like to check out. It uses a list of walls with their dimensions/locations/fill color/border color. It also has an amended collision detection.

    See the Class definition at the bottom of the code.

    This example only uses 3 walls,but of course, you could have any number.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim Walls As New List(Of Wall)
        ' This subroutine positions the maze panel on the screen
        Sub Dolayout()
            With Panel1
                .Top = 50
                .Left = 50
                .Width = 330
                .Height = 330
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
            End With
    
            player.Size = New Size(30, 30)
            player.Location = New Point(5, 5)
            player.BorderStyle = BorderStyle.FixedSingle
    
        End Sub
        'Loads form1 and runs sub routine Dolayout
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim wall1, wall2, wall3 As New Wall
            With wall1
                .x = 150
                .y = 150
                .Height = 200
                .Width = 50
                .FillCol = Brushes.Blue
                .BorderCol = Pens.Red
            End With
            With wall2
                .x = 50
                .y = 0
                .Height = 200
                .Width = 50
                .FillCol = Brushes.Red
                .BorderCol = Pens.Blue
            End With
            With wall3
                .x = 260
                .y = 80
                .Height = 100
                .Width = 100
                .FillCol = Brushes.Green
                .BorderCol = Pens.Black
            End With
            Walls.AddRange({wall1, wall2, wall3})
    
            Dolayout()
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    
            For Each w As Wall In Walls
                Dim r As New Rectangle(w.x, w.y, w.Width, w.Height)
                e.Graphics.FillRectangle(w.FillCol, r)
                e.Graphics.DrawRectangle(w.BorderCol, r)
            Next
    
        End Sub
    
        Sub bordercheck()
            If player.Left < 0 Then player.Location = New Point(0, player.Top)
            If player.Top < 0 Then player.Location = New Point(player.Left, 0)
            If (player.Left + player.Width) > Panel1.Width Then player.Location = New Point(Panel1.Width - player.Width, player.Top)
            If (player.Top + player.Height) > Panel1.Height Then player.Location = New Point(player.Left, Panel1.Height - player.Height)
        End Sub
    
        Function collisioncheck() As Boolean
            Dim iscollision As Boolean = False
            For Each w As Wall In Walls
                Dim r As New Rectangle(w.x, w.y, w.Width, w.Height)
                If player.Bounds.IntersectsWith(r) Then
                    Return True
                End If
            Next
            Return False
        End Function
    
    
        Private Sub panel1_keydown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    
            'UP
            If e.KeyCode = Keys.Up Then
                player.Top = player.Top - 10
                bordercheck()
                If collisioncheck() Then player.Top += 10
            End If
            'Down
            If e.KeyCode = Keys.Down Then
                player.Top = player.Top + 10
                bordercheck()
                If collisioncheck() Then player.Top -= 10
            End If
            'Left
            If e.KeyCode = Keys.Left Then
                player.Left = player.Left - 10
                bordercheck()
                If collisioncheck() Then player.Left += 10
            End If
    
            'right
            If e.KeyCode = Keys.Right Then
                player.Left = player.Left + 10
                bordercheck()
                If collisioncheck() Then player.Left -= 10
            End If
    
        End Sub
        Class Wall
            Property x As Integer
            Property y As Integer
            Property Height As Integer
            Property Width As Integer
            Property FillCol As Brush
            Property BorderCol As Pen
        End Class
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, November 20, 2017 4:00 PM amended colors
    • Marked as answer by CrazyMum123 Monday, November 27, 2017 1:34 PM
    Monday, November 20, 2017 3:54 PM
  • apologies for not being online for a while. I like the above code above a lot.  I have a related question - so do I mark this as solved and start a new thread or simply add here ?

    Monday, November 27, 2017 1:07 PM
  • Hi

    If you find any post answers your question, even partially, then you can mark as answer, and any post which helps you to arrive at your answer may merit  a Vote. You can mark as answer as many posts as you feel answered your question and likewise for the Votes.

    You should start a new thread once your thread has been answered.


    Regards Les, Livingston, Scotland

    • Marked as answer by CrazyMum123 Monday, November 27, 2017 1:34 PM
    Monday, November 27, 2017 1:28 PM
  • Thanks, I will post my new query separately.
    Monday, November 27, 2017 1:35 PM