none
How do I make it so if one picture box touches another picture box the form's back ground changes? RRS feed

  • Question

  • Hello,

    I am a beginner in visual basic

    I was wondering if there was a way to make it so that if one picture box touches another picture box

    the background of the fourm changes?


    Sunday, June 3, 2018 8:37 PM

All replies

  • Hi

    Here is some code to illustrate a simple collision between 2 Picture Boxes.

    The .IntersectsWith would be what you are looking for - perhaps. Just add a Timer1 to a new Form1 and replace default code with this code to try it out.

    ' Form1 with Timer1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim pb1, pb2 As New PictureBox
      Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
        With pb1
          .Size = New Size(50, 50)
          .Location = New Point(10, 10)
          .BackColor = Color.Black
        End With
        With pb2
          .Size = New Size(50, 50)
          .Location = New Point(100, 25)
          .BackColor = Color.Blue
        End With
        Controls.AddRange({pb1, pb2})
        With Timer1
          .Interval = 50
          .Enabled = True
        End With
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        pb1.Location = New Point(pb1.Location.X + 1, pb1.Location.Y)
    
        If pb1.Bounds.IntersectsWith(pb2.Bounds) Then
          pb1.BackColor = Color.Red
        Else
          pb1.BackColor = Color.Black
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, June 3, 2018 9:31 PM
    Sunday, June 3, 2018 9:30 PM
  • Well you should use an event to detect the movement of Man or Box to see when they intersect. And then provide a full path to the file of the image you want used for the BackgroundImage.

    I used two panels moving the left panel, Panel1, into the right panel, Panel2.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyCode
                Case Is = Keys.Right
                    Panel1.Location = New Point(Panel1.Location.X + 5, Panel1.Location.Y)
                Case Is = Keys.Left
                    Panel1.Location = New Point(Panel1.Location.X - 5, Panel1.Location.Y)
                Case Is = Keys.Up
                    Panel1.Location = New Point(Panel1.Location.X, Panel1.Location.Y - 5)
                Case Is = Keys.Down
                    Panel1.Location = New Point(Panel1.Location.X, Panel1.Location.Y + 5)
            End Select
        End Sub
    
        Private Sub Panel1_Move(sender As Object, e As EventArgs) Handles Panel1.Move
            If Panel1.Bounds.IntersectsWith(Panel2.Bounds) Then
                Me.BackgroundImage = Image.FromFile("C:\Users\John\Desktop\Lycosa Tarantula.Bmp")
            Else
                Me.BackgroundImage = Image.FromFile("C:\Users\John\Desktop\Background Image.Png")
            End If
        End Sub
    End Class


    La vida loca

    Monday, June 4, 2018 9:36 PM
  • Hi Zero2infinity,

    You can also try the following code, I use Timer to mointor picturebox.location.x.

      Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Image = Image.FromFile("D:\Image\1.jpg")
            PictureBox2.Image = Image.FromFile("D:\Image\2.jpg")
            Timer1.Start()
    
        End Sub
           Dim Offset As Point
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim Pos As Point = Me.PointToClient(MousePosition)
                Pos.Offset(Offset.X, Offset.Y)
                PictureBox1.Location = Pos
            End If
        End Sub
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            Offset = New Point(-e.X, -e.Y)
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If PictureBox1.Location.X + PictureBox1.Size.Width > PictureBox2.Location.X OrElse PictureBox2.Location.X < PictureBox1.Location.X + PictureBox1.Size.Width Then
                Me.BackgroundImage = Image.FromFile("D:\Image\4.jpg")
            Else
                Me.BackgroundImage = Image.FromFile("D:\Image\3.jpg")
            End If
    
        End Sub
    
        Private Sub PictureBox2_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseDown
            Offset = New Point(-e.X, -e.Y)
        End Sub
        Private Sub PictureBox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim Pos As Point = Me.PointToClient(MousePosition)
                Pos.Offset(Offset.X, Offset.Y)
                PictureBox2.Location = Pos
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, June 5, 2018 3:23 AM
    Moderator
  • As noted, the key problem is that you have your test for intersection in the Form.Load event handler.  This code only executes once after the form is loaded for the first time so there is no way for it to continue to test for collision.  You should move the test into whatever code you use to move the "Man" object.  This might be a mouse or keyboard event handler... whatever you are using for user input.  After moving the man based on user input, test for collision with the box.

    That said, you may soon find that using panels or picture boxes does not lend itself well to a game.  The collision looks sloppy; your man will collide when the dotted box around him hits the other object - visually this will still be several pixels away from an "actual visible collision" due to the transparency around the man.  You also cannot easily rotate or animate the images, if that later becomes desirable.

    I've spoken a lot about making games in the past; if you search these forums for "video games vb.net" you should find a lot of relevant material.  You may also want to review and/or utilize my PuppyBreath game engine implementation for Windows Forms.  While I've stalled a bit on some of the planned future features, the framework is already quite robust and should give you a good leg-up on developing this kind of side-scrolling game.  The GitHub page includes links to a NuGet installation as well as a simple walk-through example of a top-down game.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, June 5, 2018 12:06 PM
    Moderator