Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[ Texts & Frame ]--
- function setColor(text,backg)
- term.setTextColor(text)
- term.setBackgroundColor(backg)
- end
- function debug(err,stop)
- term.setCursorPos(30,19)
- setColor(colors.white,colors.white)
- term.write(" ")
- term.setCursorPos(30,19)
- setColor(colors.red,colors.black)
- term.write(err)
- while stop do
- event = os.pullEvent()
- if event == "key" then break end
- end
- end
- function frame()
- paintutils.drawPixel(4,19,colors.black)
- for i =1,19 do
- paintutils.drawPixel(i+4,19,colors.black)
- for j=1,2 do
- paintutils.drawPixel(j+1,i,colors.black)
- paintutils.drawPixel(j+23,i,colors.black)
- end
- end
- paintutils.drawPixel(4,19,colors.gray)
- paintutils.drawPixel(5,19,colors.gray)
- paintutils.drawPixel(6,19,colors.lightGray)
- paintutils.drawPixel(7,19,colors.lightGray)
- paintutils.drawPixel(8,19,colors.gray)
- paintutils.drawPixel(9,19,colors.gray)
- paintutils.drawPixel(10,19,colors.lightGray)
- paintutils.drawPixel(11,19,colors.lightGray)
- paintutils.drawPixel(12,19,colors.gray)
- paintutils.drawPixel(13,19,colors.gray)
- paintutils.drawPixel(14,19,colors.lightGray)
- paintutils.drawPixel(15,19,colors.lightGray)
- paintutils.drawPixel(16,19,colors.gray)
- paintutils.drawPixel(17,19,colors.gray)
- paintutils.drawPixel(18,19,colors.lightGray)
- paintutils.drawPixel(19,19,colors.lightGray)
- paintutils.drawPixel(20,19,colors.gray)
- paintutils.drawPixel(21,19,colors.gray)
- paintutils.drawPixel(22,19,colors.lightGray)
- paintutils.drawPixel(23,19,colors.lightGray)
- term.setCursorPos(30,5)
- setColor(colors.black,colors.white)
- term.write("Score :")
- term.setCursorPos(30,6)
- term.write("Line :")
- end
- function setText(line,score)
- setColor(colors.black,colors.white)
- --Score
- score = tostring(math.ceil(score))
- local len = 9-string.len(score)
- for i =1,len do
- score = tostring("0" .. score)
- end
- term.setCursorPos(38,5)
- term.write(score)
- --Line
- line = tostring(math.floor(line))
- local len = 9-string.len(line)
- for i=1,len do
- line = tostring("0" .. line)
- end
- term.setCursorPos(38,6)
- term.write(line)
- end
- function setGame()
- term.clear()
- local x,y = term.getSize()
- for i=1,x do
- for j=1,y do
- paintutils.drawPixel(i,j,colors.white)
- end
- end
- frame()
- setText(0,0)
- end
- --[ Game ]--
- -- Variables
- well = {}
- for i = 1,8 do
- well[i] = {}
- for j = 1,18 do
- well[i][j] = colors.white
- end
- end
- tetriminos = {{"I",{{{1,1},{2,1},{3,1},{4,1}},
- {{1,1},{1,2},{1,3},{1,4}},
- {{1,1},{2,1},{3,1},{4,1}},
- {{1,1},{1,2},{1,3},{1,4}}},{{4,1},{1,4},{4,1},{1,4}} ,colors.cyan},
- {"J",{{{1,1},{1,2},{2,2},{3,2}},
- {{2,1},{1,1},{1,2},{1,3}},
- {{1,1},{2,1},{3,1},{3,2}},
- {{2,1},{2,2},{2,3},{1,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.blue},
- {"L",{{{1,1},{1,2},{2,1},{3,1}},
- {{1,1},{2,1},{2,2},{2,3}},
- {{1,2},{2,2},{3,2},{3,1}},
- {{1,1},{1,2},{1,3},{2,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.orange},
- {"O",{{{1,1},{2,1},{1,2},{2,2}},
- {{1,1},{2,1},{1,2},{2,2}},
- {{1,1},{2,1},{1,2},{2,2}},
- {{1,1},{2,1},{1,2},{2,2}}},{{2,2},{2,2},{2,2},{2,2}},colors.yellow},
- {"S",{{{1,2},{2,2},{2,1},{3,1}},
- {{1,1},{1,2},{2,2},{2,3}},
- {{1,2},{2,2},{2,1},{3,1}},
- {{1,1},{1,2},{2,2},{2,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.green},
- {"T",{{{1,1},{2,1},{3,1},{2,2}},
- {{2,1},{2,2},{1,2},{2,3}},
- {{1,2},{2,2},{3,2},{2,1}},
- {{1,1},{1,2},{2,2},{1,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.purple},
- {"Z",{{{1,1},{2,1},{2,2},{3,2}},
- {{2,1},{2,2},{1,2},{1,3}},
- {{1,1},{2,1},{2,2},{3,2}},
- {{2,1},{2,2},{1,2},{1,3}}},{{3,2},{2,3},{3,2},{2,3}},colors.red}}
- -- Render
- function clearWell()
- for i = 1,10 do
- well[i] = {}
- for j = 1,18 do
- well[i][j] = colors.white
- end
- end
- end
- function paintWell()
- for i = 1,10 do
- local x = i-1
- for j = 1,18 do
- --local y = j-1
- paintutils.drawPixel(x+4+x,j,well[i][j])
- paintutils.drawPixel(x+5+x,j,well[i][j])
- end
- end
- end
- function checkWell(t,s,x,y)
- local x = x-1
- local y = y-1
- for i =1,4 do
- if not (well[x+tetriminos[t][2][s][i][1]][y+tetriminos[t][2][s][i][2]] == colors.white) then return true end
- end
- return false
- end
- function updateWell(t,s,x,y)
- for i=1,4 do
- well[x+tetriminos[t][2][s][i][1]-1][y+tetriminos[t][2][s][i][2]-1] = tetriminos[t][4]
- end
- end
- function clearLine(y)
- for i=1,10 do
- well[i][y] = colors.white
- end
- paintWell()
- os.sleep(0.1)
- for i = y, 1,-1 do
- for j = 1,10 do
- well[j][i] = well[j][i-1]
- end
- if i == 2 then break end
- end
- end
- function checkLine()
- for i = 1,10 do
- if not (well[i][1] == colors.white) then return -1 end
- end
- local a =0
- local line = true
- local x = 0
- local y = 0
- for i = 1,17 do
- y = 19-i
- line = true
- for j = 1,10 do
- x = j
- if well[x][y] == colors.white then line = false end
- end
- if line then
- a = a+1
- clearLine(y)
- paintWell()
- a = a+checkLine()
- return a
- end
- end
- return 0
- end
- function paintTetriminos(t,s,x,y)
- local color = tetriminos[t][4]
- local x = x*2
- local y = y-1
- for i = 1,4 do
- paintutils.drawPixel((x+tetriminos[t][2][s][i][1])+tetriminos[t][2][s][i][1],y+tetriminos[t][2][s][i][2],color)
- paintutils.drawPixel((x+tetriminos[t][2][s][i][1])+tetriminos[t][2][s][i][1]+1,y+tetriminos[t][2][s][i][2],color)
- end
- end
- function getTetriminosSize(t,s)
- return tetriminos[t][3][s][1],tetriminos[t][3][s][2]
- end
- -- Main
- function game()
- -- Set
- setGame()
- clearWell()
- paintWell()
- math.randomseed( os.time() )
- local tetris = math.random(1,7)
- local state = 1
- local tetriminosX = 5
- local tetriminosY = 1
- local time = os.clock()
- local elasped = 0
- local new = false
- local space = false
- local movedX = 0
- local line = 0
- local score = 0
- local nd = false
- local fps = 1/100
- local wait = 1
- -- Game
- while true do
- wait = 2.35*math.pow(line+5,-0.41)
- movedX = 0
- os.startTimer(fps)
- -- Input
- event, key = os.pullEvent()
- if event == "key" then
- os.pullEvent("timer")
- if key == 203 then
- tetriminosX = tetriminosX -1
- movedX = -1
- elseif key == 205 then
- tetriminosX = tetriminosX +1
- movedX = 1
- elseif key == 200 then state = state +1
- elseif key == 208 then state = state -1
- elseif key == 82 or key == 157 then tetriminosY = tetriminosY +1
- elseif key == 57 then space = true
- --elseif key == 76 then line = line +1
- elseif key == 14 then break end
- end
- -- Core
- if state < 1 then state=4
- elseif state > 4 then state=1 end
- local w,h = getTetriminosSize(tetris,state)
- if tetriminosX < 1 then tetriminosX = 1
- elseif tetriminosX > 11-w then tetriminosX = 11-w end
- elasped = os.clock() - time
- if elasped>wait then
- time = os.clock()
- tetriminosY = tetriminosY +1
- end
- repeat
- if space then tetriminosY = tetriminosY +1 end
- if tetriminosY > 19-h then
- tetriminosY = 19-h
- new = true
- elseif checkWell(tetris,state,tetriminosX,tetriminosY) then
- if not (movedX == 0) then
- tetriminosX = tetriminosX-movedX
- movedX = 0
- else
- tetriminosY = tetriminosY-1
- new = true
- end
- end
- if new then
- updateWell(tetris,state,tetriminosX,tetriminosY)
- local check = checkLine()
- if check == -1 then nd = true
- elseif check>0 then
- line = line+check
- score = score + (10*((check+1)*(check+1))-2*(check+1))
- end
- tetris = math.random(1,7)
- state = 1
- tetriminosX = 5
- tetriminosY = 1
- new = false
- space = false
- time = os.clock()
- end
- until space == false
- if nd then break end
- -- Render
- setText(line,score)
- paintWell()
- paintTetriminos(tetris,state,tetriminosX,tetriminosY)
- end
- setColor(colors.black,colors.white)
- term.setCursorPos(35,12)
- term.write("Again ?")
- setColor(colors.green,colors.white)
- term.setCursorPos(27,13)
- term.write("Yes= Enter")
- setColor(colors.black,colors.white)
- term.write("/")
- setColor(colors.red,colors.white)
- term.write("No= Backspace")
- while true do
- event, key = os.pullEvent()
- if event == "key" then
- if key == 28 then return true,score
- elseif key == 14 then return false,score end
- end
- end
- end
- --[ Menu ]--
- -- Variables
- local menu = {"Start","Leaderboard","Options"}
- -- Functions
- function setMenu()
- term.clear()
- local x,y = term.getSize()
- for i=1,x do
- for j=1,y do
- paintutils.drawPixel(i,j,colors.white)
- end
- end
- local w,h = term.getSize()
- setColor(colors.lightBlue,colors.white)
- term.setCursorPos(1,h/2 - #menu/2-1)
- for i = 1,w do
- term.write("_")
- end
- term.setCursorPos(1,h/2 + #menu/2 + 1)
- for i = 1,w do
- term.write("_")
- end
- term.setCursorPos(w/2-5,h/2 - #menu/2-1)
- term.write("__ Menu __")
- end
- function drawMenu(l)
- if l < 0 or l > #menu then return false end
- local w,h = term.getSize()
- for i = 1,#menu do
- setColor(colors.black,colors.white)
- local x = w/2 - string.len(menu[i])/2
- local y = h/2 - #menu/2 + i
- term.setCursorPos(1,y)
- term.clearLine()
- if l==i then
- setColor(colors.lightBlue,colors.white)
- term.setCursorPos(x-2,y)
- term.write(">")
- term.setCursorPos(x+string.len(menu[i])+1,y)
- term.write("<")
- setColor(colors.lime,colors.white)
- end
- term.setCursorPos(x,y)
- term.write(menu[i])
- end
- return true
- end
- function info(m)
- term.setCursorPos(term.getSize()-string.len(m),19)
- term.clearLine()
- setColor(colors.green,colors.white)
- term.write(m)
- end
- --[ Leaderboard ]--
- function getLeaderboard()
- f = fs.open("score","r")
- score = f.readLine()
- f.close()
- return textutils.unserialize(score)
- end
- function setLeaderboard()
- while not (fs.exists("score")) do
- setColor(colors.green,colors.white)
- term.clear()
- term.setCursorPos(1,1)
- term.write("First start / Premier lancement")
- setColor(colors.red,colors.white)
- term.setCursorPos(1,2)
- term.write("We are going to create the file 'score',")
- term.setCursorPos(1,3)
- term.write("yours scores will be save in it")
- term.setCursorPos(1,4)
- term.write("------------------------------------------------------------------------")
- term.setCursorPos(1,5)
- term.write("Nous allons cr�83�A9er le fichier 'score',")
- term.setCursorPos(1,6)
- term.write("vos scores y seront sauvegard�83�A9s")
- term.setCursorPos(1,11)
- term.write("Save then Exit (Ctrl). Thanks You")
- term.setCursorPos(1,12)
- term.write("---------------------------------------------------------------")
- term.setCursorPos(1,13)
- term.write("Sauvergardez puis quittez (Ctrl). Merci")
- os.pullEvent("key")
- shell.run("edit score")
- end
- if fs.exists("score") then
- local s = getLeaderboard()
- if s then
- if #s == 10 then return true end
- end
- end
- f = fs.open("score","w")
- local empty = {9999999,999999,99999,9999,999,321,123,99,42,36}
- f.writeLine(textutils.serialize(empty))
- f.close()
- return true
- end
- function addToLeaderboard(s)
- local score = getLeaderboard()
- for i=1,#score do
- if s >= score[i] then
- for j=(#score-1),i,-1 do
- score[j+1] = score[j]
- end
- score[i] = s
- break
- end
- end
- f = fs.open("score","w")
- f.writeLine(textutils.serialize(score))
- f.close()
- end
- function showLeaderboard()
- term.clear()
- local w,h = term.getSize()
- setColor(colors.lightBlue,colors.white)
- term.setCursorPos(1,3)
- for i = 1,w do
- term.write("_")
- end
- term.setCursorPos(1,15)
- for i = 1,w do
- term.write("_")
- end
- term.setCursorPos(w/2-8,3)
- term.write("__ Leaderboard __")
- setColor(colors.black,colors.white)
- local s = getLeaderboard()
- for i=1,#s do
- term.setCursorPos(w/2 - string.len(tostring(s[i]))/2,4+i)
- term.write(tostring(math.ceil(s[i])))
- end
- event = os.pullEvent("key")
- end
- --[ Main ]--
- setLeaderboard()
- setMenu()
- local selected = 1
- while true do
- drawMenu(selected)
- os.startTimer(0.1)
- event,key = os.pullEvent()
- if event == "key" then
- os.pullEvent()
- if key == 200 then selected = selected -1
- elseif key == 208 then selected = selected +1
- elseif key == 14 then break
- elseif key == 28 then
- if selected == 1 then
- while true do
- again,s = game()
- addToLeaderboard(s)
- if not again then break end
- end
- elseif selected == 2 then
- showLeaderboard()
- end
- setMenu()
- end
- end
- if selected < 1 then selected = 1
- elseif selected > #menu then selected = #menu end
- if selected == 1 then info("Let's fall in line")
- elseif selected == 3 then info("Press 'Backspace' To Exit")
- else info("Try to reach the top !") end
- end
- -- Clear & End
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement