Author Topic: Star-field  (Read 8249 times)

GEEK

  • Guest
Star-field
« on: February 11, 2014, 06:39:06 pm »
Hello!  :)
I'm looking for some code to make this: (picture)
I want to make a starfield screensaver in lua, but i'm not a genius in math :\
does someone want to help me?
Thanks in advanse
« Last Edit: February 11, 2014, 06:41:04 pm by GEEK »

Cosmo

  • Guest
Re: Star-field
« Reply #1 on: February 12, 2014, 10:31:27 am »
Hi Geek,
Code: [Select]
--'by Peter Wirbelauer
openwindow(640,480,0,"Stars")
setframetimer(60)

function rand(minZahl,maxZahl)
   maxZahl = (maxZahl-minZahl)
   return int(rnd()*maxZahl+minZahl)
end

local col=0;sx=0;sy=0;anz=5000;speed=2;xscreen=640;yscreen=480
local xstern ={}
local ystern ={}
local zstern ={}

for i=0,anz do
   xstern[i] = rand(-(xscreen/2), (xscreen/2))*128
   ystern[i] = rand(-(yscreen/2), (yscreen/2))*128
   zstern[i] = rand(speed,255)
end

function updatestars()
   local i,co
   for i=0,anz do
      zstern[i] = zstern[i] - speed
      if zstern[i] <= speed then zstern[i] = 255; end
      sx = (xstern[i] / zstern[i]) + (xscreen/2)
      sy = (ystern[i] / zstern[i]) + (yscreen/2)
      co = (300 -zstern[i])*.8
      alphachannel(250)
      color(co,co,co)
      box(sx,sy,sx+2,sy+2)
   end
end

repeat
k=getkey()
cls()
alphachannel(200)
updatestars()
redraw()
until k==27
closewindow()

Cosmo

  • Guest
Re: Star-field
« Reply #2 on: February 12, 2014, 11:16:16 am »
another starfield.
Code: [Select]
--'by Peter Wirbelauer
openwindow(640,480,0,"Retro-Stars")
setframetimer(60)
colorkey(0,0,0)

function rand(minZahl,maxZahl)
   maxZahl = (maxZahl-minZahl)
   return int(rnd()*maxZahl+minZahl)
end

function sprite(id,x,y,w,h,fx)
   drawimage(w*fx,0,w,h,x,y,id)
end

local np=500; j=0; z=0; p=0; v=0
local px={}
local py={}
local pz={}

p = loadimage("png/phoenix.png")

for j=0,np do
    px[j] = rand(0,639)
    py[j] = rand(0,479)
    pz[j] = rand(65536,16777215)
end

repeat
k=getkey()
cls()
for j=0,np do
     px[j] = px[j] -1
     if px[j] <0 then
        px[j] = 640
        py[j] = rand(0,479)
     end
     color(pz[j],pz[j],pz[j])
     box(px[j],py[j], px[j]+2,py[j]+2)
end

sprite(p,(640-128)/2,(480-128)/2,128,128,z)

v = v+0.2
if v >=1 then
   v = 0
   z = z+1
   if z==10 then z=0; end
end

redraw()
until k==27
closewindow()
« Last Edit: February 12, 2014, 11:20:05 am by Cosmo »

Cosmo

  • Guest
Re: Star-field
« Reply #3 on: February 12, 2014, 03:56:01 pm »
And once again.
Code: [Select]
--'by Peter Wirbelauer
openwindow(640,480,0,"Retro-Stars")
setframetimer(60)

function rand(minZahl,maxZahl)
   maxZahl = (maxZahl-minZahl)
   return int(rnd()*maxZahl+minZahl)
end

local rad = math.rad

local z = "HELLO AND WELCOME IN THE SUPER PROGRAMMING!"; letter=0
local fok, xp, yp, i, tl, x, m = 160,0,0,0,0,0,0
local x3DSterne ={}
local y3DSterne ={}
local z3DSterne ={}
local y ={}
local angle ={}
tl = len(z)

m=loadmusic("music/b.mod")
playmusic(m,128,-1)

for i=0,tl do
    angle[i] = 360-i*(360/tl)
    y[i] = 240
end

for i=0,799 do
    x3DSterne[i] = rand (1,440)
    y3DSterne[i] = rand (1,380)
    z3DSterne[i] = rand (1,500)
end

repeat
k=getkey()
alphachannel(150)
cls()
for i=0,799 do
    xp = (x3DSterne[i] * fok) / (z3DSterne[i] + fok) + 5
    yp = (y3DSterne[i] * fok) / (z3DSterne[i] + fok) + 5
    color(255,255,255)
    box(xp,yp, 2+xp,2+yp)

    xp = (-x3DSterne[i] * fok) / (z3DSterne[i] + fok) + 630
    yp = (-y3DSterne[i] * fok) / (z3DSterne[i] + fok) + 470
    box(xp,yp,2+xp,2+yp)

    xp = (-x3DSterne[i] * fok) / (z3DSterne[i] + fok) + 630
    yp = ( y3DSterne[i] * fok) / (z3DSterne[i] + fok) + 8
    box(xp,yp,2+xp,2+yp)

    xp = ( x3DSterne[i] * fok) / (z3DSterne[i] + fok) + 5
    yp = (-y3DSterne[i] * fok) / (z3DSterne[i] + fok) + 470
    box(xp,yp,2+xp,2+yp)

    z3DSterne[i] = z3DSterne[i] -1
    if z3DSterne[i] <=0 then
       z3DSterne[i] = 500
    end
end
alphachannel(255)

for x=1,tl do
   angle[x] = angle[x] +1
   if angle[x] == 360 then angle[x] =0; end
   letter = mid(z, x ,1)
   y[x] = y[x] + sin(rad(angle[x]*4))
   color(0x9C,0x5A,0xEF)
   drawtext(2+x*14,y[x],letter)
   y[x] = y[x] + cos(rad(angle[x]*2))
   color(0,255,0)
   drawtext(2+x*14,5+y[x],letter)
end

redraw()
until k==27
closewindow()
« Last Edit: February 12, 2014, 04:13:50 pm by Cosmo »

GEEK

  • Guest
Re: Star-field
« Reply #4 on: February 12, 2014, 07:30:25 pm »
wow!  :o that's sooo coool!  8)
thank you!

do you think you can give it the stripes effect?
that the stars like in the example are stripes?

I'm glad with this examples to! great  ;)

Cosmo

  • Guest
Re: Star-field
« Reply #5 on: February 12, 2014, 07:36:50 pm »
Hi Geek,

stripes are possible with alphachannel, but unfortunately, Egsl alphachannel doesn't it.
« Last Edit: February 12, 2014, 11:08:08 pm by Cosmo »

GEEK

  • Guest
Re: Star-field
« Reply #6 on: February 12, 2014, 09:18:13 pm »
ok, thanks anyway!  :)

GEEK

  • Guest
Re: Star-field
« Reply #7 on: February 12, 2014, 09:38:47 pm »
Ow, the documentation says that egsl has alpha to...

Cosmo

  • Guest
Re: Star-field
« Reply #8 on: February 12, 2014, 11:00:51 pm »
Hi Geek,

Egsl has alpha, I know.
But what I meant was, that it doesn't work!  You cannot get stripes with EGSL_ALPHACHANNEL!


Cosmo

  • Guest
Re: Star-field
« Reply #9 on: February 12, 2014, 11:05:09 pm »
If you would peek into the source code, you would see that I used alphachannel!

GEEK

  • Guest
Re: Star-field
« Reply #10 on: February 12, 2014, 11:13:03 pm »
Yes, just saw that, sry.
can i ask you why you used "alphachannel(200)" then?
if i delete it, nothing changes...

repeat
k=getkey()
cls()
alphachannel(200)
updatestars()
redraw()
until k==27
closewindow()

and is it possible witout alphachannel? just stripes, pure white? :)
« Last Edit: February 12, 2014, 11:15:51 pm by GEEK »

Cosmo

  • Guest
Re: Star-field
« Reply #11 on: February 12, 2014, 11:28:29 pm »
Quote
and is it possible witout alphachannel? just stripes, pure white?

I have tried, but without success. alphachannel in the source code is a holdover, I forgot to remove.
Alphachannel (50)  gets a darker white, but not alpha.   

GEEK

  • Guest
Re: Star-field
« Reply #12 on: February 12, 2014, 11:44:22 pm »
ok, going to mess a bit with that code, maybe i find a solution, thanks for the help!  ;)

Cosmo

  • Guest
Re: Star-field
« Reply #13 on: February 15, 2014, 09:49:35 pm »
Hi Lelldorin,

Okay, you got it  ;D

GEEK

  • Guest
Re: Star-field
« Reply #14 on: February 16, 2014, 01:54:47 am »
Hi Lelldorin ???