-
Hello!
I am new to VB. I have been experimenting with it and would like to learn even more. I have been trying to figure out how to get pictures to move randomly across the screen. Can any one help? I would like to do 2 or more pictures but one would be enough to start.
Thanks in advance.
-
Try this and see if it is what you want. Make a Form with an ImageBox and a Timer. Set the Interval for the Timer to 1. Also, make sure there is a piucture loaded into the ImageBox.
Put this code in the Timer.
Code:
Private Sub Timer1_Timer()
Dim Direction As Byte
Dim Length As Integer
Randomize
Direction = Int(Rnd * 4) + 1
Length = Int(Rnd * 500) + 1
Select Case Direction
Case 1
Image1.Left = Image1.Left + Length
Case 2
Image1.Left = Image1.Left - Length
Case 3
Image1.Top = Image1.Top + Length
Case 4
Image1.Top = Image1.Top - Length
End Select
End Sub
-
Here's some cooler effects, random controlled direction
Use a image control image1, modify it for your needs ;)
Code:
Option Explicit
Private Sub Form_Activate()
Dim x As Single, y As Single, velocity As Single, angle As Single, vangle As Single
velocity = 2
angle = Rnd * 6.283
x = image1.Left: y = image1.Top
Do
If Rnd < 0.1 Then vangle = (Rnd - 0.5) * 0.5 'Change direction velocity randomly 10%chance
angle = angle + (vangle + 6.283) Mod 6.283 'change direction
x = x + Cos(angle) * velocity 'move position
y = y + Sin(angle) * velocity
image1.Move x, y 'move image
DoEvents 'threading
Loop While Forms.Count or yourchoise
End Sub