Advertisement
BombBloke

Pipe Mania (ComputerCraft)

May 6th, 2016
1,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.03 KB | None | 0 0
  1. -- +--------------------------------------------------------+
  2. -- |                                                        |
  3. -- |                      Pipe Mania                        |
  4. -- |                                                        |
  5. -- +--------------------------------------------------------+
  6.  
  7. local version = "Version 1.0.1"
  8.  
  9. -- A clone for ComputerCraft, by Jeffrey Alexander, aka Bomb Bloke.
  10. -- Requires an Advanced Computer or better, plus ComputerCraft 1.76+ / Minecraft 1.8+.
  11. -- http://www.computercraft.info/forums2/index.php?/topic/26615-mc18-pipe-mania/
  12.  
  13. ---------------------------------------------
  14. ------------Variable Declarations------------
  15. ---------------------------------------------
  16.  
  17. if not blittle then
  18.     if not (fs.exists("blittle") or fs.exists(shell.resolve("blittle"))) then
  19.         shell.run("pastebin get ujchRSnU blittle")
  20.         os.loadAPI(shell.resolve("blittle"))
  21.     else os.loadAPI(fs.exists("blittle") and "blittle" or shell.resolve("blittle")) end
  22. end
  23.  
  24. local img = {{"770077","770077","000000","000000","770077","770077"},  --  1 Cross pipe.
  25.         {"777777","777777","000000","000000","777777","777777"},  --  2 Horizontal pipe.
  26.         {"770077","770077","770077","770077","770077","770077"},  --  3 Vertical pipe.
  27.         {"770077","770077","770000","777000","777777","777777"},  --  4 Top-right curve.
  28.         {"777777","777777","777000","770000","770077","770077"},  --  5 Bottom-right curve.
  29.         {"777777","777777","000777","000077","770077","770077"},  --  6 Bottom-left curve.
  30.         {"770077","770077","000077","000777","777777","777777"},  --  7 Top-left curve.
  31.         {"777777","787787","778877","778877","787787","777777"},  --  8 No pipe.
  32.         {"777777","7e7777","7eeee7","777e77","77ee77","777777"},  --  9 Explody pipe 1.
  33.         {"eee4ee","e4eee4","e4444e","44e4e4","e444ee","ee44ee"},  -- 10 Explody pipe 2.
  34.         {"777777","747774","777747","474777","777774","747777"}}  -- 11 Explody pipe 3.
  35.  
  36. local mon, next, grid, level, score = blittle.createWindow(), {}, {}, 1, 0
  37. local dirTemplate, dents = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}, {{"55", "55"}, {"55", "75"}, {"75", "55"}, {"57", "55"}, {"55", "57"}}
  38. local myEvent, distance, delay, curDelay, floozTimer, xSize, ySize, curX, curY, subX, subY, direction, dirBumpX, dirBumpY, exploding, fast, scores, kiosk
  39. local keyboard = {" ABCDEFGHIJKLMNOPQRSTUVWXYZ ", " abcdefghijklmnopqrstuvwxyz ", " 1234567890 !'$ \001\003\011\012\015 \027 END "}
  40.  
  41. ---------------------------------------------
  42. ------------Function Declarations------------
  43. ---------------------------------------------
  44.  
  45. -- Writes regular text at the specified location on the screen.
  46. local function writeAt(text, x , y, fgCol, bgCol)
  47.     if fgCol then term.setTextColour(fgCol) end
  48.     if bgCol then term.setBackgroundColour(bgCol) end
  49.     term.setCursorPos(x, y)
  50.     term.write(text)
  51. end
  52.  
  53. -- Returns whether one of a given set of keys was pressed.
  54. local function pressedKey(...)
  55.     if myEvent[1] ~= "key" then return false end
  56.     local key = myEvent[2]
  57.     for i=1,#arg do if arg[i] == key then return true end end
  58.     return false
  59. end
  60.  
  61. -- Returns whether a click was performed at a given location.
  62. -- If one parameter is passed, it checks to see if y is [1].
  63. -- If two parameters are passed, it checks to see if x is [1] and y is [2].
  64. -- If three parameters are passed, it checks to see if x is between [1]/[2] (non-inclusive) and y is [3].
  65. -- If four paramaters are passed, it checks to see if x is between [1]/[2] and y is between [3]/[4] (non-inclusive).
  66. local function clickedAt(...)
  67.     if myEvent[1] ~= "mouse_click" then return false end
  68.     local x, y = myEvent[3], myEvent[4]
  69.     if #arg == 1 then return (arg[1] == y)
  70.     elseif #arg == 2 then return (x == arg[1] and y == arg[2])
  71.     elseif #arg == 3 then return (x > arg[1] and x < arg[2] and y == arg[3])
  72.     else return (x > arg[1] and x < arg[2] and y > arg[3] and y < arg[4]) end
  73. end
  74.  
  75. -- Draw a section of pipe.
  76. local function drawTile(img, x, y)
  77.     for i = 1, 6 do
  78.         mon.setCursorPos(x, y + i - 1)
  79.         mon.blit("      ", "      ", img[i])
  80.     end
  81. end
  82.  
  83. -- tostring() with leading zeroes.
  84. local function zeroes(num, length)
  85.     local result = tostring(num)
  86.     return #result < length and (string.rep("0", length - #result) .. result) or result
  87. end
  88.  
  89. -- (Re)draws displayed labels and so on.
  90. local function drawLabels()
  91.     if not kiosk then writeAt("\215", xSize, 1, colours.white, colours.red) end
  92.     writeAt(" \187 ", 5, 5, fast and colours.red or colours.white, colours.grey)
  93.     writeAt(" P ", 5, 3, colours.white)
  94.     writeAt(" PIPE MANIA ", 20, 1)
  95.     writeAt(" LVL: " .. zeroes(level, 2) .. "  SCORE: " .. zeroes(score, 7) .. " DIST: " .. zeroes(distance, 2) .. " ", 10, 18)
  96. end
  97.  
  98. -- Prepare the screen and re-init the playing grid for a new level.
  99. local function newLevel()
  100.     delay, exploding, fast = 18 - level, false, false
  101.     if delay < 1 then delay = 1 end
  102.     curDelay = delay
  103.     distance = 13 + math.floor((level + 1) / 2)
  104.  
  105.     local oldTerm = term.redirect(mon)
  106.     mon.setVisible(false)
  107.    
  108.     term.setBackgroundColour(colours.lightGrey)
  109.     term.clear()
  110.    
  111.     local back = img[8]
  112.     for y = 1, 7 do
  113.         grid[y] = {}
  114.         for x = 1, 10 do drawTile(back, (x - 1) * 6 + 23, (y - 1) * 6 + 7) end
  115.     end
  116.    
  117.     curX, curY, subX, subY, direction = math.random(8) + 1, math.random(5) + 1, 2, 2, math.random(4)
  118.     grid[curY][curX] = 8
  119.    
  120.     local midX, midY = 19 + curX * 6, 3 + curY * 6
  121.     paintutils.drawFilledBox(midX - 2, midY - 2, midX + 3, midY + 3, colours.grey)
  122.     paintutils.drawBox(midX, midY, midX + 1, midY + 1, colours.lime)
  123.     dirBumpX, dirBumpY = dirTemplate[direction][1], dirTemplate[direction][2]
  124.     midX, midY = midX + dirBumpX * 2, midY + dirBumpY * 2
  125.     paintutils.drawBox(midX, midY, midX + 1, midY + 1, colours.white)
  126.    
  127.     paintutils.drawBox( 8, 43, 15, 49, colours.cyan)
  128.     paintutils.drawBox(92,  9, 95, 46, colours.grey)
  129.     paintutils.drawBox(93, 10, 94, 45, colours.lime)
  130.     paintutils.drawBox(22,  6, 83, 49, colours.black)
  131.     paintutils.drawBox( 8, 18, 15, 42)
  132.     paintutils.drawBox(40,  0, 63,  4)
  133.     paintutils.drawBox(18, 51, 87, 55)
  134.     paintutils.drawBox( 8,  6, 15, 10)
  135.     paintutils.drawBox( 8, 12, 15, 16)
  136.    
  137.     for i = 1, 5 do
  138.         next[i] = math.random(7)
  139.         drawTile(img[next[i]], 9, 49 - i * 6)
  140.     end
  141.    
  142.     term.redirect(oldTerm)
  143.     mon.setVisible(true)
  144.    
  145.     drawLabels()
  146.  
  147.     floozTimer = os.startTimer(0.5)
  148. end
  149.  
  150. -- Tries to jump flooz to a new pipe segment, returns false if impossible.
  151. local function checkMovement()
  152.     curX, curY = curX + dirBumpX, curY + dirBumpY
  153.     if curX < 1 or curX > 10 or curY < 1 or curY > 7 then return false end
  154.    
  155.     local pipeType = grid[curY][curX]
  156.    
  157.     if not pipeType or pipeType == 8 then return false end
  158.    
  159.     if (pipeType == 2 or pipeType == 3 or pipeType == 9 or pipeType == 10) and direction % 2 ~= pipeType % 2 then return false end
  160.    
  161.     if pipeType > 3 and pipeType < 8 then
  162.         pipeType = (pipeType - direction) % 4
  163.         if pipeType > 1 then return false end
  164.     end
  165.    
  166.     if subX < 1 then subX = 3 elseif subX > 3 then subX = 1 elseif subY < 1 then subY = 3 elseif subY > 3 then subY = 1 end
  167.    
  168.     return true
  169. end
  170.  
  171. -- Render moving flooz.
  172. local function drawFlooz(pipeType)
  173.     local thisBlob, midX, midY = dents[pipeType and (pipeType - 2) or 1], curX * 6 + subX * 2 + 15, curY * 6 + subY * 2 - 1
  174.    
  175.     mon.setCursorPos(midX, midY)
  176.     mon.blit("  ", "  ", thisBlob[1])
  177.     mon.setCursorPos(midX, midY + 1)
  178.     mon.blit("  ", "  ", thisBlob[2])
  179. end
  180.  
  181. -- Press a key to continue (or exit).
  182. local function pressAnyKey()
  183.     while true do
  184.         myEvent = {coroutine.yield()}
  185.  
  186.         if (clickedAt(xSize, 1) or pressedKey(keys.x, keys.q) or myEvent[1] == "terminate") and not kiosk then
  187.             if myEvent[1] == "key" then os.pullEvent("char") end
  188.             return false
  189.        
  190.         elseif myEvent[1] == "key" or myEvent[1] == "mouse_click" then
  191.             return true
  192.         end
  193.     end
  194. end
  195.  
  196. -- High-score board.
  197. local function scoreboard(level, score)
  198.     if not scores and (fs.exists(shell.resolve("pipemania.dat")) or fs.exists("pipemania.dat")) then
  199.         local input = fs.open(fs.exists(shell.resolve("pipemania.dat")) and shell.resolve("pipemania.dat") or "pipemania.dat", "r")
  200.         scores = textutils.unserialise(input.readAll())
  201.         input.close()
  202.     end
  203.    
  204.     if type(scores) ~= "table" then
  205.         scores = {{10, 10000, "APE"},
  206.             {   9,  9000, "BAT"},
  207.             {   8,  8000, "CAT"},
  208.             {   7,  7000, "DOG"},
  209.             {   6,  6000, "EEL"},
  210.             {   5,  5000, "NAZ"},
  211.             {   4,  4000, "CAC"},
  212.             {   3,  3000, "ORP"},
  213.             {   2,  2000, "ORA"},
  214.             {   1,  1000, "TIO"}}
  215.     end
  216.    
  217.     local newScore = false
  218.     for i = 1, 10 do if score > scores[i][2] then
  219.         table.insert(scores, i, {level, score, ""})
  220.         scores[11], newScore = nil, i
  221.         break
  222.     end end
  223.    
  224.     local oldTerm = term.redirect(mon)
  225.     mon.setVisible(false)
  226.     term.setBackgroundColour(colours.lightGrey)
  227.     term.clear()
  228.     paintutils.drawFilledBox(17,  7, 84, 42, colours.grey)
  229.     paintutils.drawBox(16,  6, 85, 43, colours.black)
  230.     paintutils.drawBox(40,  0, 65,  4)
  231.     paintutils.drawLine(16, 11, 85, 11)
  232.     term.redirect(oldTerm)
  233.     mon.setVisible(true)
  234.    
  235.     if not kiosk then writeAt("\215", xSize, 1, colours.white, colours.red) end
  236.     writeAt(" High Scores ", 20, 1, colours.white, colours.grey)
  237.     writeAt(" Level        Name         Score  ", 9, 3)
  238.    
  239.     for i = 1, 10 do
  240.         term.setTextColour(i == newScore and colours.orange or colours.white)
  241.         local thisScore = scores[i]
  242.         writeAt(zeroes(thisScore[1], 2), 12, 4 + i)
  243.         writeAt(thisScore[3], 16 + math.floor((18 - #thisScore[3]) / 2), 4 + i)
  244.         writeAt(zeroes(thisScore[2], 7), 35, 4 + i)
  245.     end
  246.    
  247.     if newScore then
  248.         term.redirect(mon)
  249.         paintutils.drawBox(22,  45, 79, 55, colours.black)
  250.         term.redirect(oldTerm)
  251.        
  252.         for i = 1, 3 do writeAt(keyboard[i], 12, 15 + i, colours.white, colours.grey) end
  253.        
  254.         term.setTextColour(colours.orange)
  255.         term.setCursorPos(16, 4 + newScore)
  256.         term.setCursorBlink(true)
  257.        
  258.         local name, curPos = "", 1
  259.        
  260.         -- Name entry:
  261.         while true do
  262.             myEvent = {coroutine.yield()}
  263.            
  264.             if (clickedAt(xSize, 1) or myEvent[1] == "terminate") and not kiosk then
  265.                 if myEvent[1] == "key" then os.pullEvent("char") end
  266.                 term.setCursorBlink(false)
  267.                 return false
  268.            
  269.             elseif myEvent[1] == "char" and #name < 18 then
  270.                 name = name:sub(1, curPos - 1) .. myEvent[2] .. name:sub(curPos)
  271.                 curPos = curPos + 1
  272.                 writeAt(name, 16, 4 + newScore)
  273.                 term.setCursorPos(16 + curPos - 1, 4 + newScore)
  274.            
  275.             elseif (clickedAt(34, 18) or pressedKey(keys.backspace)) and curPos > 1 then
  276.                 name = name:sub(1, curPos - 2) .. name:sub(curPos)
  277.                 curPos = curPos - 1
  278.                 writeAt(name .. " ", 16, 4 + newScore)
  279.                 term.setCursorPos(16 + curPos - 1, 4 + newScore)
  280.            
  281.             elseif pressedKey(keys.left) and curPos > 1 then
  282.                 curPos = curPos - 1
  283.                 term.setCursorPos(16 + curPos - 1, 4 + newScore)
  284.            
  285.             elseif pressedKey(keys.right) and curPos < 19 and curPos < #name + 1 then
  286.                 curPos = curPos + 1
  287.                 term.setCursorPos(16 + curPos - 1, 4 + newScore)
  288.            
  289.             elseif clickedAt(35, 39, 18) or pressedKey(keys.enter) then
  290.                 break
  291.            
  292.             elseif clickedAt(12, 39, 15, 19) and not clickedAt(33, 39, 18) and #name < 18 then
  293.                 local x = myEvent[3] - 11
  294.                 name = name:sub(1, curPos - 1) .. keyboard[myEvent[4] - 15]:sub(x, x) .. name:sub(curPos)
  295.                 curPos = curPos + 1
  296.                 writeAt(name, 16, 4 + newScore)
  297.                 term.setCursorPos(16 + curPos - 1, 4 + newScore)
  298.            
  299.             end
  300.         end
  301.        
  302.         term.setCursorBlink(false)
  303.        
  304.         if name == "" then name = "---" end
  305.         scores[newScore][3] = name
  306.         writeAt(string.rep(" ", 18), 16, 4 + newScore)
  307.         writeAt(name, 16 + math.floor((18 - #name) / 2), 4 + newScore)
  308.        
  309.         local output = fs.open(shell.resolve("pipemania.dat"), "w") or fs.open("pipemania.dat", "w")
  310.        
  311.         if output then
  312.             output.writeLine(textutils.serialise(scores))
  313.             output.close()
  314.         end
  315.     else
  316.         writeAt("You:", 24, 17, colours.black, colours.lightGrey)
  317.         writeAt("Level - " .. zeroes(level, 2) .. "  Score - " .. zeroes(score, 7), 13, 18, colours.black, colours.lightGrey)
  318.     end
  319.    
  320.     return pressAnyKey()
  321. end
  322.  
  323. ---------------------------------------------
  324. ------------         Init        ------------
  325. ---------------------------------------------
  326.  
  327. -- Check display capabilities:
  328. if term.current().setTextScale then
  329.     local curScale = 5
  330.    
  331.     repeat
  332.         term.current().setTextScale(curScale)
  333.         xSize, ySize = term.getSize()
  334.         if xSize > 49 and ySize > 17 then break end
  335.         curScale = curScale - 0.5
  336.     until curScale == 0
  337. else xSize, ySize = term.getSize() end
  338.    
  339. if xSize < 50 or ySize < 18 then error("Sorry, a larger display is required.") end
  340. if not term.isColour() then error("Sorry, a colour display is required.") end
  341.  
  342. -- Kiosk mode?
  343. do
  344.     local arg = {...}
  345.     for i = 1, #arg do if arg[i]:lower() == "-k" then kiosk = true end end
  346. end
  347.  
  348. -- Splash screen:
  349. mon.setVisible(false)
  350. for y = 0, 9 do for x = 0, 16 do drawTile(img[math.random(7)], x * 6 + 1, y * 6 + 1) end end
  351. mon.setVisible(true)
  352. writeAt(" PIPE MANIA ", 20, 9, colours.red, colours.black)
  353. writeAt(" " .. version .. " ", math.floor((51 - #version - 2) / 2) + 1, 10, colours.yellow)
  354. repeat myEvent = {coroutine.yield()} until myEvent[1] == "key" or myEvent[1] == "mouse_click"
  355.  
  356. -- Prepare new game:
  357. level, score = 1, 0
  358. newLevel()
  359.  
  360. ---------------------------------------------
  361. ------------  Main Program Loop  ------------
  362. ---------------------------------------------
  363.  
  364. while true do
  365.     myEvent = {coroutine.yield()}
  366.    
  367.     -- Attempt to place tile:
  368.     if clickedAt(11, 42, 2, 17) and not exploding then
  369.         local x, y = math.floor((myEvent[3] - 12) / 3) + 1, math.floor((myEvent[4] - 3) / 2) + 1
  370.        
  371.         -- Empty tile, place pipe:
  372.         if not grid[y][x] then
  373.             grid[y][x] = table.remove(next, 1)
  374.             next[5] = math.random(7)
  375.             for i = 1, 5 do drawTile(img[next[i]], 9, 49 - i * 6) end
  376.             drawTile(img[grid[y][x]], (x - 1) * 6 + 23, (y - 1) * 6 + 7)
  377.        
  378.         -- Filled tile, explode pipe:
  379.         elseif grid[y][x] < 8 and not (curX == x and curY == y) then
  380.             grid[y][x] = 8
  381.             drawTile(img[9], (x - 1) * 6 + 23, (y - 1) * 6 + 7)
  382.             exploding = {x, y, os.startTimer(0.4), 1}
  383.         end
  384.    
  385.     -- Pause:
  386.     elseif clickedAt(4, 8, 3) or pressedKey(keys.p) then
  387.         local blank = string.rep(" ", 30)
  388.         term.setTextColour(colours.white)
  389.         term.setBackgroundColour(colours.black)
  390.         for i = 1, 14 do writeAt(blank, 12, 2 + i) end
  391.        
  392.         local blink1, blink2 = os.startTimer(0), os.startTimer(1)
  393.        
  394.         while true do
  395.             myEvent = {coroutine.yield()}
  396.            
  397.             if (clickedAt(xSize, 1) or pressedKey(keys.x, keys.q) or myEvent[1] == "terminate") and not kiosk then
  398.                 if myEvent[1] == "key" then os.pullEvent("char") end
  399.                 blank = false
  400.                 break
  401.             elseif clickedAt(4, 8, 3) or pressedKey(keys.p) then
  402.                 break
  403.             elseif myEvent[1] == "timer" and myEvent[2] == blink1 then
  404.                 writeAt("PAUSED", 24, 9)
  405.                 blink1 = os.startTimer(2)
  406.             elseif myEvent[1] == "timer" and myEvent[2] == blink2 then
  407.                 writeAt("      ", 24, 9)
  408.                 blink2 = os.startTimer(2)
  409.             end
  410.         end
  411.        
  412.         if not blank then break end
  413.        
  414.         mon.redraw()
  415.         drawLabels()
  416.         floozTimer = os.startTimer(0)
  417.    
  418.     -- Toggle speed:
  419.     elseif clickedAt(4, 8, 5) or pressedKey(keys.f) then
  420.         fast = not fast
  421.         writeAt(" \187 ", 5, 5, fast and colours.red or colours.white, colours.grey)
  422.        
  423.     -- Timers:
  424.     elseif myEvent[1] == "timer" then
  425.        
  426.         -- Explosion animation timer:
  427.         if exploding and myEvent[2] == exploding[3] then
  428.             exploding[4] = exploding[4] + 1
  429.  
  430.             -- Finished exploding, replace pipe:
  431.             if exploding[4] > 3 then
  432.                 local x, y = exploding[1], exploding[2]
  433.                 grid[y][x] = table.remove(next, 1)
  434.                 next[5], exploding, score = math.random(7), false, score > 49 and (score - 50) or 0
  435.                 for i = 1, 5 do drawTile(img[next[i]], 9, 49 - i * 6) end
  436.                 drawTile(img[grid[y][x]], (x - 1) * 6 + 23, (y - 1) * 6 + 7)
  437.                 writeAt(zeroes(score, 7), 27, 18, colours.white, colours.grey)
  438.  
  439.             -- Continue exploding animation:
  440.             else
  441.                 exploding[3] = os.startTimer(0.4)
  442.                 drawTile(img[8 + exploding[4]], (exploding[1] - 1) * 6 + 23, (exploding[2] - 1) * 6 + 7)
  443.             end
  444.  
  445.         -- Flooz timer:
  446.         elseif myEvent[2] == floozTimer then
  447.            
  448.             -- Flooz hasn't started yet, update progress bar to the right:
  449.             if curDelay > 0 then
  450.                 local top = 35 - math.floor(curDelay / delay * 35)
  451.                 curDelay = fast and 0 or (curDelay - 0.5)
  452.                 local bottom = 35 - math.floor(curDelay / delay * 35)
  453.                
  454.                 mon.setBackgroundColour(colours.black)
  455.                 for i = top, bottom do
  456.                     mon.setCursorPos(93, i + 10)
  457.                     mon.write("  ")
  458.                 end
  459.                
  460.                 floozTimer = os.startTimer(fast and 0 or 0.5)
  461.                
  462.             -- Flooz is moving:
  463.             else
  464.                 subX, subY = subX + dirBumpX, subY + dirBumpY
  465.                
  466.                 local pipeType = grid[curY][curX]
  467.                
  468.                 if subX == 2 and subY == 2 then
  469.                    
  470.                     -- Middle of a corner changes direction:
  471.                     if pipeType > 3 and pipeType < 8 then
  472.                         direction = direction + ((pipeType + direction) % 2 == 1 and (-1) or 1)
  473.                         if direction > 4 then direction = 1 elseif direction < 1 then direction = 4 end
  474.                         dirBumpX, dirBumpY = dirTemplate[direction][1], dirTemplate[direction][2]
  475.  
  476.                         drawFlooz(pipeType)
  477.                        
  478.                     -- Middle of a junction gives a bonus:
  479.                     else
  480.                         if pipeType > 8 then
  481.                             score = score + 500
  482.                             writeAt(zeroes(score, 7), 27, 18, colours.white, colours.grey)
  483.                         end
  484.  
  485.                         drawFlooz()
  486.                     end
  487.                    
  488.                 -- Jump to a new pipe tile:
  489.                 elseif subX < 1 or subX > 3 or subY < 1 or subY > 3 then
  490.                    
  491.                     if pipeType < 8 then grid[curY][curX] = (pipeType == 1) and (direction % 2 == 0 and 9 or 10) or 8 end
  492.                    
  493.                     if pipeType ~= 8 then
  494.                         if distance > 0 then distance, score = distance - 1, score + (fast and 100 or 50) else score = score + (fast and 200 or 100) end
  495.                         writeAt(zeroes(score, 7), 27, 18, colours.white, colours.grey)
  496.                         writeAt(zeroes(distance, 2), 41, 18, colours.white, colours.grey)
  497.                     end
  498.                    
  499.                     -- Move success:
  500.                     if checkMovement() then
  501.                         drawFlooz()
  502.                    
  503.                     -- Flooz spilled!
  504.                     else
  505.                         -- Explode unused pipe:
  506.                         for y = 1, 7 do for x = 1, 10 do
  507.                             local thisPipe = grid[y][x]
  508.                             if thisPipe and thisPipe < 8 then
  509.                                 for i = 1, 3 do
  510.                                     drawTile(img[8 + i], (x - 1) * 6 + 23, (y - 1) * 6 + 7)
  511.                                     sleep(0.1)
  512.                                 end
  513.                                
  514.                                 drawTile(img[8], (x - 1) * 6 + 23, (y - 1) * 6 + 7)
  515.                                 score = score > 99 and (score - 100) or 0
  516.                                 writeAt(zeroes(score, 7), 27, 18, colours.white, colours.grey)
  517.                             end
  518.                         end end
  519.                        
  520.                         -- Next level:
  521.                         if distance == 0 then
  522.                             writeAt(" LEVEL COMPLETE ", 18, 9, colours.yellow, colours.black)
  523.                             if not pressAnyKey() then break end
  524.                             level = level + 1
  525.                             newLevel()
  526.                            
  527.                         -- Game Over!
  528.                         else
  529.                             writeAt(" GAME OVER ", 21, 9, colours.red, colours.black)
  530.                             if not (pressAnyKey() and scoreboard(level, score)) then break end
  531.                             level, score = 1, 0
  532.                             newLevel()
  533.                         end
  534.                     end
  535.                    
  536.                 -- Regular mid-pipe movement:
  537.                 else drawFlooz() end
  538.                
  539.                 floozTimer = os.startTimer(fast and 0.1 or 1)
  540.             end
  541.         end
  542.    
  543.     -- Bail:
  544.     elseif (clickedAt(xSize, 1) or pressedKey(keys.x, keys.q) or myEvent[1] == "terminate") and not kiosk then
  545.         if myEvent[1] == "key" then os.pullEvent("char") end
  546.         break
  547.     end
  548. end
  549.  
  550. term.setBackgroundColour(colours.black)
  551. term.clear()
  552. writeAt("Thanks for playing Pipe Mania!", 1 , 1, colours.white)
  553. term.setCursorPos(1, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement