Starfield with trails

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
angros47
Posts: 1890
Joined: Jun 21, 2005 19:04

Starfield with trails

Postby angros47 » Jun 19, 2021 18:42

Very simple starfield effect, made using the old school trick of color cycling. Use PgUp and PgDown to set the speed.

Code: Select all

const w=800: const h=600

screenres w, h, 8


Dim pal(0 To 255) As Integer

dim as single x, y, c, sx, sy, sc
c=1.0

for i as integer=1 to 300
   x=w/2: y=h/2
   sx=(rnd()*2.0)-1.0
   sy=(rnd()*2.0)-1.0
   sc=rnd()
   do
      x+=sx: y+=sy: c+=sc
      if c>255.0 then c-=254.0
      pset (x,y), c
   loop until x<0 orelse x>w orelse y<0 orelse y>h

next

dim t as single
dim speed as single=.01

do
   palette using pal

   dim d as integer=&hFF0000-(speed *100)
   for i as integer=1 to 255
      if pal(i) then pal(i)=pal(i)/20*19 and &hFF0000
   next
   t= (t+speed)
   if t>=255.0 then t-=254.0
   pal (t+.5)=&hFFFFFF

   if multikey(&h49) andalso speed <1 then speed+=.001
   if multikey(&h51) andalso speed >0.001 then speed-=.001
   'screensync
   sleep 1

loop until multikey(1)

dafhi
Posts: 1400
Joined: Jun 04, 2005 9:51

Re: Starfield with trails

Postby dafhi » Jun 23, 2021 11:02

neat! had to tweak. also looks cool with the lines uncommented

Code: Select all

   var rad = .01 + 250 * rnd
   var ang = rnd * tau
   sx = cos(ang)
   sy = sin(ang)
   x=w/2 + rad * sx
   y=h/2 + rad * sy
   rad = rnd + .1
   sx *= rad
   sy *= rad
   'sx = (rnd()*2.0)-1.0
   'sy = (rnd()*2.0)-1.0
integer
Posts: 394
Joined: Feb 01, 2007 16:54
Location: usa

Re: Starfield with trails

Postby integer » Jun 25, 2021 3:35

dafhi wrote:neat! had to tweak. also looks cool with the lines uncommented

Code: Select all

   var rad = .01 + 250 * rnd
   var ang = rnd * tau
   sx = cos(ang)
   sy = sin(ang)
   x=w/2 + rad * sx
   y=h/2 + rad * sy
   rad = rnd + .1
   sx *= rad
   sy *= rad
   'sx = (rnd()*2.0)-1.0
   'sy = (rnd()*2.0)-1.0

Just curious.
What is your initial value for "tau"?
angros47
Posts: 1890
Joined: Jun 21, 2005 19:04

Re: Starfield with trails

Postby angros47 » Jun 25, 2021 10:18

tau is a math constant: it is equal to 2*pi, or 6.28318530718

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests