Kingdaro

matrix

Mar 9th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. local chars = {}
  2.  
  3. local function new(t)
  4.     t.y = 0
  5.     table.insert(chars, t)
  6. end
  7.  
  8. setmetatable(chars, {__call = function(t)
  9.     local i = 0
  10.     return function()
  11.         i = i + 1
  12.         return t[i], i
  13.     end
  14. end})
  15.  
  16. local function timer(period)
  17.     return {
  18.         id = nil;
  19.         run = function(self)
  20.             self.id = os.startTimer(period)
  21.         end;
  22.     }
  23. end
  24.  
  25. local updateTimer = timer(0.1)
  26. local creator = timer(0.1)
  27.  
  28. updateTimer:run()
  29. creator:run()
  30.  
  31. term.setBackgroundColor(colors.black)
  32. term.clear()
  33. term.setCursorPos(1,1)
  34.  
  35. while true do
  36.     local _, t = os.pullEvent('timer')
  37.     local w,h = term.getSize()
  38.    
  39.     if t == updateTimer.id then
  40.         for char, i in chars() do
  41.             char.y = char.y + 1
  42.            
  43.             if char.y > h then
  44.                 table.remove(chars, i)
  45.             else
  46.                 term.setCursorPos(char.x, char.y)
  47.                 if char.color == colors.black then
  48.                     term.setBackgroundColor(colors.black)
  49.                     term.write(' ')
  50.                 else
  51.                     term.setTextColor(char.color)
  52.                     term.write(string.char(math.random(38, 126)))
  53.                 end
  54.             end
  55.         end
  56.        
  57.         updateTimer:run()
  58.     elseif t == creator.id then
  59.        
  60.         local time = math.random()*0.8 + 0.5
  61.         local tail = timer(time)
  62.         local eraser = timer(time + math.random()*0.25 + 0.5)
  63.        
  64.         new {
  65.             x = math.random(w);
  66.             color = colors.lime;
  67.             tail = tail;
  68.             eraser = eraser;
  69.             ishead = true;
  70.         }
  71.        
  72.         creator:run()
  73.         tail:run()
  74.         eraser:run()
  75.     else
  76.         for char in chars() do
  77.             if char.ishead then
  78.                 if t == char.tail.id then
  79.                     new {
  80.                         x = char.x;
  81.                         color = colors.green;
  82.                     }
  83.                     break
  84.                 elseif t == char.eraser.id then
  85.                     new {
  86.                         x = char.x;
  87.                         color = colors.black;
  88.                     }
  89.                     break
  90.                 end
  91.             end
  92.         end
  93.     end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment