# Retrogamecoding and Basicprogramming Forum

## Retrogamecoding(.org) => EGSL => Topic started by: GEEK on 11. February 2014, 19:39:06

Title: Star-field
Post by: GEEK on 11. February 2014, 19:39:06
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
Title: Re: Star-field
Post by: Cosmo on 12. February 2014, 11:31:27
Hi Geek,
Code: [Select]
`--'by Peter Wirbelaueropenwindow(640,480,0,"Stars")setframetimer(60)function rand(minZahl,maxZahl)   maxZahl = (maxZahl-minZahl)   return int(rnd()*maxZahl+minZahl)endlocal col=0;sx=0;sy=0;anz=5000;speed=2;xscreen=640;yscreen=480local 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)endfunction 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)   endendrepeatk=getkey()cls()alphachannel(200)updatestars()redraw()until k==27closewindow()`
Title: Re: Star-field
Post by: Cosmo on 12. February 2014, 12:16:16
another starfield.
Code: [Select]
`--'by Peter Wirbelaueropenwindow(640,480,0,"Retro-Stars")setframetimer(60)colorkey(0,0,0)function rand(minZahl,maxZahl)   maxZahl = (maxZahl-minZahl)   return int(rnd()*maxZahl+minZahl)endfunction sprite(id,x,y,w,h,fx)   drawimage(w*fx,0,w,h,x,y,id)endlocal np=500; j=0; z=0; p=0; v=0local 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)endrepeatk=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)endsprite(p,(640-128)/2,(480-128)/2,128,128,z)v = v+0.2if v >=1 then   v = 0   z = z+1   if z==10 then z=0; endendredraw()until k==27closewindow() `
Title: Re: Star-field
Post by: Cosmo on 12. February 2014, 16:56:01
And once again.
Code: [Select]
`--'by Peter Wirbelaueropenwindow(640,480,0,"Retro-Stars")setframetimer(60)function rand(minZahl,maxZahl)   maxZahl = (maxZahl-minZahl)   return int(rnd()*maxZahl+minZahl)endlocal rad = math.radlocal z = "HELLO AND WELCOME IN THE SUPER PROGRAMMING!"; letter=0local fok, xp, yp, i, tl, x, m = 160,0,0,0,0,0,0local 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] = 240endfor i=0,799 do    x3DSterne[i] = rand (1,440)    y3DSterne[i] = rand (1,380)    z3DSterne[i] = rand (1,500)endrepeatk=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    endendalphachannel(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)endredraw()until k==27closewindow()`
Title: Re: Star-field
Post by: GEEK on 12. February 2014, 20:30:25
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  ;)
Title: Re: Star-field
Post by: Cosmo on 12. February 2014, 20:36:50
Hi Geek,

stripes are possible with alphachannel, but unfortunately, Egsl alphachannel doesn't it.
Title: Re: Star-field
Post by: GEEK on 12. February 2014, 22:18:13
ok, thanks anyway!  :)
Title: Re: Star-field
Post by: GEEK on 12. February 2014, 22:38:47
Ow, the documentation says that egsl has alpha to...
Title: Re: Star-field
Post by: Cosmo on 13. February 2014, 00:00:51
Hi Geek,

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

Title: Re: Star-field
Post by: Cosmo on 13. February 2014, 00:05:09
If you would peek into the source code, you would see that I used alphachannel!
Title: Re: Star-field
Post by: GEEK on 13. February 2014, 00:13:03
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? :)
Title: Re: Star-field
Post by: Cosmo on 13. February 2014, 00:28:29
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.
Title: Re: Star-field
Post by: GEEK on 13. February 2014, 00:44:22
ok, going to mess a bit with that code, maybe i find a solution, thanks for the help!  ;)
Title: Re: Star-field
Post by: Cosmo on 15. February 2014, 22:49:35
Hi Lelldorin,

Okay, you got it  ;D
Title: Re: Star-field
Post by: GEEK on 16. February 2014, 02:54:47
Hi Lelldorin ???
Title: Re: Star-field
Post by: Bereb on 16. February 2014, 17:33:43
I've been trying that a few time ago :

Code: [Select]
`openwindow(300,300,32,"MÉTÉORITE")backcolour(0,0,50)colour(0,255,255)clearscreen()num = 60mx, my = {}, {}while getkey() ~= 27 do for i = 2, num do mx[i - 1] = mx[i] my[i - 1] = my[i] end -- mx[num] = mousex() my[num] = mousey() -- clearscreen() for i = 1, num do colour(4 * i, 2 * i, i) fillcircle(mx[i], my[i], i/4) end redraw()endclosewindow()`
Maybe it could help ... adding some alphachannel() at the right place.

Title: Re: Star-field
Post by: Bereb on 16. February 2014, 17:40:49
Here's a version with alphachannel(), to compare :

Code: [Select]
`openwindow(300,300,32,"MÉTÉORITE")backcolour(0,0,50)colour(0,255,255)clearscreen()num = 60mx, my = {}, {}while getkey() ~= 27 do for i = 2, num do mx[i - 1] = mx[i] my[i - 1] = my[i] end -- mx[num] = mousex() my[num] = mousey() -- clearscreen() for i = 1, num do                alphachannel(100-i) colour(4 * i, 2 * i, i) fillcircle(mx[i], my[i], i/4) end redraw()endclosewindow()`
Title: Re: Star-field
Post by: GEEK on 17. February 2014, 21:38:28
Thanks Bereb :)
I will try as soon as I got a bit more time again.
Title: Re: Star-field
Post by: GEEK on 19. February 2014, 20:33:54
hey! done some changes to the code to understand it a bit more, made it look cool with colors ;D
still thinking about that stripe effect, the example @Bereb gave me did't work because every star is redrawn every time so its a bit more complicated..
have a few questions (look at the comments of the code)
but I have coded sort of a solution, what do you think? :)
sometimes it glitches for a sec, a square flickering somewhere and then its gone.. is it something wrong in the code?

Code: [Select]
`openwindow(1600,900,0,"Stars") --set to screensaver sizetogglefullscreen()setframetimer(60) --why are you using this?function rand(minZahl,maxZahl)   maxZahl = (maxZahl-minZahl)   return int(rnd()*maxZahl+minZahl)endlocal sx=0;sy=0;anz=255;speed=0.1;xscreen=screenwidth();yscreen=screenheight() --why are they all local?local xstern ={}local ystern ={}local zstern ={}local colors ={}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)   local rndnum = int(rnd()*3)   if rndnum == 0 then       colors[i] = {255,0,0}   elseif rndnum == 1 then       colors[i] = {0,255,0}   else       colors[i] = {0,0,255}   endendfunction updatestars()   local i,x   for i=0,anz do      zstern[i] = zstern[i] - speed      if zstern[i] <= speed then         xstern[i] = rand(-(xscreen/2), (xscreen/2))*128         ystern[i] = rand(-(yscreen/2), (yscreen/2))*128         zstern[i] = 255      end      sx = (xstern[i] / zstern[i]) + (xscreen/2)      sy = (ystern[i] / zstern[i]) + (yscreen/2)      for x = 1,3 do         if colors[i][x] > 0 then            colors[i][x]=(255+1)-zstern[i] --optional!            break         end      end      color(colors[i],colors[i],colors[i])      local Rsize = int(255 - zstern[i]) / 100      fillbox(sx-Rsize,sy-Rsize,sx+Rsize,sy+Rsize)   endendrepeat  k=getkey()  cls()  for j = 1,50 do     updatestars()  end  redraw()until k==27closewindow()`
Title: Re: Star-field
Post by: Osgeld on 20. February 2014, 02:20:54
yea looks pretty good, only so many ways one can do a starfield, and mine is similar, if you want some more fun I added a little chunk out of my demo code I have been using for years that smoothly cycles though all the RGB color possibilities at a given brightness

Code: [Select]
`openwindow(800,600,0,"Stars") --set to screensaver size--togglefullscreen()setframetimer(60) --why are you using this?angle = 0 -- needed for rainbow color --obochannel = 0 -- needed for rainbow color --oboRAD = (math.pi / 180) -- needed for rainbow color -- obofunction rand(minZahl,maxZahl)   maxZahl = (maxZahl-minZahl)   return int(rnd()*maxZahl+minZahl)endlocal sx=0;sy=0;anz=255;speed=0.1;xscreen=screenwidth();yscreen=screenheight() --why are they all local?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-- RAINBOW COLOR -- OBOgetColour = function(angle, channel) local cos =  math.cos(angle * RAD) * 255   -- lower 255 for darker colors local sin =  math.sin(angle * RAD) * 255   -- lower 255 for darker colors if     channel == 0 then  colour(cos, sin, 0) elseif channel == 1 then  colour(0, cos, sin)    elseif channel == 2 then  colour(sin, 0, cos)    endendfunction updatestars()   local i,x   for i=0,anz do      zstern[i] = zstern[i] - speed      if zstern[i] <= speed then         xstern[i] = rand(-(xscreen/2), (xscreen/2))*128         ystern[i] = rand(-(yscreen/2), (yscreen/2))*128         zstern[i] = 255      end      sx = (xstern[i] / zstern[i]) + (xscreen/2)      sy = (ystern[i] / zstern[i]) + (yscreen/2)      local Rsize = int(255 - zstern[i]) / 100      fillbox(sx-Rsize,sy-Rsize,sx+Rsize,sy+Rsize)   endendrepeat  k=getkey()  cls()      -- color update --obo      if angle == 90 then angle = 0         if channel ~= 2 then channel = channel + 1        else channel = 0        end      end            getColour(angle, channel)       angle = angle + 1      -- end color update -- obo  for j = 1,50 do     updatestars()  end  redraw()until k==27closewindow()`
Title: Re: Star-field
Post by: GEEK on 20. February 2014, 19:45:00
wow, nice :D
I was looking for a good star-field screensaver for a while you know.
The funny thing is, now I can make my own :P and its free!
Nicely done Osgeld ;)
Title: Re: Star-field
Post by: GEEK on 21. February 2014, 21:30:04
Just noticed if you use setframetimer(60) you have to use sync() to wait time left...
Title: Re: Star-field
Post by: GEEK on 21. February 2014, 21:44:00
Thanks again for the color function Osgeld!
I used it for this to:

Code: [Select]
`screen(800,500,0,".scr")--togglefullscreen()--mousehide()setframetimer(60)w = screenwidth()h = screenheight()x_ = w/2y_ = h/2function random(min,max)   return int((max-min+1)*rnd()+min)endangle = 0 -- needed for rainbow color --obochannel = 0 -- needed for rainbow color --oboRAD = (math.pi / 180) -- needed for rainbow color -- obo-- RAINBOW COLOR -- OBOgetColour = function(angle, channel) local cos =  math.cos(angle * RAD) * 255   -- lower 255 for darker colors local sin =  math.sin(angle * RAD) * 255   -- lower 255 for darker colors if     channel == 0 then  colour(cos, sin, 0) elseif channel == 1 then  colour(0, cos, sin)    elseif channel == 2 then  colour(sin, 0, cos)    endend--------------------------------------------------------------------------------speed = 1amount = 10myRotation = {}xchange = {}ychange = {}x= {}y= {}for i = 1,amount do   x[i] = x_   y[i] = y_   xchange[i] = 0   ychange[i] = 0   myRotation[i] = int(rnd()*360)endfunction update()        local i        for i = 1,amount do myRotation[i] = myRotation[i] + int(rnd()*30-15) ychange[i] = (cos((math.pi/180)*myRotation[i]))*speed xchange[i] = (sin((math.pi/180)*myRotation[i]))*speed if x[i] < 0 then x[i] = w end if x[i] > w then x[i] = 0 end if y[i] < 0 then y[i] = h end if y[i] > h then y[i] = 0 end y[i] = y[i] - ychange[i] x[i] = x[i] + xchange[i]        circle(x[i],y[i],3)        endendrepeat k=getkey()--cls()      -- color update --obo      if angle == 90 then angle = 0        if channel ~= 2 then channel = channel + 1        else channel = 0        end      end      getColour(angle, channel)      angle = angle + 1      -- end color update -- oboupdate()redraw()until k==27 closewindow()`
Title: Re: Star-field
Post by: Osgeld on 21. February 2014, 23:52:38
I just copied my junk into your code :)
Title: Re: Star-field
Post by: GEEK on 22. February 2014, 17:19:28
@Cosmo, I have a question..
what happens with a star when it is outside the screen?
Title: Re: Star-field
Post by: GEEK on 24. February 2014, 00:39:45
FOUND A SOLUTION ;D

Code: [Select]
`openwindow(800,600,0,"Stars")setframetimer(60)function rand(minZahl,maxZahl)   maxZahl = (maxZahl-minZahl)   return int(rnd()*maxZahl+minZahl)endlocal col=0;sx=0;sy=0;anz=1000;speed=0.1;xscreen=800;yscreen=600local 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)endfunction 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      color(0,co,0)      fillbox(sx,sy,sx+2,sy+2)   endendrepeatk=getkey()--cls()alphachannel(5)color(0,0,0)fillbox(0,0,800,600)alphachannel(255)updatestars()sync()until k==27closewindow()`