Author Topic: Star-field  (Read 10871 times)

Bereb

  • Guest
Re: Star-field
« Reply #15 on: February 16, 2014, 04:33:43 PM »
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.

   
« Last Edit: February 16, 2014, 04:41:56 PM by Bereb »

Bereb

  • Guest
Re: Star-field
« Reply #16 on: February 16, 2014, 04:40:49 PM »
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()

GEEK

  • Guest
Re: Star-field
« Reply #17 on: February 17, 2014, 08:38:28 PM »
Thanks Bereb :)
I will try as soon as I got a bit more time again.

GEEK

  • Guest
Re: Star-field
« Reply #18 on: February 19, 2014, 07:33:54 PM »
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()
« Last Edit: February 21, 2014, 01:57:13 AM by GEEK »

Osgeld

  • Guest
Re: Star-field
« Reply #19 on: February 20, 2014, 01:20:54 AM »
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()

GEEK

  • Guest
Re: Star-field
« Reply #20 on: February 20, 2014, 06:45:00 PM »
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 ;)

GEEK

  • Guest
Re: Star-field
« Reply #21 on: February 21, 2014, 08:30:04 PM »
Just noticed if you use setframetimer(60) you have to use sync() to wait time left...

GEEK

  • Guest
Re: Star-field
« Reply #22 on: February 21, 2014, 08:44:00 PM »
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()

Osgeld

  • Guest
Re: Star-field
« Reply #23 on: February 21, 2014, 10:52:38 PM »
I just copied my junk into your code :)

GEEK

  • Guest
Re: Star-field
« Reply #24 on: February 22, 2014, 04:19:28 PM »
@Cosmo, I have a question..
what happens with a star when it is outside the screen?

GEEK

  • Guest
Re: Star-field
« Reply #25 on: February 23, 2014, 11:39:45 PM »
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()