Author Topic: Star-field  (Read 2569 times)

Bereb

  • Newcomer
  • *
  • Posts: 47
    • View Profile
Re: Star-field
« Reply #15 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.

   
« Last Edit: 16. February 2014, 17:41:56 by Bereb »
Regards, Bertrand :)
** Lubuntu 14.04 (Intel Celeron CPU 2.66GHz - 1280MiB) **

Bereb

  • Newcomer
  • *
  • Posts: 47
    • View Profile
Re: Star-field
« Reply #16 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()
Regards, Bertrand :)
** Lubuntu 14.04 (Intel Celeron CPU 2.66GHz - 1280MiB) **

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #17 on: 17. February 2014, 21:38:28 »
Thanks Bereb :)
I will try as soon as I got a bit more time again.

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #18 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()
« Last Edit: 21. February 2014, 02:57:13 by GEEK »

Osgeld

  • Newcomer
  • *
  • Posts: 33
    • View Profile
Re: Star-field
« Reply #19 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()

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #20 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 ;)

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #21 on: 21. February 2014, 21:30:04 »
Just noticed if you use setframetimer(60) you have to use sync() to wait time left...

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #22 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()

Osgeld

  • Newcomer
  • *
  • Posts: 33
    • View Profile
Re: Star-field
« Reply #23 on: 21. February 2014, 23:52:38 »
I just copied my junk into your code :)

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #24 on: 22. February 2014, 17:19:28 »
@Cosmo, I have a question..
what happens with a star when it is outside the screen?

GEEK

  • Jr. Member
  • **
  • Posts: 83
  • want to learn c++, if you want to teach me, pm me!
    • View Profile
Re: Star-field
« Reply #25 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()