Advertisement
Aadvert

Matrix

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