Retrogamecoding(.org) > Examples
3D Stars
Peter:
Hi cyber,
The error was the double x!
This Bereb source code runs now.
--- Code: ---openwindow(800,600,32,"3D Stars - EGSL")
alphachannel(120)
mousehide()
local random, abs = math.random, math.abs
local numstars,key,i,s = 3000,0,0,0
x = {}
y = {}
for i=1, numstars do
x[i] = random(800)-512
y[i] = random(600)-368
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*32, s*32, 255)
color(255,255,255)
fillcircle(512+x[i],368+y[i],random(.5,1))
end
redraw()
until key == 27
closewindow()
--- End code ---
kevin:
It seems the issue of NaLaa crashing with my earlier example can be fixed using a 2 dimensional array instead:
--- Code: ---rem ---------import required libraries----------
rem --------------------------------------------
import "Speed.lib"
constant:
rem we'll have 3000 stars
STARS 3000
X 0
Y 1
S 2
rem STAR_IMG 0
hidden:
rem set window size and position
set window 0,0, 800,600
set redraw off
visible:
rem initialise variables
rem array to hold x and y positions of each star, and a colour component for each
starfield#[STARS][3]
rem set initial positions and colours for each star
for i = 0 to STARS - 1
starfield[i][X]=float(rnd(800) - 400)
starfield[i][Y]=float(rnd(600) - 300)
starfield[i][S]=float(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
starfield[i][X]=starfield[i][X]*1.02
starfield[i][Y]=starfield[i][Y]*1.02
rem replace stars as they leave the window
if abs#(starfield[i][X])>400.0 or abs#(starfield[i][Y])>300.0
starfield[i][X]=float(rnd(800)-400)
starfield[i][Y]=float(rnd(600)-300)
endif
rem draw each star using its own colour
set color int(starfield[i][S])*32,int(starfield[i][S])*32,255
draw rect int(starfield[i][X])+400,int(starfield[i][Y])+300,4,4
next
redraw
shouldDraw = SPD_HoldFrameDraw(60)
until keydown(27) or not running()
--- End code ---
I think I still prefer the look of the ESGL version though....maybe because of its use of circles instead of my rectangles?
kevin:
One final change, then I'll be moving on to other things....this makes the size of the "stars" vary for a more interesting result....
--- Code: ---rem ---------import required libraries----------
rem --------------------------------------------
import "Speed.lib"
constant:
rem we'll have 3000 stars
STARS 3000
X 0
Y 1
S 2
SIZE 3
hidden:
rem set window size and position
set window 0,0, 800,600
set redraw off
visible:
rem initialise variables
rem array to hold x and y positions of each star, and a colour component for each
starfield#[STARS][4]
rem set initial positions and colours for each star
for i = 0 to STARS - 1
starfield[i][X]=float(rnd(800) - 400)
starfield[i][Y]=float(rnd(600) - 300)
starfield[i][S]=float(rnd(4) + 1)
starfield[i][SIZE]=float(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
starfield[i][X]=starfield[i][X]*1.02
starfield[i][Y]=starfield[i][Y]*1.02
rem replace stars as they leave the window
if abs#(starfield[i][X])>400.0 or abs#(starfield[i][Y])>300.0
starfield[i][X]=float(rnd(800)-400)
starfield[i][Y]=float(rnd(600)-300)
endif
rem draw each star using its own colour
set color int(starfield[i][S])*32,int(starfield[i][S])*32,255
draw rect int(starfield[i][X])+400,int(starfield[i][Y])+300,int(starfield[i][SIZE]),int(starfield[i][SIZE])
next
redraw
shouldDraw = SPD_HoldFrameDraw(60)
until keydown(27) or not running()
--- End code ---
Bereb:
--- Quote from: Peter on November 17, 2012, 05:26:18 PM ---I got an error here: D:\EGSL\berebstar.lua:23: attempt to perform arithmetic on local 'x' (a table value)
--- End quote ---
Really sorry
:-[
I don't know why in:
--- Code: ---if abs(x[i])>512 or abs(y[i])>368 then
x[i] = random(512)-256
y[i] = random(368)-184
end
--- End code ---
x[] and y[] became x and y when I posted my code in this forum ? (maybe because of the BBcode ?)
???
--- Quote from: Peter on November 17, 2012, 05:26:18 PM ---Hi Bereb,
You said sdlbasic. I am there member.
I think this is by me.
--- End quote ---
Fine. So thanks for that script which inspired me :D
In a way we have been working as a team ;)
P.S.: I confirm. The "bug" was due to BBcode: "i" between square brackets is used to mean "in italics". All the code was wrong. Now it's correct
Peter:
Hi Bereb,
Just I saw that you are also SdlBasic member.
Is a little boring there, at the moment and I think in the future too.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version