i am writing a simple program that has a moving image. i want to add another image that moves randomly around the screen. how do i make the images start at random places on the screen. then move the second image move at random.
Printable View
i am writing a simple program that has a moving image. i want to add another image that moves randomly around the screen. how do i make the images start at random places on the screen. then move the second image move at random.
Welcome to the forums.
Are you talking about having a window with images moving around on it, or do you want a number of images moving around on the desktop?
sorry yes the images are moving around in a frame.
Shame. I quite liked the example I put together assuming that you wanted them all over the screen.
VB Code:
Option Explicit Private Sub Form_Load() With Timer1 .Enabled = (Forms.Count = 1) .Interval = 100 End With BorderStyle = 0 Left = (Screen.Width - Width) / 2 Top = (Screen.Height - Height) / 2 Picture = LoadPicture("c:\picture.jpg") If Forms.Count < 10 Then Dim f As New Form1 f.Show End If End Sub Private Sub Timer1_Timer() Dim f As Form For Each f In Forms f.Left = f.Left + Rnd * 1000 - Rnd * 1000 If f.Left < 0 Then f.Left = 0 If f.Left > Screen.Width - f.Width Then f.Left = Screen.Width - f.Width If f.Top < 0 Then f.Top = 0 If f.Top > Screen.Height - f.Height Then f.Top = Screen.Height - f.Height f.Top = f.Top + Rnd * 1000 - Rnd * 1000 Next End Sub
Can you adapt the above example to do what you want?
thanks i have a go let you know how i get on
Just for interest sake:
Also Note:
NCCLog is the name of the picture
To Bounce on the screen walls do this:VB Code:
Private Sub TNCC_Timer() 'you could call either of the listed subs in the timer event MoveNCC 'or MoveUPD 'or MoveLER End SubTo move up and down, Down and up (and right)VB Code:
Private Sub MoveNCC() Select Case Motion Case 1 ' Move the graphic left and up by 20 twips using the Move method. NCCLog.Move NCCLog.Left - 20, NCCLog.Top - 20 ' If the graphic reaches the left edge of the form, move it to the right and up. If NCCLog.Left <= 0 Then Motion = 2 ' If the graphic reaches the top edge of the form, move it to the left and down. ElseIf NCCLog.Top <= 0 Then Motion = 4 End If Case 2 ' Move the graphic right and up by 20 twips. NCCLog.Move NCCLog.Left + 20, NCCLog.Top - 20 ' If the graphic reaches the right edge of the form, move it to the left and up. ' Routine determines the right edge of the form by subtracting the graphic ' width from the form width. If NCCLog.Left >= (Width - NCCLog.Width) Then Motion = 1 ' If the graphic reaches the top edge of the form, move it to the right and down. ElseIf NCCLog.Top <= 0 Then Motion = 3 End If Case 3 ' Move the graphic right and down by 20 twips. NCCLog.Move NCCLog.Left + 20, NCCLog.Top + 20 ' If the graphic reaches the right edge of the form, move it to the left and down. If NCCLog.Left >= (Width - NCCLog.Width) Then Motion = 4 ' If the graphic reaches the bottom edge of the form, move it to the right and up ' Routine determines the bottom of the form by subtracting ' the graphic height from the form height less 680 twips for the height ' of title bar and menu bar. ElseIf NCCLog.Top >= (Height - NCCLog.Height) - 680 Then Motion = 2 End If Case 4 ' Move the graphic left and down by 20 twips. NCCLog.Move NCCLog.Left - 20, NCCLog.Top + 20 ' If the graphic reaches the left edge of the form, move it to the right and down. If NCCLog.Left <= 0 Then Motion = 3 ' If the graphic reaches the bottom edge of the form, move it to the left and up. ElseIf NCCLog.Top >= (Height - NCCLog.Height) - 680 Then Motion = 1 End If End Select End Sub
To move left to right, Right to left (and down)VB Code:
Private Sub MoveUPD() Static MDown As Boolean Static NShiftToRight As Boolean Select Case True Case NCCLog.Left + NCCLog.Width >= Me.Width NCCLog.Move 0, 0 MDown = True Case NCCLog.Top <= 0 MDown = True If NShiftToRight Then NCCLog.Left = NCCLog.Left + NCCLog.Width NShiftToRight = False End If Case NCCLog.Top + NCCLog.Width >= Me.Height MDown = False NShiftToRight = True NCCLog.Left = NCCLog.Left + NCCLog.Width End Select If MDown Then NCCLog.Top = NCCLog.Top + NCCLog.Height Else NCCLog.Top = NCCLog.Top - NCCLog.Height End If End Sub
VB Code:
Private Sub MoveLER() Static MRight As Boolean Static NShiftToBottom As Boolean Select Case True Case NCCLog.Top + NCCLog.Height >= Me.Height NCCLog.Move 0, 0 MRight = True Case NCCLog.Left <= 0 MRight = True If NShiftToBottom = True Then NCCLog.Top = NCCLog.Top + NCCLog.Height NShiftToBottom = False End If Case NCCLog.Left + NCCLog.Height >= Me.Width MRight = False NShiftToBottom = True NCCLog.Top = NCCLog.Top + NCCLog.Height End Select If MRight Then NCCLog.Left = NCCLog.Left + NCCLog.Width Else NCCLog.Left = NCCLog.Left - NCCLog.Width End If End Sub