Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- -- Number of stars.
- local n = tonumber(args[1])
- if not n then n = 50 end
- -- Speed of stars (pixels/tick).
- local speed = tonumber(args[2])
- if not speed then speed = 1 end
- -- Color of stars. Can be any member of the colors table.
- local col = colors[args[3]]
- if not col then col = colors.white end
- -- Character to draw stars with.
- local stCh = args[4]
- if not stCh then stCh = "." end
- local clr = string.rep(" ", string.len(stCh))
- -- Find a monitor
- local m
- for i,s in ipairs(redstone.getSides()) do
- m = peripheral.wrap(s)
- if m then break end
- end
- if not m then
- print("To run this program, connect the computer to a monitor.")
- return false
- end
- -- Set color (only if we have an advanced monitor)
- if m.isColor() then
- m.setTextColor(col)
- m.setBackgroundColor(colors.black)
- end
- m.setTextScale(0.5)
- m.clear()
- -- width, height of the monitor
- local w,h = m.getSize()
- -- x and y coordinates of the monitor's center
- local cx, cy = w/2, h/2
- local stars = {}
- -- Generates k new stars in the middle of the screen.
- -- Position of a star in polar coordinates: r,phi.
- function genStars(k)
- for i = 1,k do
- table.insert(stars, {0, math.random() * 2 * math.pi})
- end
- end
- -- Returns the position of a star as x,y values.
- function getPos(star)
- local x = math.sin(star[2]) * star[1]
- local y = math.cos(star[2]) * star[1]
- return cx + x, cy + y
- end
- -- Main program
- genStars(n)
- while true do
- local toRemove = {}
- for i = 1,#stars do
- -- Remove star from display
- local x,y = getPos(stars[i])
- m.setCursorPos(x,y)
- m.write(clr)
- -- Calculate new position of the star.
- stars[i][1] = stars[i][1] + speed
- x,y = getPos(stars[i])
- if x<0 or x>w or y<0 or y>h then
- -- Remove star.
- table.insert(toRemove, i)
- else
- -- Draw star.
- m.setCursorPos(x,y)
- m.write(stCh)
- end
- end
- -- Remove stars which fell off the screen.
- for j = #toRemove, 1, -1 do
- table.remove(stars, toRemove[j])
- end
- -- Generate new stars.
- genStars(#toRemove)
- sleep(0.05)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement