Advertisement
FCKJesus

dream

Nov 10th, 2020
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. local event = require("event")
  2. local term = require("term")
  3.  
  4. local gpu = require("component").gpu
  5.  
  6. local width, height = gpu.getResolution()
  7. height = height * 2
  8.  
  9. local minLen, maxLen = 100, 200
  10.  
  11. local pipes = {}
  12. local sides = {
  13.   top = 1,
  14.   left = 2,
  15.   bottom = 3,
  16.   right = 4,
  17.   "top",
  18.   "left",
  19.   "bottom",
  20.   "right"
  21. }
  22. local char = "▀"
  23.  
  24. local turn = 5
  25.  
  26. local oldFg, oldBg = gpu.getForeground(), gpu.getBackground()
  27. gpu.fill(1, 1, width, height / 2, " ")
  28.  
  29. local spawnInterval = 500
  30. local lifeTime = 600
  31. local clearInterval = 2500
  32.  
  33. local i = 0
  34. while true do
  35.   if i % spawnInterval == 0 then
  36.     local x, y
  37.     local side = math.random(1, 4)  -- choose one of 4 sides
  38.     local direction
  39.     if side == sides.top then
  40.       x, y = math.random(1, width), 1
  41.       direction = sides.bottom
  42.     elseif side == sides.left then
  43.       x, y = 1, math.random(1, height)
  44.       direction = sides.right
  45.     elseif side == sides.bottom then
  46.       x, y = math.random(1, width), height
  47.       direction = sides.top
  48.     elseif side == sides.right then
  49.       x, y = width, math.random(1, height)
  50.       direction = sides.left
  51.     end
  52.     local length = math.random(minLen, maxLen)
  53.     local r, g, b = 0, 0, 0
  54.     while r < 127 and g < 127 and b < 127 do
  55.       r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255)
  56.     end
  57.     local color = r * 0x10000 + g * 0x100 + b
  58.     local pipe = {
  59.       x = x,
  60.       y = y,
  61.       length = length,
  62.       dir = direction,
  63.       color = color,
  64.       life = lifeTime
  65.     }
  66.     table.insert(pipes, pipe)
  67.   end
  68.  
  69.   for n = #pipes, 1, -1 do
  70.     local pipe = pipes[n]
  71.     if pipe.life == 0 then
  72.       table.remove(pipes, n)
  73.     else
  74.       local c, fg, bg = gpu.get(pipe.x, 1 + math.floor((pipe.y - 1) / 2))
  75.       if c ~= char then
  76.         fg = oldBg
  77.         bg = oldBg
  78.       end
  79.       local part = (pipe.y - 1) % 2
  80.       local sBg, sFG
  81.       if part == 0 then
  82.         sFg = pipe.color
  83.         sBg = bg
  84.       else
  85.         sFg = fg
  86.         sBg = pipe.color
  87.       end
  88.  
  89.       if gpu.getForeground() ~= sFg then
  90.         gpu.setForeground(sFg)
  91.       end
  92.       if gpu.getBackground() ~= sBg then
  93.         gpu.setBackground(sBg)
  94.       end
  95.  
  96.       gpu.set(pipe.x, 1 + math.floor((pipe.y - 1) / 2), char)
  97.  
  98.  
  99.       local dir = pipe.dir
  100.  
  101.       local shouldTurn = math.random(1, 100) <= turn
  102.       local remains = {
  103.         pipe.y - 2,
  104.         pipe.x - 2,
  105.         height - pipe.y - 1,
  106.         width - pipe.x - 1
  107.       }
  108.  
  109.       if remains[dir] == 0 then
  110.         shouldTurn = true
  111.       end
  112.  
  113.       if shouldTurn then
  114.         local options = {dir - 1, dir + 1}
  115.         if options[1] == 0 then
  116.           options[1] = 4
  117.         end
  118.         if options[1] == 5 then
  119.           options[1] = 1
  120.         end
  121.         if options[2] == 0 then
  122.           options[2] = 4
  123.         end
  124.         if options[2] == 5 then
  125.           options[2] = 1
  126.         end
  127.  
  128.         if remains[options[2]] < 1 then
  129.           table.remove(options, 2)
  130.         end
  131.         if remains[options[1]] < 1 then
  132.           table.remove(options, 1)
  133.         end
  134.  
  135.         dir = options[math.random(1, #options)]
  136.       end
  137.  
  138.       pipe.dir = dir
  139.       if pipe.dir == sides.top then
  140.         pipe.y = pipe.y - 1
  141.       elseif pipe.dir == sides.left then
  142.         pipe.x = pipe.x - 1
  143.       elseif pipe.dir == sides.right then
  144.         pipe.x = pipe.x + 1
  145.       elseif pipe.dir == sides.bottom then
  146.         pipe.y = pipe.y + 1
  147.       end
  148.       pipe.life = pipe.life - 1
  149.     end
  150.   end
  151.   i = i + 1
  152.   if i >= clearInterval then
  153.     if event.pull(2, "interrupted") then
  154.       break
  155.     end
  156.     gpu.setBackground(0x000000)
  157.     gpu.setForeground(0xffffff)
  158.     gpu.fill(1, 1, width, height / 2, " ")
  159.     i = 0
  160.     pipes = {}
  161.   else
  162.     if event.pull(.05, "interrupted") then
  163.       break
  164.     end
  165.   end
  166. end
  167.  
  168. gpu.setForeground(oldFg)
  169. gpu.setBackground(oldBg)
  170.  
  171. gpu.fill(1, 1, width, height / 2, " ")
  172.  
  173. term.setCursor(1, 1)
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement