Advertisement
Guest User

FILE

a guest
Mar 24th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. --#filename:matrix-ss
  2. --#version:0.8
  3. --#author:Advert
  4. -- Matrix screensaver v0.8
  5. -- (C) 2012 User 'Advert' at http://www.computercraft.info/forums2/
  6. -- X11/mit licence (use/modify at your will, but please, leave credit where due)
  7. -- Spacebar to quit cleanly!
  8. local monitor = peripheral.wrap( "top" )
  9. monitor.setTextColor(colors.green )
  10. monitor.setTextScale(0.5 )
  11. local mobj = {}
  12.  
  13. do -- Standart variables.
  14.     mobj.x = 0
  15.     mobj.y = 0 -- Offscreen
  16.     mobj.lingerMin = 3 -- Minimum time to linger (frames)
  17.     mobj.lingerMax = 6 -- Maximum time to linger
  18.     mobj.linger = 4 -- How long this object will linger
  19.     mobj.tailLenMin = 3 -- Minimum tail length
  20.     mobj.tailLenMax = 7 -- Maximum tail length
  21.     mobj.tailLen = 5
  22.     mobj.chars = "1234567890-=!@#$%^&*()_+qwertyuiop[]QWERTYUIOP{}asdfghjkl;'\\ASDFGHJKL:\"|zxcvbnm,./ZXCVBNM<>?"
  23.     mobj.charsLen = mobj.chars:len()
  24.     mobj.currentTail = {"a", "a", "a", "a", "a", "a", "a", "a", "a", "a"}
  25.     mobj.secretText = {}
  26.     mobj.skipFrame = 0
  27.     mobj.skipFrameMin = 1
  28.     mobj.skipFrameMax = 3
  29.     mobj.skipFrameMod = -1
  30.     mobj.currFrame = 0
  31. end
  32.  
  33. function mobj:new(o, ...)
  34.     local o = o or {}
  35.     setmetatable(o, self)
  36.     self.__index = self
  37.     o:__init(...)
  38.     return o
  39. end
  40.  
  41.  
  42. function mobj:__init(x)
  43.     self.linger = math.random(self.lingerMin, self.lingerMax)
  44.     self.tailLen = math.random(self.tailLenMin, self.tailLenMax)
  45.     self.x = x
  46.     self.y = 1
  47.     self.nFrame = 1
  48.     self.currentTail = {}
  49.     self.skipFrame = math.random(self.skipFrameMin, self.skipFrameMax) + self.skipFrameMod
  50. end
  51. function mobj:randomLinger()
  52.     self.linger = math.random(self.lingerMin, self.lingerMax)
  53. end
  54. function mobj:printPos(x, y, sText)
  55. --  term.setCursorPos(x, y)
  56.  monitor.setCursorPos(x, y)
  57. --  term.write(sText)
  58.  monitor.write(sText)
  59. end
  60. function mobj:printPosT(x, y, sText)
  61.     if self.secretText[y] and self.secretText[y][x] then
  62.         sText = self.secretText[y][x]
  63.     end
  64.     return self:printPos(x, y, sText)
  65. end
  66. function mobj:randomChar()
  67.     local r = math.random(1, self.charsLen)
  68.     return string.sub(self.chars, r, r)
  69. end
  70. function mobj:printTail()
  71.     for i = 1, self.tailLen do
  72.         if not self.currentTail[i] then break end
  73.         self:printPosT(self.x, self.y - i, self.currentTail[i])
  74.     end
  75. end
  76.  
  77. loc al termX, termY = term.getSize()
  78. do
  79.     for y = 1, termY do
  80.         mobj.secretText[y] = {}
  81.     end
  82.     function mobj:addSecretText(x, y, sText)
  83.         if sText:len() + x > termX then
  84.             -- lolno.
  85.         else
  86.             for i = 1, sText:len() do
  87.                 local chr = sText:sub(i, i)
  88.                 self.secretText[y][x + i-1] = chr
  89.             end
  90.         end
  91.     end
  92.     function mobj:addSecretTextD(x, y, dir, sText)
  93.         -- 1 = bottomright, 2 = bottomleft, 3 = topright, 4 = topleft
  94.         local xmod = dir %2 == 1 and 1 or -1
  95.         local ymod = (dir == 1 or dir == 2) and 1 or -1
  96.         local finalx, finaly = x + (xmod * sText:len()), y + (ymod *sText:len())
  97.         --print("finalx, finaly", finalx, finaly)
  98.         --read()
  99.         local cx, cy = x, y
  100.         for i = 1, sText:len() do
  101.             local cx, cy = x + (xmod * (i -1)), y + (ymod * (i -1))
  102.             if self.secretText[cy] then
  103.                 self.secretText[cy][cx] = sText:sub(i, i)
  104.             end
  105.         end
  106.     end
  107.    
  108. end
  109.  
  110. function mobj:render()
  111.     self:printTail()
  112.     local sSkip = false
  113.     if self.currFrame ~= self.skipFrame then
  114.         self.currFrame = self.currFrame + 1
  115.     else
  116.         self.currFrame = 0
  117.         self.nFrame = self.nFrame + 1
  118.         if self.nFrame == self.linger then
  119.             table.insert(self.currentTail, 1, self.lastRandomChar)
  120.             self:printPosT(self.x, self.y, self.currentTail[1])
  121.             self.y = self.y + 1
  122.             self.nFrame = 1
  123.             if self.y > termY + self.tailLen then
  124.                 self:__init(self.x)
  125.             end
  126.             sSkip = true
  127.         else
  128.             self.lastRandomChar = self:randomChar()
  129.         end
  130.     end
  131.     if not sSkip then
  132.         self:printPos(self.x, self.y, self.lastRandomChar)
  133.     end
  134. end
  135.  
  136. local objects = {}
  137. local eventT
  138.  
  139. function init()
  140.     for i = 1, termX do
  141.         objects[i] = mobj:new({}, i)
  142.     end
  143.     eventT = os.startTimer(0)
  144. end
  145.  
  146. function render()
  147.     term.clear()
  148.     for _, k in pairs(objects) do
  149.         k:render()
  150.     end
  151. end
  152.  
  153. mobj:addSecretTextD(15, 4, 1, "Casper7526")
  154. mobj:addSecretTextD(18, 4, 1, "made me")
  155. mobj:addSecretTextD(21, 4, 1, "do this.")
  156. mobj:addSecretText(15, 15, "Spacebar to exit.")
  157. init()
  158.  
  159. while true do
  160.     local e, p1, p2 = os.pullEvent()
  161.     if e == "timer" and p1 == eventT then
  162.         eventT = os.startTimer(0.05) -- 20 fps
  163.         render()
  164.     elseif e == "key" and p1 == 57 then
  165.         term.clear()
  166.         term.setCursorPos(1,1)
  167.         break
  168.     end
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement