Advertisement
TriiNoxYs

[ComputerCraft] Tetris

Jul 6th, 2014
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.90 KB | None | 0 0
  1. --[ Texts & Frame ]--
  2. function setColor(text,backg)
  3.     term.setTextColor(text)
  4.     term.setBackgroundColor(backg)
  5. end
  6. function debug(err,stop)
  7.     term.setCursorPos(30,19)
  8.     setColor(colors.white,colors.white)
  9.     term.write("                ")
  10.     term.setCursorPos(30,19)
  11.     setColor(colors.red,colors.black)
  12.     term.write(err)
  13.     while stop do
  14.         event = os.pullEvent()
  15.         if event == "key" then break end
  16.     end
  17. end
  18. function frame()
  19.     paintutils.drawPixel(4,19,colors.black)
  20.     for i =1,19 do
  21.         paintutils.drawPixel(i+4,19,colors.black)
  22.         for j=1,2 do
  23.             paintutils.drawPixel(j+1,i,colors.black)
  24.             paintutils.drawPixel(j+23,i,colors.black)
  25.         end
  26.     end
  27.     paintutils.drawPixel(4,19,colors.gray)
  28.     paintutils.drawPixel(5,19,colors.gray)
  29.     paintutils.drawPixel(6,19,colors.lightGray)
  30.     paintutils.drawPixel(7,19,colors.lightGray)
  31.     paintutils.drawPixel(8,19,colors.gray)
  32.     paintutils.drawPixel(9,19,colors.gray)
  33.     paintutils.drawPixel(10,19,colors.lightGray)
  34.     paintutils.drawPixel(11,19,colors.lightGray)
  35.     paintutils.drawPixel(12,19,colors.gray)
  36.     paintutils.drawPixel(13,19,colors.gray)
  37.     paintutils.drawPixel(14,19,colors.lightGray)
  38.     paintutils.drawPixel(15,19,colors.lightGray)
  39.     paintutils.drawPixel(16,19,colors.gray)
  40.     paintutils.drawPixel(17,19,colors.gray)
  41.     paintutils.drawPixel(18,19,colors.lightGray)
  42.     paintutils.drawPixel(19,19,colors.lightGray)
  43.     paintutils.drawPixel(20,19,colors.gray)
  44.     paintutils.drawPixel(21,19,colors.gray)
  45.     paintutils.drawPixel(22,19,colors.lightGray)
  46.     paintutils.drawPixel(23,19,colors.lightGray)
  47.  
  48.     term.setCursorPos(30,5)
  49.     setColor(colors.black,colors.white)
  50.     term.write("Score :")
  51.     term.setCursorPos(30,6)
  52.     term.write("Line :")
  53. end
  54. function setText(line,score)
  55.     setColor(colors.black,colors.white)
  56.         --Score
  57.     score = tostring(math.ceil(score))
  58.     local len = 9-string.len(score)
  59.     for i =1,len do
  60.         score = tostring("0" .. score)
  61.     end
  62.     term.setCursorPos(38,5)
  63.     term.write(score)
  64.         --Line
  65.     line = tostring(math.floor(line))
  66.     local len = 9-string.len(line)
  67.     for i=1,len do
  68.         line = tostring("0" .. line)
  69.     end
  70.     term.setCursorPos(38,6)
  71.     term.write(line)
  72. end
  73. function setGame()
  74.     term.clear()
  75.     local x,y = term.getSize()
  76.     for i=1,x do
  77.         for j=1,y do
  78.             paintutils.drawPixel(i,j,colors.white)
  79.         end
  80.     end
  81.   frame()
  82.   setText(0,0)
  83. end
  84.  
  85. --[ Game ]--
  86.           -- Variables
  87. well = {}
  88. for i = 1,8 do
  89.   well[i] = {}
  90.   for j = 1,18 do
  91.     well[i][j] = colors.white
  92.   end
  93. end
  94.  
  95. tetriminos = {{"I",{{{1,1},{2,1},{3,1},{4,1}},
  96.                     {{1,1},{1,2},{1,3},{1,4}},
  97.                     {{1,1},{2,1},{3,1},{4,1}},
  98.                     {{1,1},{1,2},{1,3},{1,4}}},{{4,1},{1,4},{4,1},{1,4}} ,colors.cyan},
  99.               {"J",{{{1,1},{1,2},{2,2},{3,2}},
  100.                     {{2,1},{1,1},{1,2},{1,3}},
  101.                     {{1,1},{2,1},{3,1},{3,2}},
  102.                     {{2,1},{2,2},{2,3},{1,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.blue},
  103.               {"L",{{{1,1},{1,2},{2,1},{3,1}},
  104.                     {{1,1},{2,1},{2,2},{2,3}},
  105.                     {{1,2},{2,2},{3,2},{3,1}},
  106.                     {{1,1},{1,2},{1,3},{2,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.orange},
  107.               {"O",{{{1,1},{2,1},{1,2},{2,2}},
  108.                     {{1,1},{2,1},{1,2},{2,2}},
  109.                     {{1,1},{2,1},{1,2},{2,2}},
  110.                     {{1,1},{2,1},{1,2},{2,2}}},{{2,2},{2,2},{2,2},{2,2}},colors.yellow},
  111.               {"S",{{{1,2},{2,2},{2,1},{3,1}},
  112.                     {{1,1},{1,2},{2,2},{2,3}},
  113.                     {{1,2},{2,2},{2,1},{3,1}},
  114.                     {{1,1},{1,2},{2,2},{2,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.green},
  115.               {"T",{{{1,1},{2,1},{3,1},{2,2}},
  116.                     {{2,1},{2,2},{1,2},{2,3}},
  117.                     {{1,2},{2,2},{3,2},{2,1}},
  118.                     {{1,1},{1,2},{2,2},{1,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.purple},
  119.               {"Z",{{{1,1},{2,1},{2,2},{3,2}},
  120.                     {{2,1},{2,2},{1,2},{1,3}},
  121.                     {{1,1},{2,1},{2,2},{3,2}},
  122.                     {{2,1},{2,2},{1,2},{1,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.red}}
  123.  
  124.             -- Render
  125. function clearWell()
  126.     for i = 1,10 do
  127.         well[i] = {}
  128.         for j = 1,18 do
  129.             well[i][j] = colors.white
  130.         end
  131.     end
  132. end
  133. function paintWell()
  134.     for i = 1,10 do
  135.         local x = i-1
  136.         for j = 1,18 do
  137.             --local y = j-1
  138.             paintutils.drawPixel(x+4+x,j,well[i][j])
  139.             paintutils.drawPixel(x+5+x,j,well[i][j])
  140.         end
  141.     end
  142. end
  143.  
  144. function checkWell(t,s,x,y)
  145.     local x = x-1
  146.     local y = y-1
  147.     for i =1,4 do
  148.         if not (well[x+tetriminos[t][2][s][i][1]][y+tetriminos[t][2][s][i][2]] == colors.white) then return true end
  149.     end
  150.     return false
  151. end
  152.  
  153. function updateWell(t,s,x,y)
  154.     for i=1,4 do
  155.         well[x+tetriminos[t][2][s][i][1]-1][y+tetriminos[t][2][s][i][2]-1] = tetriminos[t][4]
  156.     end
  157. end
  158.  
  159. function clearLine(y)
  160.     for i=1,10 do
  161.         well[i][y] = colors.white
  162.     end
  163.     paintWell()
  164.     os.sleep(0.1)
  165.     for i = y, 1,-1 do
  166.         for j = 1,10 do
  167.             well[j][i] = well[j][i-1]
  168.         end
  169.         if i == 2 then break end
  170.     end
  171. end
  172.  
  173. function checkLine()
  174.     for i = 1,10 do
  175.         if not (well[i][1] == colors.white) then return -1 end
  176.     end
  177.     local a =0
  178.     local line = true
  179.     local x = 0
  180.     local y = 0
  181.     for i = 1,17 do
  182.         y = 19-i
  183.         line = true
  184.         for j = 1,10 do
  185.             x = j
  186.             if well[x][y] == colors.white then line = false end
  187.         end
  188.         if line then
  189.             a = a+1
  190.             clearLine(y)
  191.             paintWell()
  192.             a = a+checkLine()
  193.             return a
  194.         end
  195.     end
  196.     return 0
  197. end
  198.  
  199. function paintTetriminos(t,s,x,y)
  200.     local color = tetriminos[t][4]
  201.     local x = x*2
  202.     local y = y-1
  203.     for i = 1,4 do
  204.         paintutils.drawPixel((x+tetriminos[t][2][s][i][1])+tetriminos[t][2][s][i][1],y+tetriminos[t][2][s][i][2],color)
  205.         paintutils.drawPixel((x+tetriminos[t][2][s][i][1])+tetriminos[t][2][s][i][1]+1,y+tetriminos[t][2][s][i][2],color)
  206.     end
  207. end
  208. function getTetriminosSize(t,s)
  209.     return tetriminos[t][3][s][1],tetriminos[t][3][s][2]
  210. end
  211.           -- Main
  212. function game()
  213.               -- Set
  214.     setGame()
  215.     clearWell()
  216.     paintWell()
  217.     math.randomseed( os.time() )
  218.     local tetris = math.random(1,7)
  219.     local state = 1
  220.     local tetriminosX = 5
  221.     local tetriminosY = 1
  222.     local time = os.clock()
  223.     local elasped = 0
  224.     local new = false
  225.     local space = false
  226.     local movedX = 0
  227.     local line = 0
  228.     local score = 0
  229.     local nd = false
  230.     local fps = 1/100
  231.     local wait = 1
  232.             -- Game
  233.     while true do
  234.         wait = 2.35*math.pow(line+5,-0.41)
  235.         movedX = 0
  236.         os.startTimer(fps)
  237.             -- Input
  238.         event, key = os.pullEvent()
  239.         if event == "key" then
  240.             os.pullEvent("timer")
  241.             if key == 203 then
  242.                 tetriminosX = tetriminosX -1
  243.                 movedX = -1
  244.             elseif key == 205 then
  245.                 tetriminosX = tetriminosX +1
  246.                 movedX = 1
  247.             elseif key == 200 then state = state +1
  248.             elseif key == 208 then state = state -1
  249.             elseif key == 82 or key == 157 then tetriminosY = tetriminosY +1
  250.             elseif key == 57 then space = true
  251.             --elseif key == 76 then line = line +1
  252.             elseif key == 14 then break end
  253.         end
  254.             -- Core
  255.         if state < 1 then state=4
  256.         elseif state > 4 then state=1 end
  257.         local w,h = getTetriminosSize(tetris,state)
  258.         if tetriminosX < 1 then tetriminosX = 1
  259.         elseif tetriminosX > 11-w then tetriminosX = 11-w end
  260.  
  261.         elasped = os.clock() - time
  262.         if elasped>wait then
  263.             time = os.clock()
  264.             tetriminosY = tetriminosY +1
  265.         end
  266.         repeat
  267.         if space then tetriminosY = tetriminosY +1 end
  268.         if tetriminosY > 19-h then
  269.             tetriminosY = 19-h
  270.             new = true
  271.         elseif checkWell(tetris,state,tetriminosX,tetriminosY) then
  272.             if not (movedX == 0) then
  273.                 tetriminosX = tetriminosX-movedX
  274.                 movedX = 0
  275.             else
  276.                 tetriminosY = tetriminosY-1
  277.                 new = true
  278.             end
  279.         end
  280.         if new then
  281.             updateWell(tetris,state,tetriminosX,tetriminosY)
  282.             local check = checkLine()
  283.             if check == -1 then nd = true
  284.             elseif check>0 then
  285.                 line = line+check
  286.                 score = score + (10*((check+1)*(check+1))-2*(check+1))
  287.             end
  288.             tetris = math.random(1,7)
  289.             state = 1
  290.             tetriminosX = 5
  291.             tetriminosY = 1
  292.             new = false
  293.             space = false
  294.             time = os.clock()
  295.         end
  296.         until space == false
  297.         if nd then break end
  298.             -- Render
  299.         setText(line,score)
  300.         paintWell()
  301.         paintTetriminos(tetris,state,tetriminosX,tetriminosY)
  302.     end
  303.     setColor(colors.black,colors.white)
  304.     term.setCursorPos(35,12)
  305.     term.write("Again ?")
  306.     setColor(colors.green,colors.white)
  307.     term.setCursorPos(27,13)
  308.     term.write("Yes= Enter")
  309.     setColor(colors.black,colors.white)
  310.     term.write("/")
  311.     setColor(colors.red,colors.white)
  312.     term.write("No= Backspace")
  313.     while true do
  314.         event, key = os.pullEvent()
  315.         if event == "key" then
  316.             if key == 28 then return true,score
  317.             elseif key == 14 then return false,score end
  318.         end
  319.     end
  320. end
  321.  
  322. --[ Menu ]--
  323.           -- Variables
  324. local menu = {"Start","Leaderboard","Options"}
  325.  
  326.           -- Functions
  327. function setMenu()
  328.     term.clear()
  329.     local x,y = term.getSize()
  330.     for i=1,x do
  331.         for j=1,y do
  332.             paintutils.drawPixel(i,j,colors.white)
  333.         end
  334.     end
  335.     local w,h = term.getSize()
  336.  
  337.     setColor(colors.lightBlue,colors.white)
  338.     term.setCursorPos(1,h/2 - #menu/2-1)
  339.     for i = 1,w do
  340.         term.write("_")
  341.     end
  342.     term.setCursorPos(1,h/2 + #menu/2 + 1)
  343.     for i = 1,w do
  344.         term.write("_")
  345.     end
  346.     term.setCursorPos(w/2-5,h/2 - #menu/2-1)
  347.     term.write("__ Menu __")
  348. end
  349.  
  350. function drawMenu(l)
  351.     if l < 0 or l > #menu then return false end
  352.     local w,h = term.getSize()
  353.     for i = 1,#menu do
  354.         setColor(colors.black,colors.white)
  355.         local x = w/2 - string.len(menu[i])/2
  356.         local y = h/2 - #menu/2 + i
  357.         term.setCursorPos(1,y)
  358.         term.clearLine()
  359.         if l==i then
  360.             setColor(colors.lightBlue,colors.white)
  361.             term.setCursorPos(x-2,y)
  362.             term.write(">")
  363.             term.setCursorPos(x+string.len(menu[i])+1,y)
  364.             term.write("<")
  365.             setColor(colors.lime,colors.white)
  366.         end
  367.         term.setCursorPos(x,y)
  368.         term.write(menu[i])
  369.     end
  370.     return true
  371. end
  372. function info(m)
  373.     term.setCursorPos(term.getSize()-string.len(m),19)
  374.     term.clearLine()
  375.     setColor(colors.green,colors.white)
  376.     term.write(m)
  377. end
  378.  
  379. --[ Leaderboard ]--
  380. function getLeaderboard()
  381.     f = fs.open("score","r")
  382.     score = f.readLine()
  383.     f.close()
  384.     return textutils.unserialize(score)
  385. end
  386. function setLeaderboard()
  387.     while not (fs.exists("score")) do
  388.         setColor(colors.green,colors.white)
  389.         term.clear()
  390.         term.setCursorPos(1,1)
  391.         term.write("First start / Premier lancement")
  392.         setColor(colors.red,colors.white)
  393.         term.setCursorPos(1,2)
  394.         term.write("We are going to create the file 'score',")
  395.         term.setCursorPos(1,3)
  396.         term.write("yours scores will be save in it")
  397.         term.setCursorPos(1,4)
  398.         term.write("------------------------------------------------------------------------")
  399.         term.setCursorPos(1,5)
  400.         term.write("Nous allons cr�83�A9er le fichier 'score',")
  401.         term.setCursorPos(1,6)
  402.         term.write("vos scores y seront sauvegard�83�A9s")
  403.         term.setCursorPos(1,11)
  404.         term.write("Save then Exit (Ctrl). Thanks You")
  405.         term.setCursorPos(1,12)
  406.         term.write("---------------------------------------------------------------")
  407.         term.setCursorPos(1,13)
  408.         term.write("Sauvergardez puis quittez (Ctrl). Merci")
  409.         os.pullEvent("key")
  410.         shell.run("edit score")
  411.     end
  412.     if fs.exists("score") then
  413.         local s = getLeaderboard()
  414.         if s then
  415.             if #s == 10 then return true end
  416.         end
  417.     end
  418.  
  419.     f = fs.open("score","w")
  420.     local empty = {9999999,999999,99999,9999,999,321,123,99,42,36}
  421.     f.writeLine(textutils.serialize(empty))
  422.     f.close()
  423.     return true
  424. end
  425. function addToLeaderboard(s)
  426.     local score = getLeaderboard()
  427.     for i=1,#score do
  428.         if s >= score[i] then
  429.             for j=(#score-1),i,-1 do
  430.                 score[j+1] = score[j]
  431.             end
  432.             score[i] = s
  433.             break
  434.         end
  435.     end
  436.     f = fs.open("score","w")
  437.     f.writeLine(textutils.serialize(score))
  438.     f.close()
  439. end
  440. function showLeaderboard()
  441.     term.clear()
  442.     local w,h = term.getSize()
  443.  
  444.     setColor(colors.lightBlue,colors.white)
  445.     term.setCursorPos(1,3)
  446.     for i = 1,w do
  447.         term.write("_")
  448.     end
  449.     term.setCursorPos(1,15)
  450.     for i = 1,w do
  451.         term.write("_")
  452.     end
  453.     term.setCursorPos(w/2-8,3)
  454.     term.write("__ Leaderboard __")
  455.  
  456.     setColor(colors.black,colors.white)
  457.     local s = getLeaderboard()
  458.     for i=1,#s do
  459.         term.setCursorPos(w/2 - string.len(tostring(s[i]))/2,4+i)
  460.         term.write(tostring(math.ceil(s[i])))
  461.     end
  462.     event = os.pullEvent("key")
  463. end
  464.  
  465. --[ Main ]--
  466. setLeaderboard()
  467. setMenu()
  468. local selected = 1
  469. while true do
  470.     drawMenu(selected)
  471.     os.startTimer(0.1)
  472.     event,key = os.pullEvent()
  473.     if event == "key" then
  474.         os.pullEvent()
  475.         if key == 200 then selected = selected -1
  476.         elseif key == 208 then selected = selected +1
  477.         elseif key == 14 then break
  478.         elseif key == 28 then
  479.             if selected == 1 then
  480.                 while true do
  481.                     again,s = game()
  482.                     addToLeaderboard(s)
  483.                     if not again then break end
  484.                 end
  485.             elseif selected == 2 then
  486.                 showLeaderboard()
  487.             end
  488.             setMenu()
  489.         end
  490.     end
  491.     if selected < 1 then selected = 1
  492.     elseif selected > #menu then selected = #menu end
  493.     if selected == 1 then info("Let's fall in line")
  494.     elseif selected == 3 then info("Press 'Backspace' To Exit")
  495.     else info("Try to reach the top !") end
  496. end
  497.  
  498.           -- Clear & End
  499. term.setBackgroundColor(colors.black)
  500. term.clear()
  501. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement