Advertisement
Guest User

startup

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