VB icon

Draw a moving starfield on a form

Submitted on: 1/1/2015 3:06:00 PM
By: Theo Kandiliotis (from psc cd)  
Level: Not Given
User Rating: By 101 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 1671
     It draws a moving starfield on a form with simple VB graphics methods and a timer control.
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: Draw a moving starfield on a form
' Description:It draws a moving starfield on a form with simple VB graphics methods and a timer control.
' By: Theo Kandiliotis (from psc cd)

How to draw a moving starfield
This example shows how to design a moving star field ,the standard animated background used in most space shoot'em up games.You know,the one that asteroids of all kinds of sizes zip by with various speeds,creating a 3D effect.Here we go: 
1.Create a Timer control. 2.Make these settings through the Properties Window:
Form1.WindowStart = 2
Form1.Backcolor = &H00000000& (that's black)
Timer1.Interval = 1
3.The algorythm is kinda complicated to explain in spoken words,so I'll leave it up to you to figer out what's going on. 
Dim X(50), Y(50), pace(50), size(50) As Integer
Private Sub Form_Activate()
For I = 1 To 50
x1 = Int(Form1.Width * Rnd)
y1 = Int(Form1.Height * Rnd)
pace1 = Int(500 - (Int(Rnd * 499)))
size1 = 16 * Rnd
X(I) = x1
Y(I) = y1
pace(I) = pace1
size(I) = size1
End Sub
Private Sub Timer1_Timer()
For I = 1 To 50
Circle (X(I), Y(I)), size(I), BackColor
Y(I) = Y(I) + pace(I)
If Y(I) >= Form1.Height Then Y(I) = 0: X(I) = Int(Form1.Width * Rnd)
Circle (X(I), Y(I)), size(I)
End Sub

Other 4 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.