Advertisement
Guest User

stars

a guest
Sep 1st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. local args = {...}
  2. -- Number of stars.
  3. local n = tonumber(args[1])
  4. if not n then n = 50 end
  5.  
  6. -- Speed of stars (pixels/tick).
  7. local speed = tonumber(args[2])
  8. if not speed then speed = 1 end
  9.  
  10. -- Color of stars. Can be any member of the colors table.
  11. local col = colors[args[3]]
  12. if not col then col = colors.white end
  13.  
  14. -- Character to draw stars with.
  15. local stCh = args[4]
  16. if not stCh then stCh = "." end
  17. local clr = string.rep(" ", string.len(stCh))
  18.  
  19. -- Find a monitor
  20. local m
  21. for i,s in ipairs(redstone.getSides()) do
  22.   m = peripheral.wrap(s)
  23.   if m then break end
  24. end
  25.  
  26. if not m then
  27.   print("To run this program, connect the computer to a monitor.")
  28.   return false
  29. end
  30.  
  31. -- Set color (only if we have an advanced monitor)
  32. if m.isColor() then
  33.   m.setTextColor(col)
  34.   m.setBackgroundColor(colors.black)
  35. end
  36. m.setTextScale(0.5)
  37. m.clear()
  38.  
  39. -- width, height of the monitor
  40. local w,h = m.getSize()
  41.  
  42. -- x and y coordinates of the monitor's center
  43. local cx, cy = w/2, h/2
  44.  
  45. local stars = {}
  46.  
  47. -- Generates k new stars in the middle of the screen.
  48. -- Position of a star in polar coordinates: r,phi.
  49. function genStars(k)
  50.   for i = 1,k do
  51.     table.insert(stars, {0, math.random() * 2 * math.pi})
  52.   end
  53. end
  54.  
  55. -- Returns the position of a star as x,y values.
  56. function getPos(star)
  57.   local x = math.sin(star[2]) * star[1]
  58.   local y = math.cos(star[2]) * star[1]
  59.   return cx + x, cy + y
  60. end
  61.  
  62. -- Main program
  63. genStars(n)
  64.  
  65. while true do
  66.   local toRemove = {}
  67.  
  68.   for i = 1,#stars do
  69.     -- Remove star from display
  70.     local x,y = getPos(stars[i])
  71.     m.setCursorPos(x,y)
  72.     m.write(clr)
  73.    
  74.     -- Calculate new position of the star.
  75.     stars[i][1] = stars[i][1] + speed
  76.     x,y = getPos(stars[i])
  77.    
  78.     if x<0 or x>w or y<0 or y>h then
  79.       -- Remove star.
  80.       table.insert(toRemove, i)
  81.     else
  82.       -- Draw star.
  83.       m.setCursorPos(x,y)
  84.       m.write(stCh)
  85.     end
  86.   end
  87.  
  88.   -- Remove stars which fell off the screen.
  89.   for j = #toRemove, 1, -1 do
  90.     table.remove(stars, toRemove[j])
  91.   end
  92.  
  93.   -- Generate new stars.
  94.   genStars(#toRemove)
  95.  
  96.   sleep(0.05)
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement