VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Draw a moving starfield on a form

by Theo Kandiliotis (5 Submissions)
Category: Games
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (101 Votes)

It draws a moving starfield on a form with simple VB graphics methods and a timer control.

API Declarations


Rate Draw a moving starfield on a form

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()
Randomize
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
Next
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)
Next
End Sub

Download this snippet    Add to My Saved Code

Draw a moving starfield on a form Comments

No comments have been posted about Draw a moving starfield on a form. Why not be the first to post a comment about Draw a moving starfield on a form.

Post your comment

Subject:
Message:
0/1000 characters