abdiel_kavash

CC stars screensaver

Feb 5th, 2013
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. -- Stars
  2. -- Emulation of the Windows 98 Starfield screensaver on a ComputerCraft Monitor.
  3. -- by Abdiel, 2013
  4. --
  5. -- Place a computer directly next to a monitor of any size and run this program.
  6. -- To terminate, hold ctrl+T for three seconds.
  7. --
  8. -- Arguments:
  9. --   stars [count] [speed] [color] [char]
  10. --
  11. -- count - Number of stars displayed. Default 25. You might want more on a large monitor.
  12. -- speed - Speed at which the stars move. Default one pixel per tick.
  13. -- color - Color of the stars. Can be any value from the colors table.
  14. --         (see http://computercraft.info/wiki/Colors_(API) )
  15. --         If you are not using an advanced monitor, the color is ignored.
  16. -- char  - Character to draw stars with. Default "." (period).
  17. --
  18. -- Try this on at least a 3x3 monitor:
  19. --   stars 10 0.3 red |-o-|
  20. --
  21. --
  22. -- This work is free. You can redistribute it and/or modify it under the
  23. -- terms of the Do What The Fuck You Want To Public License, Version 2,
  24. -- as published by Sam Hocevar. See http://www.wtfpl.net/about/ for more details.
  25.  
  26. local args = {...}
  27.  
  28. -- Argument parsing
  29. -- Number of stars.
  30. local n = tonumber(args[1])
  31. if not n then n = 25 end
  32.  
  33. -- Speed of stars (pixels/tick).
  34. local speed = tonumber(args[2])
  35. if not speed then speed = 1 end
  36.  
  37. -- Color of stars. Can be any member of the colors table.
  38. local col = colors[args[3]]
  39. if not col then col = colors.white end
  40.  
  41. -- Character to draw stars with.
  42. local stCh = args[4]
  43. if not stCh then stCh = "." end
  44. local clr = string.rep(" ", string.len(stCh))
  45.  
  46. -- Find a monitor
  47. local m
  48. for i,s in ipairs(redstone.getSides()) do
  49.   m = peripheral.wrap(s)
  50.   if m then break end
  51. end
  52.  
  53. if not m then
  54.   print("To run this program, connect the computer to a monitor.")
  55.   return false
  56. end
  57.  
  58. -- Set color (only if we have an advanced monitor)
  59. if m.isColor() then
  60.   m.setTextColor(col)
  61.   m.setBackgroundColor(colors.black)
  62. end
  63. m.setTextScale(0.5)
  64. m.clear()
  65.  
  66. -- width, height of the monitor
  67. local w,h = m.getSize()
  68.  
  69. -- x and y coordinates of the monitor's center
  70. local cx, cy = w/2, h/2
  71.  
  72. local stars = {}
  73.  
  74. -- Generates k new stars in the middle of the screen.
  75. -- Position of a star in polar coordinates: r,phi.
  76. function genStars(k)
  77.   for i = 1,k do
  78.     table.insert(stars, {0, math.random() * 2 * math.pi})
  79.   end
  80. end
  81.  
  82. -- Returns the position of a star as x,y values.
  83. function getPos(star)
  84.   local x = math.sin(star[2]) * star[1]
  85.   local y = math.cos(star[2]) * star[1]
  86.   return cx + x, cy + y
  87. end
  88.  
  89. -- Main program
  90. genStars(n)
  91.  
  92. while true do
  93.   local toRemove = {}
  94.  
  95.   for i = 1,#stars do
  96.     -- Remove star from display
  97.     local x,y = getPos(stars[i])
  98.     m.setCursorPos(x,y)
  99.     m.write(clr)
  100.    
  101.     -- Calculate new position of the star.
  102.     stars[i][1] = stars[i][1] + speed
  103.     x,y = getPos(stars[i])
  104.    
  105.     if x<0 or x>w or y<0 or y>h then
  106.       -- Remove star.
  107.       table.insert(toRemove, i)
  108.     else
  109.       -- Draw star.
  110.       m.setCursorPos(x,y)
  111.       m.write(stCh)
  112.     end
  113.   end
  114.  
  115.   -- Remove stars which fell off the screen.
  116.   for j = #toRemove, 1, -1 do
  117.     table.remove(stars, toRemove[j])
  118.   end
  119.  
  120.   -- Generate new stars.
  121.   genStars(#toRemove)
  122.  
  123.   sleep(0.05)
  124. end
Advertisement
Add Comment
Please, Sign In to add comment