Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Stars
- -- Emulation of the Windows 98 Starfield screensaver on a ComputerCraft Monitor.
- -- by Abdiel, 2013
- --
- -- Place a computer directly next to a monitor of any size and run this program.
- -- To terminate, hold ctrl+T for three seconds.
- --
- -- Arguments:
- -- stars [count] [speed] [color] [char]
- --
- -- count - Number of stars displayed. Default 25. You might want more on a large monitor.
- -- speed - Speed at which the stars move. Default one pixel per tick.
- -- color - Color of the stars. Can be any value from the colors table.
- -- (see http://computercraft.info/wiki/Colors_(API) )
- -- If you are not using an advanced monitor, the color is ignored.
- -- char - Character to draw stars with. Default "." (period).
- --
- -- Try this on at least a 3x3 monitor:
- -- stars 10 0.3 red |-o-|
- --
- --
- -- This work is free. You can redistribute it and/or modify it under the
- -- terms of the Do What The Fuck You Want To Public License, Version 2,
- -- as published by Sam Hocevar. See http://www.wtfpl.net/about/ for more details.
- local args = {...}
- -- Argument parsing
- -- Number of stars.
- local n = tonumber(args[1])
- if not n then n = 25 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