Advertisement
Guest User

image

a guest
Oct 20th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. local monitorSide = "left"
  2. local file = "skgk.nfp" --file to read from
  3. local monitor = peripheral.wrap(monitorSide)
  4. local aniIdentifier = "--!--" --Identifier for new animation frame
  5.  
  6. --returns the color from ID
  7. function getColor(hex)
  8.   if hex == "0" then return colors.white
  9.   elseif hex == "1" then return colors.orange
  10.   elseif hex == "2" then return colors.magenta
  11.   elseif hex == "3" then return colors.lightBlue
  12.   elseif hex == "4" then return colors.yellow
  13.   elseif hex == "5" then return colors.lime
  14.   elseif hex == "6" then return colors.pink
  15.   elseif hex == "7" then return colors.gray
  16.   elseif hex == "8" then return colors.lightGray
  17.   elseif hex == "9" then return colors.cyan
  18.   elseif hex == "a" then return colors.purple
  19.   elseif hex == "b" then return colors.blue
  20.   elseif hex == "c" then return colors.brown
  21.   elseif hex == "d" then return colors.green
  22.   elseif hex == "e" then return colors.red
  23.   elseif hex == "f" then return colors.black
  24.   else return colors.black
  25.   end
  26. end
  27. --Reads in a file and returns it in an 3 dimensional array (frame, x, y)
  28. function readFile(file)
  29.     result = {}
  30.  
  31.     file = fs.open(file, "r")
  32.     line = file.readLine()
  33.     index = 0
  34.     animation = 0
  35.     result[animation] = {}
  36.    
  37.     while line ~= nil do
  38.    
  39.       if line == aniIdentifier then
  40.         animation = animation + 1
  41.         result[animation] = {}
  42.         index = 0
  43.         line = file.readLine()
  44.       else
  45.           result[animation][index] = {}
  46.           for i = 0, string.len(line) do
  47.             char = string.sub(line, i, i)
  48.             if char == nil then char = "" end
  49.             result[animation][index][i] = char
  50.           end
  51.           index = index + 1
  52.           line = file.readLine()
  53.         end
  54.        
  55.     end
  56.     file.close()
  57.     return result
  58. end
  59.  
  60. --Prints a 2D array to the screen. Currently unused.
  61. function print2DArray(a)
  62.   for i = 0, #a, 1 do
  63.     for j = 0, #a[i], 1 do
  64.       item = a[i][j];
  65.       if item == nil then item = "null" end
  66.       write(item)
  67.     end
  68.     print()
  69.   end
  70. end
  71.  
  72. --Draws the frame to the screen.
  73. function draw(a)
  74.   for i = 0, #a, 1 do
  75.     for j = 0, #a[i], 1 do
  76.         item = a[i][j];
  77.         monitor.setCursorPos(j, i + 1)
  78.         monitor.setBackgroundColor(getColor(item))
  79.         monitor.write(" ")
  80.     end
  81.     print()
  82.   end
  83. end
  84.  
  85. --Resets the monitor
  86. monitor.setCursorPos(1, 1)
  87. monitor.setBackgroundColor(colors.black)
  88. monitor.clear()
  89.  
  90. local width = 8 * 10 + 2;
  91. local height = 5 * 6 + 3;
  92.  
  93. pixels = readFile(file)
  94.  
  95. --Loops through all the pixels and draws them!
  96. if #pixels == 1 then
  97.     draw(pixels[0])
  98. else
  99.     while true do
  100.         for i = 0, #pixels do
  101.             monitor.clear()
  102.             monitor.setCursorPos(1, 1)
  103.             draw(pixels[i])
  104.             sleep(0.1)
  105.         end
  106.     end
  107. end
  108.  
  109. --Resets cursor position so image is always seen from top left
  110. monitor.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement