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 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()
Title: Re: Star-field
Post by: Cosmo on 12. February 2014, 12:16:16
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()
Title: Re: Star-field
Post by: Cosmo on 12. February 2014, 16:56:01
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()
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 = 60
mx, 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()
end

closewindow()

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 = 60
mx, 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()
end

closewindow()
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 size
togglefullscreen()

setframetimer(60) --why are you using this?

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

local 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}
   end
end

function 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][1],colors[i][2],colors[i][3])

      local Rsize = int(255 - zstern[i]) / 100
      fillbox(sx-Rsize,sy-Rsize,sx+Rsize,sy+Rsize)
   end
end

repeat
  k=getkey()
  cls()
  for j = 1,50 do
     updatestars()
  end
  redraw()
until k==27

closewindow()
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 --obo
channel = 0 -- needed for rainbow color --obo
RAD = (math.pi / 180) -- needed for rainbow color -- obo

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

local 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 -- OBO
getColour = 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)
    end
end

function 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)
   end
end

repeat
  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==27

closewindow()
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/2
y_ = h/2

function random(min,max)
   return int((max-min+1)*rnd()+min)
end

angle = 0 -- needed for rainbow color --obo
channel = 0 -- needed for rainbow color --obo
RAD = (math.pi / 180) -- needed for rainbow color -- obo

-- RAINBOW COLOR -- OBO
getColour = 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)
    end
end

--------------------------------------------------------------------------------

speed = 1
amount = 10

myRotation = {}
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)
end

function 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)
        end
end

repeat 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
update()

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)
end

local col=0;sx=0;sy=0;anz=1000;speed=0.1;xscreen=800;yscreen=600
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
      color(0,co,0)
      fillbox(sx,sy,sx+2,sy+2)
   end
end

repeat
k=getkey()
--cls()
alphachannel(5)
color(0,0,0)
fillbox(0,0,800,600)
alphachannel(255)
updatestars()
sync()
until k==27
closewindow()