Moving Picture box Randomly on Form

Joined
Oct 10, 2007
Messages
9
Programming Experience
1-3
I've Seen a Small Programm in which there is a smily face on the screen and when mouse enter in the are of that smily icon that icon randomly moves away from the Cursor but it doesn't move out side from the form, i have written a small program where i try to achieve the goal but sometime it move away from the cursor but sometime it just moves 1 or 2 points


NOTE: Width of The form is 312 and Height is also 312

Dim Rnd as Random


Private Sub picBall_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles picBall.MouseEnter

picBall.Location = New Point(Rnd.Next(Me.Height / 100, Me.Width / 100), Rnd.Next(Me.Height / 50, Me.Width / 50))
Me.Text = "X:" & picBall.Location.X & ", Y:" & picBall.Location.Y

End Sub


Take Care
ENJOY
 
try this

VB.NET:
Public Class form1

    Private Rnd As Random = New Random

    Private Sub picBall_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles picBall.MouseEnter

        picBall.Location = New Point(Rnd.Next(0, Me.Width - picBall.Width), Rnd.Next(0, Me.Height - (picBall.Height + (Me.Height - Me.ClientSize.Height))))
        Me.Text = "X:" & picBall.Location.X & ", Y:" & picBall.Location.Y

    End Sub

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
    End Sub
End Class
 
Why do you call Randomize() ? It has no effect for Random class.
 
No, but it has nothing to do with use of Random class.
 
try this

VB.NET:
Public Class form1

    Private Rnd As Random = New Random

    Private Sub picBall_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles picBall.MouseEnter

        picBall.Location = New Point(Rnd.Next(0, Me.Width - picBall.Width), Rnd.Next(0, Me.Height - (picBall.Height + (Me.Height - Me.ClientSize.Height))))
        Me.Text = "X:" & picBall.Location.X & ", Y:" & picBall.Location.Y

    End Sub

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
    End Sub
End Class



Thank you .Paul. your code works perfectly.
can u help me on collision detection as well?? i mean there are two object on form they are moving randomly on the form and suddenly when both object touch each other a collision detection message will appear , one object i am moving with left and right arrow key to left and right of the form and one object is moving with timer control from upside to down side and vise verce i achieved both here i am attaching sample code.

VB.NET:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim x As Integer = Me.width - 150
    Dim y As Integer = Me.Height - 50
    Dim direction As Boolean = True


 Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        picPadle.Location = New Point(x, picPadle.Location.Y)

 End Sub

 Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        lblPadleX.Text = picPadle.Location.X
        Select Case e.KeyCode
            Case Keys.Left
                picPadle.Location = New Point(x, picPadle.Location.Y)

                If x = 15 Then
                    Exit Select
                Else
                    x -= 5
                End If

            Case Keys.Right
                picPadle.Location = New Point(x, picPadle.Location.Y)
                If x > Me.Width - 100 Then
                    Exit Select
                Else
                    x += 5
                End If
            Case Else
                Exit Sub
        End Select
    End Sub



Private Sub tmrGameControl_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGameControl.Tick
        lblBally.Text = picBall.Location.Y

        If direction = True Then
            If picBall.Location.Y < y Then
                picBall.Location = New Point(picBall.Location.X, picBall.Location.Y + 10)
            Else
                direction = False
            End If
        End If

        If direction = False Then
            If picBall.Location.Y > 15 Then
                picBall.Location = New Point(picBall.Location.X, picBall.Location.Y - 10)
            Else
                direction = True
            End If
        End If

End Sub



Take Care
ENJOY
 
Rectangle.IntersectsWith Method, if you have 2 rectangles A and B then you check if A.IntersectsWith(B)
 
Rectangle.IntersectsWith Method, if you have 2 rectangles A and B then you check if A.IntersectsWith(B)

i have two Picture Boxes named picBall & picpadle on the form picball is moving from upside down and vise versa on the other hand picpadle is moving as i press left or right arrow key from keyboard. there is no rectangle or circle or any shape i am using. thankx for the quick reply


Take Care
ENJOY
 
So your pictureboxes is shapeless? heheh :) Bounds property tell you the control Rectangle.
 
Back
Top