Advertisement
hunters2020

Tiles!

May 30th, 2014
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.49 KB | None | 0 0
  1. --[[
  2.     "Tiles!" By Detective_Smith
  3.     A small CC Game I worked up in a hour or so.
  4.     Enjoy the game! Dont cheat! :P
  5.     Game Version 1.2
  6. ]]--
  7.  
  8. local sx, sy = term.getSize()
  9. local tileWidth = 4 --math.ceil(sx / 13)
  10. local tileHeight = 3 --math.floor(sy / 6)
  11. local cHighScore = 0
  12. local zHighScore = 0
  13. local rHighScore = 0
  14. local score = 0
  15. local ox = math.ceil((sx / 4) + tileWidth)
  16. local oex = sx - ox
  17. local timer = 0.1
  18. local zenTime = 30
  19. local rushTime = 10
  20. local gameOver = false
  21. local showControls = false
  22. local tileColor = colors.black
  23. local bgColor = colors.white
  24. local menuColor = colors.black
  25. local missColor = colors.white
  26. local textColor = colors.white
  27. local subTextColor = colors.white
  28. local buttons = {}
  29.  
  30. if pocket then
  31.     ox = 5
  32.     oex = sx - ox + 1
  33. end
  34.  
  35. if term.isColor() then
  36.     menuColor = colors.gray
  37.     missColor = colors.red
  38.     textColor = colors.yellow
  39. end
  40.  
  41. function saveScore()
  42.     if fs.exists(".tilescore") then
  43.         fs.delete(".tilescore")
  44.     end
  45.  
  46.     local file = fs.open(".tilescore", "w")
  47.     file.writeLine(cHighScore)
  48.     file.writeLine(zHighScore)
  49.     file.writeLine(rHighScore)
  50.     file.close()
  51. end
  52.  
  53. function loadScore()
  54.     if fs.exists(".tilescore") then
  55.         local file = fs.open(".tilescore", "r")
  56.         cHighScore = tonumber(file.readLine())
  57.         zHighScore = tonumber(file.readLine())
  58.         rHighScore = tonumber(file.readLine())
  59.         file.close()
  60.     end
  61. end
  62.  
  63. function drawSquare(x, y, c)
  64.     for i = 1, tileHeight do
  65.         local dX, dY = ox - 3 + (x * tileWidth), sy - (y * 3) + i
  66.         paintutils.drawLine(dX, dY, dX + tileWidth - 1, dY, c)
  67.     end
  68. end
  69.  
  70. function newTileTable(n)
  71.     tiles = {}
  72.     tiles[9000] = {}
  73.     if n then
  74.         tiles[9000].A = n
  75.         tiles[9000].L = true
  76.     else
  77.         tiles[9000].A = sy / tileHeight
  78.         tiles[9000].L = false
  79.     end
  80.     for i = 1, tiles[9000].A do
  81.         if i ~= tiles[9000].A then
  82.             tiles[i] = {}
  83.             tiles[i].X = math.random(4)
  84.             tiles[i].Y = i
  85.             tiles[i].C = tileColor
  86.         else
  87.             tiles[i] = {}
  88.             tiles[i + 1] = {}
  89.             tiles[i + 2] = {}
  90.             tiles[i + 3] = {}
  91.             tiles[i].X = 1
  92.             tiles[i + 1].X = 2
  93.             tiles[i + 2].X = 3
  94.             tiles[i + 3].X = 4
  95.             tiles[i].Y = i
  96.             tiles[i + 1].Y = i
  97.             tiles[i + 2].Y = i
  98.             tiles[i + 3].Y = i
  99.             tiles[i].C = tileColor
  100.             tiles[i + 1].C = tileColor
  101.             tiles[i + 2].C = tileColor
  102.             tiles[i + 3].C = tileColor
  103.         end
  104.     end
  105. end
  106.  
  107. function newTile(n)
  108.     table.remove(tiles, n)
  109.     for i = n, #tiles do
  110.         tiles[i].Y = tiles[i].Y - 1
  111.     end
  112.     if tiles[9000].L then
  113.         tiles[9000].A = tiles[9000].A - 1
  114.     end
  115.     if not tiles[9000].L then
  116.         local newNum = #tiles + 1
  117.         local nX = math.random(4)
  118.         local nY = tiles[newNum - 1].Y + 1
  119.         tiles[newNum] = {}
  120.         tiles[newNum].X = nX
  121.         tiles[newNum].Y = nY
  122.         tiles[newNum].C = tileColor
  123.     end
  124. end
  125.  
  126. function checkForTile(x, y, bool)
  127.     for i = 1, #tiles do
  128.         if tiles[i].X == x then
  129.             if tiles[i].Y == y then
  130.                 newTile(y)
  131.                 if bool then
  132.                     score = score + 1
  133.                     if score > zHighScore then
  134.                         zHighScore = score
  135.                     end
  136.                     if score > rHighScore then
  137.                         rHighScore = score
  138.                     end
  139.                 end
  140.                 if tiles[9000].L then
  141.                     return true, tiles[9000].A
  142.                 else
  143.                     return true
  144.                 end
  145.             end
  146.         end
  147.     end
  148. end
  149.  
  150. function round(num, idp)
  151.   local mult = 10^(idp or 0)
  152.   return math.floor(num * mult + 0.5) / mult
  153. end
  154.  
  155. function overlay(highScore, str, bool)
  156.     if bool then
  157.         term.clear()
  158.         drawSquares()
  159.         for i = 1, sy do
  160.             paintutils.drawLine(1, i, ox, i, menuColor)
  161.             paintutils.drawLine(oex, i, sx, i, menuColor)
  162.         end
  163.         paintutils.drawPixel(sx, 1, missColor)
  164.         term.setTextColor(subTextColor)
  165.         term.setCursorPos(sx, 1)
  166.         term.write("X")
  167.         paintutils.drawLine(ox + 1, 1, oex - 1, 1, menuColor)
  168.     end
  169.     term.setBackgroundColor(colors.gray)
  170.     term.setCursorPos(ox + 1, 1)
  171.     term.setTextColor(textColor)
  172.     print(str.. "    ")
  173.     term.setCursorPos(1, 2)
  174.     if not pocket then
  175.         term.write("SCORE: ")
  176.         term.setTextColor(subTextColor)
  177.         print(score)
  178.         term.setTextColor(textColor)
  179.         term.write("HIGH SCORE: ")
  180.         term.setTextColor(subTextColor)
  181.         print(highScore)
  182.         if showControls then
  183.             term.setCursorPos(1,5)
  184.             term.setTextColor(textColor)
  185.             print("CONTROLS:")
  186.             term.setTextColor(subTextColor)
  187.             print("CLICK ON A BLACK")
  188.             print("TILE TO REMOVE IT")
  189.             print("OR USE A, S, D, F")
  190.             print("KEYS TO REMOVE")
  191.             print("TILES. DONT CLICK")
  192.             print("THE WHITE ONES!")
  193.             print("PRESS H TO HIDE")
  194.             print("THIS MESSAGE.")
  195.         end
  196.     else
  197.         print("SCORE")
  198.         term.setTextColor(subTextColor)
  199.         print(score)
  200.         print("")
  201.         term.setTextColor(textColor)
  202.         print("HIGH")
  203.         print("SCORE")
  204.         term.setTextColor(subTextColor)
  205.         print(highScore)
  206.         if showControls then
  207.             print("")
  208.             term.setTextColor(textColor)
  209.             print("HOWTO")
  210.             term.setTextColor(subTextColor)
  211.             print("CLICK")
  212.             print("ONLY")
  213.             print("ON")
  214.             print("BLACK")
  215.             print("TILES")
  216.             print("OR")
  217.             print("USE")
  218.             print("A, S")
  219.             print("D, F")
  220.             print("KEYS")
  221.         end
  222.     end
  223. end
  224.  
  225. function drawSquares()
  226.     for i = 1, oex - ox - 1 do
  227.         paintutils.drawLine(ox + i, 2, ox + i, sy, bgColor)
  228.     end
  229.     for i = 1, sy / tileHeight + 4 do
  230.         if tiles[i] then
  231.             if tiles[i].X and tiles[i].Y and tiles[i].C then
  232.                 drawSquare(tiles[i].X, tiles[i].Y, tiles[i].C)
  233.             end
  234.         end
  235.     end
  236. end
  237.  
  238. function drawScreen(hS, str, b, b2)
  239.     if b2 then
  240.         drawSquares()
  241.         drawSquare(tiles[#tiles].X, tiles[#tiles].Y, tiles[#tiles].C)
  242.     end
  243.     overlay(hS, str, b)
  244. end
  245.  
  246. function classicMode()
  247.     local exit = false
  248.     newTileTable(30)
  249.     totalTime = 0
  250.     local tLeft = 30
  251.     drawScreen(cHighScore, "TILES LEFT: " ..tLeft, true)
  252.  
  253.     function gameReset(x, y, bool)
  254.         local newNum = #tiles + 1
  255.         tiles[newNum] = {}
  256.         tiles[newNum].X = x
  257.         tiles[newNum].Y = y
  258.         tiles[newNum].C = missColor
  259.         if bool then
  260.             if cHighScore <= 0 then
  261.                 cHighScore = score
  262.             end
  263.             if score <= cHighScore then
  264.                 cHighScore = score
  265.             end
  266.         end
  267.         tLeft = tLeft or "LOST"
  268.         drawScreen(cHighScore, "TILES LEFT: " ..tLeft, false, true)
  269.         saveScore()
  270.         sleep(1)
  271.         score = 0
  272.         newTileTable(30)
  273.         totalTime = 0
  274.         tLeft = 30
  275.         drawScreen(cHighScore, "TILES LEFT: " ..tLeft, true, true)
  276.     end
  277.  
  278.     while true do
  279.         e, p1, p2, p3 = os.pullEventRaw()
  280.         if e == "char" then
  281.             if p1 == "a" or p1 == "s" or p1 == "d" or p1 == "f" then
  282.                 cTimer = os.startTimer(timer)
  283.             end
  284.             if p1 == "a" then
  285.                 bool, tLeft = checkForTile(1, 1)
  286.                 if not bool then gameReset(1,1) end
  287.             elseif p1 == "s" then
  288.                 bool, tLeft = checkForTile(2, 1)
  289.                 if not bool then gameReset(2,1) end
  290.             elseif p1 == "d" then
  291.                 bool, tLeft = checkForTile(3, 1)
  292.                 if not bool then gameReset(3,1) end
  293.             elseif p1 == "f" then
  294.                 bool, tLeft = checkForTile(4, 1)
  295.                 if not bool then gameReset(4,1) end
  296.             elseif p1 == "h" then
  297.                 showControls = not showControls
  298.                 drawScreen(cHighScore, "TILES LEFT: " ..tLeft, true)
  299.             end
  300.             drawScreen(cHighScore, "TILES LEFT: " ..tLeft, false, true)
  301.         elseif e == "mouse_click" then
  302.             if p2 >= ox + 1 and p2 <= oex -1 and p3 ~= 1 then
  303.                 cTimer = os.startTimer(timer)
  304.                 local cx = math.ceil((p2 - ox) / tileWidth)
  305.                 local cy = math.ceil((p3 - 1) / tileHeight)
  306.                 local cy = -cy + 7
  307.                 bool, tLeft = checkForTile(cx, cy)
  308.                 if not bool then gameReset(cx, cy) end
  309.             elseif p2 == sx and p3 == 1 then
  310.                 exit = true
  311.             end
  312.             if exit then
  313.                 score = 0
  314.                 totalTime = 0
  315.                 saveScore()
  316.                 break
  317.             end
  318.             drawScreen(cHighScore, "TILES LEFT: " ..tLeft, false, true)
  319.         elseif e == "timer" then
  320.             if p1 == cTimer then
  321.                 cTimer = os.startTimer(timer)
  322.                 totalTime = totalTime + 0.1
  323.                 score = round(totalTime,3)
  324.             end
  325.             drawScreen(cHighScore, "TILES LEFT: " ..tLeft)
  326.         elseif e == "terminate" then
  327.             gameOver = true
  328.             break
  329.         end
  330.         if tLeft then
  331.             if tLeft <= 0 then
  332.                 gameReset(sx, sy, true)
  333.             end
  334.         end
  335.     end
  336. end
  337.  
  338. function zenMode()
  339.     local exit = false
  340.     newTileTable()
  341.     totalTime = zenTime
  342.     drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3), true)
  343.  
  344.     function gameReset(x, y)
  345.         local newNum = #tiles + 1
  346.         tiles[newNum] = {}
  347.         tiles[newNum].X = x
  348.         tiles[newNum].Y = y
  349.         tiles[newNum].C = missColor
  350.         drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3), false, true)
  351.         saveScore()
  352.         sleep(1)
  353.         score = 0
  354.         newTileTable()
  355.         totalTime = zenTime
  356.         drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3), true, true)
  357.     end
  358.  
  359.     while true do
  360.         e, p1, p2, p3 = os.pullEventRaw()
  361.         if e == "char" then
  362.             if p1 == "a" or p1 == "s" or p1 == "d" or p1 == "f" then
  363.                 zTimer = os.startTimer(timer)
  364.             end
  365.             if p1 == "a" then
  366.                 local bool = checkForTile(1, 1, true)
  367.                 if not bool then gameReset(1,1) end
  368.             elseif p1 == "s" then
  369.                 local bool = checkForTile(2, 1, true)
  370.                 if not bool then gameReset(2,1) end
  371.             elseif p1 == "d" then
  372.                 local bool = checkForTile(3, 1, true)
  373.                 if not bool then gameReset(3,1) end
  374.             elseif p1 == "f" then
  375.                 local bool = checkForTile(4, 1, true)
  376.                 if not bool then gameReset(4,1) end
  377.             elseif p1 == "h" then
  378.                 showControls = not showControls
  379.                 drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3), true)
  380.             end
  381.             drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3), false, true)
  382.         elseif e == "mouse_click" then
  383.             if p2 >= ox + 1 and p2 <= oex -1 and p3 ~= 1 then
  384.                 zTimer = os.startTimer(timer)
  385.                 local cx = math.ceil((p2 - ox) / tileWidth)
  386.                 local cy = math.ceil((p3 - 1) / tileHeight)
  387.                 local cy = -cy + 7
  388.                 local bool = checkForTile(cx, cy, true)
  389.                 if not bool then gameReset(cx,cy) end
  390.             elseif p2 == sx and p3 == 1 then
  391.                 exit = true
  392.             end
  393.             if exit then
  394.                 score = 0
  395.                 totalTime = zenTime
  396.                 saveScore()
  397.                 break
  398.             end
  399.             drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3), false, true)
  400.         elseif e == "timer" then
  401.             if p1 == zTimer then
  402.                 zTimer = os.startTimer(timer)
  403.                 totalTime = totalTime - 0.1
  404.                 if totalTime <= 0 then
  405.                     gameReset(sx, sy)
  406.                 end
  407.             end
  408.             drawScreen(zHighScore, "TIME LEFT: " ..round(totalTime,3))
  409.         elseif e == "terminate" then
  410.             gameOver = true
  411.             break
  412.         end
  413.     end
  414. end
  415.  
  416. function rushMode()
  417.     local exit = false
  418.     local totalTime = rushTime
  419.     local tilesNeeded = 30
  420.     local level = 0
  421.     local totalTilesNeeded = tilesNeeded + level
  422.     newTileTable()
  423.     drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3), true)
  424.  
  425.     function gameReset(x, y)
  426.         local newNum = #tiles + 1
  427.         tiles[newNum] = {}
  428.         tiles[newNum].X = x
  429.         tiles[newNum].Y = y
  430.         tiles[newNum].C = missColor
  431.         drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3), false, true)
  432.         saveScore()
  433.         sleep(1)
  434.         score = 0
  435.         newTileTable()
  436.         totalTime = rushTime
  437.         level = 0
  438.         totalTilesNeeded = tilesNeeded + level
  439.         drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3), true, true)
  440.     end
  441.  
  442.     while true do
  443.         e, p1, p2, p3 = os.pullEventRaw()
  444.         if e == "char" then
  445.             if p1 == "a" or p1 == "s" or p1 == "d" or p1 == "f" then
  446.                 rTimer = os.startTimer(timer)
  447.             end
  448.             if p1 == "a" then
  449.                 local bool = checkForTile(1, 1, true)
  450.                 if not bool then gameReset(1,1) end
  451.             elseif p1 == "s" then
  452.                 local bool = checkForTile(2, 1, true)
  453.                 if not bool then gameReset(2,1) end
  454.             elseif p1 == "d" then
  455.                 local bool = checkForTile(3, 1, true)
  456.                 if not bool then gameReset(3,1) end
  457.             elseif p1 == "f" then
  458.                 local bool = checkForTile(4, 1, true)
  459.                 if not bool then gameReset(4,1) end
  460.             elseif p1 == "h" then
  461.                 showControls = not showControls
  462.                 drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3), true)
  463.             end
  464.             totalTilesNeeded = totalTilesNeeded - 1
  465.             if totalTilesNeeded <= 0 then
  466.                 level = level + 1
  467.                 totalTilesNeeded = tilesNeeded + level
  468.                 totalTime = totalTime + 10
  469.             end
  470.             drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3), false, true)
  471.         elseif e == "mouse_click" then
  472.             if p2 >= ox + 1 and p2 <= oex -1 and p3 ~= 1 then
  473.                 zTimer = os.startTimer(timer)
  474.                 local cx = math.ceil((p2 - ox) / tileWidth)
  475.                 local cy = math.ceil((p3 - 1) / tileHeight)
  476.                 local cy = -cy + 7
  477.                 local bool = checkForTile(cx, cy, true)
  478.                 if not bool then gameReset(cx,cy) end
  479.             elseif p2 == sx and p3 == 1 then
  480.                 exit = true
  481.             end
  482.             if exit then
  483.                 score = 0
  484.                 totalTime = rushTime
  485.                 saveScore()
  486.                 break
  487.             end
  488.             drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3), false, true)
  489.         elseif e == "timer" then
  490.             if p1 == rTimer then
  491.                 rTimer = os.startTimer(timer)
  492.                 totalTime = totalTime - 0.1
  493.                 if totalTime <= 0 then
  494.                     gameReset(sx, sy)
  495.                 end
  496.             end
  497.             drawScreen(rHighScore, "TIME LEFT: " ..round(totalTime,3))
  498.         elseif e == "terminate" then
  499.             gameOver = true
  500.             break
  501.         end
  502.     end
  503. end
  504.  
  505. function exitGame()
  506.     saveScore()
  507.     term.setBackgroundColor(colors.black)
  508.     term.setTextColor(colors.white)
  509.     term.clear()
  510.     term.setCursorPos(1,1)
  511.     print("Thanks for playing 'Tiles!'. Your score was saved.")
  512. end
  513.  
  514. function newButton(x,y,str,bColor,tColor,func)
  515.     local w = #str + 1
  516.     local newNumber = #buttons + 1
  517.     buttons[newNumber] = {}
  518.     buttons[newNumber].X = x
  519.     buttons[newNumber].Y = y
  520.     buttons[newNumber].eX = x + w
  521.     buttons[newNumber].eY = y + 3
  522.     buttons[newNumber].W = w
  523.     buttons[newNumber].S = str
  524.     buttons[newNumber].bC = bColor
  525.     buttons[newNumber].tC = tColor
  526.     buttons[newNumber].F = func
  527. end
  528.  
  529. function checkButton(x,y)
  530.     for i = 1, #buttons do
  531.         if x >= buttons[i].X and x <= buttons[i].eX and y >= buttons[i].Y and y <= buttons[i].eY then
  532.             local func = buttons[i].F
  533.             term.setTextColor(colors.white)
  534.             func()
  535.             return true
  536.         end
  537.     end
  538. end
  539.  
  540. function drawMenu()
  541.     term.setBackgroundColor(menuColor)
  542.     term.clear()
  543.     term.setCursorPos((sx / 2) - 3,3)
  544.     term.setTextColor(textColor)
  545.     print("Tiles!")
  546.     for i = 1, #buttons do
  547.         for j = 1, 3 do
  548.             paintutils.drawLine(buttons[i].X, (buttons[i].Y - 1) + j, buttons[i].X + buttons[i].W, (buttons[i].Y - 1) + j, buttons[i].bC)
  549.         end
  550.         term.setCursorPos(buttons[i].X + 1, buttons[i].Y + 1)
  551.         term.setTextColor(buttons[i].tC)
  552.         print(buttons[i].S)
  553.     end
  554.     paintutils.drawPixel(sx, 1, missColor)
  555.     term.setTextColor(subTextColor)
  556.     term.setCursorPos(sx, 1)
  557.     term.write("X")
  558. end
  559.  
  560. function mainMenu()
  561.     newButton((sx/2) - 10, 5, "Classic", colors.white, colors.black, classicMode)
  562.     newButton((sx/2) + 2, 5, "Zen Mode", colors.white, colors.black, zenMode)
  563.     newButton((sx/2) - 10, 9, "Rush Mode", colors.white, colors.black, rushMode)
  564.     drawMenu()
  565.     while true do
  566.         if gameOver then
  567.             exitGame()
  568.             break
  569.         end
  570.         e, p1, p2, p3 = os.pullEvent()
  571.         if p2 and p3 then
  572.             local inGame = checkButton(p2,p3)
  573.             if not inGame then
  574.                 if p2 == sx and p3 == 1 then
  575.                     exitGame()
  576.                     break
  577.                 end
  578.             end
  579.         end
  580.         drawMenu()
  581.     end
  582. end
  583.  
  584. loadScore()
  585. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement