Retrogamecoding(.org) > Examples

3D Stars

(1/3) > >>

Peter:
Hi,

Some funny stars.

--- Code: ---screen(640,480,0,"3D Stars")
backcolor(0,0,0)
setframetimer(60)

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

sx,sy,m,col,anz,speed,xscreen,yscreen = 0,0,0,0,5000,2,640,480
stern_x ={}
stern_y ={}
stern_z ={}

for m=1,anz do
   stern_x[m]=0
   stern_y[m]=0
   stern_z[m]=0
end

function starinit()
  for m=1,anz do
    stern_x[m]= Rand(-(xscreen/2),(xscreen/2)) *128
    stern_y[m]= Rand(-(yscreen/2),(yscreen/2)) *128
    stern_z[m]= Rand(speed,255)
  end
end

starinit()
repeat
k=getkey()
alphachannel(75)
cls()

for m=1,anz do
   stern_z[m] = stern_z[m] - speed
   if stern_z[m] <= speed then
      stern_z[m] = 255
   end
   sx = (stern_x[m] / stern_z[m]) + (xscreen /2)
   sy = (stern_y[m] / stern_z[m]) + (yscreen /2)
   color(255,255,255)
   fillcircle(sx,sy,2)
end

redraw()
until k==27
closewindow()
 
--- End code ---

Bereb:
Fine  :)
May I take the liberty of posting one of my scripts, adapted from an SDLBasic one and modified with EGSL features, which is very similar to yours:


--- Code: ---#!/usr/bin/egsl

openwindow(800,600,32,"3D Stars - EGSL")
alphachannel(160)
mousehide()

local random, abs = math.random, math.abs

numstars = 3000
x = {}
y = {}
s = {}

for i = 1, numstars do
   x[i] = random(800)-512
   y[i] = random(600)-368
   s[i] = random(4)+1
end

repeat
   key = getkey()
   clearscreen()
   for i = 1, numstars do
      x[i] = x[i]*1.02
      y[i] = y[i]*1.02
      if abs(x[i])>512 or abs(y[i])>368 then
         x[i] = random(512)-256
         y[i] = random(368)-184
      end
      colour(s[i]*32, s[i]*32, 255)
      fillcircle(512+x[i], 368+y[i],random(0.5,1))
   end
   redraw()
until key == 27

closewindow()

--- End code ---

You can delete or comment the 2nd 'local' line. That doesn't modify speed ...


*** I'v corrected the code *** (18-nov-2012)

Peter:
Hi Bereb,

You said sdlbasic. I am there member.
I think this is by me.

I got an error here:  D:\EGSL\berebstar.lua:23: attempt to perform arithmetic on local 'x' (a table value)

kevin:
Hi - I can get a similar thing in NaaLaa, but the EGSL version seems smoother to me - anyone there to show a better version for NaaLaa?

--- Code: ---rem ---------import required libraries----------
rem --------------------------------------------
 import "Speed.lib"
 constant:
rem we'll have 3000 stars
STARS 3000
 hidden:
rem set window size and position
 set window 0,0, 800,600
 set redraw off
visible:
rem initialise variables

rem arrays to hold x and y positions of each star, and a colour component for each
x#[STARS]
y#[STARS]
s[STARS]

rem set initial positions and colours for each star
for i = 0 to STARS - 1
x[i]=float(rnd(800) - 400)
y[i]=float(rnd(600) - 300)
s[i]=rnd(4) + 1
next


 shouldDraw = true
 do
rem prepare the drawing board
set color 0,0,0
 cls
 set color 255,255,255

rem move the stars one at a time
for i = 0 to STARS - 1
x[i]=x[i]*1.02
y[i]=y[i]*1.02
rem replace stars as they leave the window
if abs#(x[i])>400.0 or abs#(y[1])>300.0
x[i]=float(rnd(800)-400)
y[i]=float(rnd(600)-300)
endif
rem draw each star using its own colour
set color s[i]*32,s[i]*32,255
draw rect  int(x[i])+400,int(y[i])+300,3,3,true
next

redraw
shouldDraw = SPD_HoldFrameDraw(60)
 until keydown(27) or not running()




--- End code ---

Edit....I've noticed that NaaLaa stops responding after a minute or so of running this, so maybe it needs a more efficient way of doing this in NaaLaa...it may on the other hand be my laptop - an older Centrino Duo, or maybe a silly mistake in the code above...I'll keep looking.....

Cybermonkey:
Same here ... I tried to change the variable name to xc but then I got (even if I give xc a value of e.g. 1):

--- Quote ---/home/markus/Examples-EGSL/stars2.lua:25: attempt to perform arithmetic on local 'xc' (a nil value)
--- End quote ---

Navigation

[0] Message Index

[#] Next page

Go to full version