Important alert: (current site time 7/16/2013 4:14:51 AM EDT)
 

VB icon

Draw a moving starfield on a form

Email
Submitted on: 5/28/1998
By: Theo Kandiliotis  
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: 35295
 
     It draws a moving starfield on a form with simple VB graphics methods and a timer control.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' 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
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=906&lngWId=1'for details.'**************************************

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


Other 3 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

5/11/1999 6:47:00 PMDan

The background has to be white because the stars are black.
(If this comment was disrespectful, please report it.)

 
5/13/1999 11:29:00 AMMN Karthik

Nice piece of graphics. Hope you create more such graphics to help beginners like us.
(If this comment was disrespectful, please report it.)

 
5/22/1999 9:38:00 AMtommy

is there any way to get the stars to be white, and the back color black, looking more like a space shot?
I'm reffering to the: Draw a moving starfield on a form
Submitted on: 5/28/98
By: Theo Kandiliotis

thanks
(If this comment was disrespectful, please report it.)

 
5/25/1999 11:42:00 AMKeir

For that person that wanted to make white on black, simply change "backcolor" in the timer statement to the code for white, and set your form background to black. Also i think this code needs a "CLS" at the beginning of the timer code. at least it does if you are doing white on black
(If this comment was disrespectful, please report it.)

 
6/19/1999 1:19:00 AMMr.Man

There's an easier way to make the stars white and the back black... You just make the Frm's ForeColor = white!
(If this comment was disrespectful, please report it.)

 
7/24/1999 1:20:00 PMTheo Floyd

This is really cool, it works well
thanks!!
(If this comment was disrespectful, please report it.)

 
8/4/1999 9:10:00 AMSteve

i can't get this to work, can someone send me the exe file of it so i know what it should look like?
(If this comment was disrespectful, please report it.)

 
8/18/1999 7:27:00 AMMichel Rutten

Stars dropping down? I hope I never have to witness that in real life... :-)

(If this comment was disrespectful, please report it.)

 
9/3/1999 2:24:00 PMHammerHead

Pretty cool application. Keir is right, though, you need a CLS at the beginning of the timer to clear the screen between each run. Otherwise it creates a pretty frightening (and sickening) effect.

I wonder if I could do something similar to the Matrix with this...
(If this comment was disrespectful, please report it.)

 
9/24/1999 6:50:00 PMtenya

I would like to know how to create grid coordinate in VB5, then when the user click on it the point will appear with the point coordinate(x,y)........I hope if there is anyone know about it can help me...pleaseeeeeeeeee...thanks a lot
(If this comment was disrespectful, please report it.)

 
11/26/1999 8:07:00 PMOwen

Hey this is ausome but I was just
wondering if there was a way to
stop the "stars" from eating away
pictures and other objects. THANZ!
(If this comment was disrespectful, please report it.)

 
11/26/1999 8:08:00 PMOwen

Hey this is awsome but I was just
wondering if there was a way to
stop the "stars" from eating away
pictures and other objects, THANZ!
(If this comment was disrespectful, please report it.)

 
11/26/1999 8:09:00 PMOwen

oops!
(If this comment was disrespectful, please report it.)

 
2/21/2000 10:39:13 PMBilly Bob

Owen, set the form's autoredraw to true. That will stop the stars from eating away at the form's graphics.
(If this comment was disrespectful, please report it.)

 
8/16/2000 8:31:24 AMToby Gunston

Very cool for such a small amount of code!
Nice work
(If this comment was disrespectful, please report it.)

 
9/26/2000 7:22:41 PMJimmy

Do you know that you can change that setting, that appeared as bubbles? haha, I figured it out, it is cool.
(If this comment was disrespectful, please report it.)

 
7/1/2004 4:31:53 PMHallsoft

I cannot believe this (piece of) code is a winner... :P
(If this comment was disrespectful, please report it.)

 

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.