Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not(fs.exists("2048score")) then
- f=fs.open("2048score","w")
- f.writeLine("0")
- f.close()
- end
- f=fs.open("2048score","r")
- highScore=tonumber(f.readLine())
- f.close()
- ---------variables------------
- Squares={}
- finished={}
- ani=0.1
- ani2=0.1
- score=0
- ---------functions-----------
- drawSquare=function(number,pos)
- if number==0 then color="white" Squares[pos]=nil end
- if number==1 then color="lightGray" end
- if number==2 then color="gray" end
- if number==4 then color="lightBlue" end
- if number==8 then color="blue" end
- if number==16 then color="lime" end
- if number==32 then color="green" end
- if number==64 then color="orange" end
- if number==128 then color="yellow" end
- if number==256 then color="red" end
- if number==512 then color="pink" end
- if number==1024 then color="magenta" end
- if number==2048 then color="purple" end
- xPos=math.fmod(pos,4)
- if xPos==0 then xPos=4 end
- yPos=math.ceil(pos/4)
- paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)+1,((xPos-1)*7)+7,((yPos-1)*5)+4,colors[color])
- l=string.len(tostring(number))
- term.setCursorPos(((xPos-1)*7)+4-math.floor(l/2),((yPos-1)*5)+2)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors[color])
- write(number)
- if not(number==0) then
- Squares[pos]=number
- end
- end
- spawnSquare=function()
- pos=math.random(1,16)
- number=math.random(1,2)
- while not(Squares[pos]==nil) do
- pos=math.random(1,16)
- end
- drawSquare(number,pos)
- score=score+number
- writeScore(score)
- end
- moveSquares=function(direction)
- if direction=="up" then
- finished={}
- for i=1,4 do
- for i=5,16 do
- if not(Squares[i]==nil or finished[i]==true) then
- index=Squares[i]
- if Squares[i-4]==nil then
- Squares[i]=nil
- drawSquare(0,i)
- animation(index,i-4,"up")
- drawSquare(index,i-4)
- if not(ani2==0) then sleep(ani2) end
- elseif Squares[i-4]==Squares[i] then
- Squares[i]=nil
- drawSquare(0,i)
- drawSquare(index*2,i-4)
- finished[i]=true
- score=score+(index*2)
- writeScore(score)
- end
- end
- end
- end
- end
- if direction=="down" then
- finished={}
- for i=1,4 do
- for i=1,12 do
- if not(Squares[i]==nil or finished[i]==true) then
- index=Squares[i]
- if Squares[i+4]==nil then
- Squares[i]=nil
- drawSquare(0,i)
- animation(index,i+4,"down")
- drawSquare(index,i+4)
- if not(ani2==0) then sleep(ani2) end
- elseif Squares[i+4]==Squares[i] then
- Squares[i]=nil
- drawSquare(0,i)
- drawSquare(index*2,i+4)
- finished[i]=true
- score=score+(index*2)
- writeScore(score)
- end
- end
- end
- end
- end
- if direction=="left" then
- finished={}
- for i=1,4 do
- for i=2,16 do
- if not(i==5 or i==9 or i==13 or finished[i]==true) then
- if not(Squares[i]==nil) then
- index=Squares[i]
- if Squares[i-1]==nil then
- drawSquare(0,i)
- Squares[i]=nil
- animation(index,i-1,"left")
- drawSquare(index,i-1)
- if not(ani2==0) then sleep(ani2) end
- elseif Squares[i-1]==Squares[i] then
- Squares[i]=nil
- drawSquare(0,i)
- drawSquare(index*2,i-1)
- finished[i]=true
- score=score+(index*2)
- writeScore(score)
- end
- end
- end
- end
- end
- end
- if direction=="right" then
- finshed={}
- for i=1,4 do
- for i=1,15 do
- if not(i==4 or i==8 or i==12 or finshed[i]==true) then
- if not(Squares[i]==nil) then
- index=Squares[i]
- if Squares[i+1]==nil then
- Squares[i]=nil
- drawSquare(0,i)
- animation(index,i+1,"right")
- drawSquare(index,i+1)
- if not(ani2==0) then sleep(ani2) end
- elseif Squares[i+1]==Squares[i] then
- Squares[i]=nil
- drawSquare(0,i)
- drawSquare(index*2,i+1)
- finished[i]=true
- score=score+(index*2)
- writeScore(score)
- end
- end
- end
- end
- end
- end
- end
- ---------Design-----------
- resetDesign=function()
- term.setBackgroundColor(colors.gray)
- term.clear()
- paintutils.drawFilledBox(1,0,29,20,colors.white)
- paintutils.drawBox(1,0,29,20,colors.black)
- paintutils.drawBox(8,0,8,20,colors.black)
- paintutils.drawBox(15,0,15,20,colors.black)
- paintutils.drawBox(22,0,22,20,colors.black)
- paintutils.drawBox(1,5,29,5,colors.black)
- paintutils.drawBox(1,10,29,10,colors.black)
- paintutils.drawBox(1,15,29,15,colors.black)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(34,11)
- write("move: arrow Keys")
- term.setCursorPos(34,13)
- write("enter: restart")
- term.setCursorPos(34,15)
- write("backSpace: quit")
- term.setCursorPos(34,17)
- write("animation: "..ani+ani2.."s")
- term.setCursorPos(33,4)
- write("score: ")
- term.setCursorPos(33,6)
- write("HighScore: "..tostring(highScore))
- term.setTextColor(colors.purple)
- term.setCursorPos(31,1)
- write("--2048 by Maxintosh--")
- end
- drawGutter=function()
- paintutils.drawBox(1,0,29,20,colors.black)
- paintutils.drawBox(8,0,8,20,colors.black)
- paintutils.drawBox(15,0,15,20,colors.black)
- paintutils.drawBox(22,0,22,20,colors.black)
- paintutils.drawBox(1,5,29,5,colors.black)
- paintutils.drawBox(1,10,29,10,colors.black)
- paintutils.drawBox(1,15,29,15,colors.black)
- end
- animation=function(number,pos,dir)
- if number==1 then color="lightGray" end
- if number==2 then color="gray" end
- if number==4 then color="lightBlue" end
- if number==8 then color="blue" end
- if number==16 then color="lime" end
- if number==32 then color="green" end
- if number==64 then color="orange" end
- if number==128 then color="yellow" end
- if number==256 then color="red" end
- if number==512 then color="pink" end
- if number==1024 then color="magenta" end
- if number==2048 then color="purple" end
- l=string.len(tostring(number))
- xPos=math.fmod(pos,4)
- if xPos==0 then xPos=4 end
- yPos=math.ceil(pos/4)
- if dir=="up" then
- paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)+4,((xPos-1)*7)+7,((yPos-1)*5)+7,colors[color])
- if not(ani==0) then sleep(ani) end
- paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)+4,((xPos-1)*7)+7,((yPos-1)*5)+7,colors.white)
- drawGutter()
- end
- if dir=="down" then
- paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)-2,((xPos-1)*7)+7,((yPos-1)*5)+1,colors[color])
- if not(ani==0) then sleep(ani) end
- paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)-2,((xPos-1)*7)+7,((yPos-1)*5)+1,colors.white)
- drawGutter()
- end
- if dir=="left" then
- paintutils.drawFilledBox(((xPos-1)*7)+4,((yPos-1)*5)+1,((xPos-1)*7)+9,((yPos-1)*5)+4,colors[color])
- if not(ani==0) then sleep(ani) end
- paintutils.drawFilledBox(((xPos-1)*7)+4,((yPos-1)*5)+1,((xPos-1)*7)+9,((yPos-1)*5)+4,colors.white)
- drawGutter()
- end
- if dir=="right" then
- paintutils.drawFilledBox(((xPos-1)*7)+0,((yPos-1)*5)+1,((xPos-1)*7)+5,((yPos-1)*5)+4,colors[color])
- if not(ani==0) then sleep(ani) end
- paintutils.drawFilledBox(((xPos-1)*7)+0,((yPos-1)*5)+1,((xPos-1)*7)+5,((yPos-1)*5)+4,colors.white)
- drawGutter()
- end
- end
- writeScore=function(s)
- paintutils.drawBox(41,4,51,4,colors.gray)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.black)
- term.setCursorPos(41,4)
- write(score)
- if score>highScore then
- f=fs.open("2048score","w")
- f.writeLine(score)
- f.flush()
- end
- end
- ---------Program-----------
- resetDesign()
- spawnSquare()
- while true do
- event,k=os.pullEvent("key")
- if k==200 then --up
- moveSquares("up")
- spawnSquare()
- elseif k==208 then --down
- moveSquares("down")
- spawnSquare()
- elseif k==203 then --left
- moveSquares("left")
- spawnSquare()
- elseif k==205 then --right
- moveSquares("right")
- spawnSquare()
- elseif k==14 then --leave
- error()
- elseif k==28 then --down
- shell.run(shell.getRunningProgram())
- error()
- elseif k==2 or k==3 or k==4 or k==5 or k==6 or k==7 or k==8 or k==9 or k==10 or k==11 then
- if k==2 then ani=0.05 ani2=0 end
- if k==3 then ani=0.05 ani2=0.05 end
- if k==4 then ani=0.1 ani2=0.05 end
- if k==5 then ani=0.1 ani2=0.1 end
- if k==6 then ani=0.15 ani2=0.1 end
- if k==7 then ani=0.15 ani2=0.15 end
- if k==8 then ani=0.2 ani2=0.15 end
- if k==9 then ani=0.2 ani2=0.2 end
- if k==10 then ani=0.25 ani2=0.2 end
- if k==11 then ani=0 ani2=0 end
- term.setTextColor(colors.black)
- paintutils.drawBox(34,17,50,17,colors.gray)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(34,17)
- write("animation: "..ani+ani2.."s")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment