CW = 420 CH = 330 myUniverse = {} sinus = {} cosinus = {} littleStarsMaximumDistance = 300 function initUniverse(howManyStars, minSpeed, maxSpeed) for i=1,howManyStars do local newStar = { position = { X=0, Y=0 }, angle = math.random(0,359), speed = math.random()*(maxSpeed-minSpeed)+minSpeed, -- BBGGRR color = ((math.random() > 0.66) and 0xffff00 or ((math.random() > 0.66) and 0xffa000 or 0xa0a000)), } myUniverse[i] = newStar end -- caluculate sinus and cosinus for all angles (0-359). So we don't have slowdowns later (in animate function). for i=0,359 do sinus[i] = math.sin(i / 180 * 3.141592) cosinus[i] = math.cos(i / 180 * 3.141592) end end function animate(timer) -- clean scene canvas_fillRect(canvas, 0,0,CW,CH) -- move stars for i=1,#myUniverse do -- get star attributes local currentPos = myUniverse[i].position local angle = myUniverse[i].angle local speed = myUniverse[i].speed -- move star currentPos.X = currentPos.X + cosinus[angle] * speed currentPos.Y = currentPos.Y + sinus[angle] * speed -- is star outside window? If yes, re-use it. if math.abs(currentPos.X) > CW/2 or math.abs(currentPos.Y) > CH/2 then currentPos.X = math.random(0,10)-5 -- move star to the center. We can move to (0,0). currentPos.Y = math.random(0,10)-5 -- But, I think using random is better (x=-5,5 y=-5,5) myUniverse[i].angle = math.random(0,359) -- change angle end -- draw your stuff local distance = math.sqrt(currentPos.X*currentPos.X+currentPos.Y*currentPos.Y) if distance > littleStarsMaximumDistance then pen_setColor(pen, myUniverse[i].color) canvas_ellipse(canvas, currentPos.X+CW/2-1, currentPos.Y+CH/2-1, -- draw bigger star currentPos.X+CW/2+1, currentPos.Y+CH/2+1 ) else canvas_setPixel(canvas, currentPos.X+CW/2 ,currentPos.Y+CH/2 , myUniverse[i].color) -- draw normal star end myUniverse[i].position = currentPos -- store star position end end if animateTimer~=nil then object_destroy(animateTimer); animateTimer = nil; end animateTimer = createTimer(nil,false) timer_setInterval(animateTimer,20) -- 20 milliseconds timer_onTimer(animateTimer,animate) initUniverse(400,1,5) -- 400 stars, speed: minimum=1, maximum=5 timer_setEnabled(animateTimer,true) -- start animation