Legebatterie

apps--2048

Feb 23rd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. if not(fs.exists("2048score")) then
  2. f=fs.open("2048score","w")
  3. f.writeLine("0")
  4. f.close()
  5. end
  6.  
  7. f=fs.open("2048score","r")
  8. highScore=tonumber(f.readLine())
  9. f.close()
  10.  
  11. ---------variables------------
  12. Squares={}
  13. finished={}
  14. ani=0.1
  15. ani2=0.1
  16. score=0
  17.  
  18.  
  19. ---------functions-----------
  20.  
  21. drawSquare=function(number,pos)
  22.  
  23. if number==0 then color="white" Squares[pos]=nil end
  24. if number==1 then color="lightGray" end
  25. if number==2 then color="gray" end
  26. if number==4 then color="lightBlue" end
  27. if number==8 then color="blue" end
  28. if number==16 then color="lime" end
  29. if number==32 then color="green" end
  30. if number==64 then color="orange" end
  31. if number==128 then color="yellow" end
  32. if number==256 then color="red" end
  33. if number==512 then color="pink" end
  34. if number==1024 then color="magenta" end
  35. if number==2048 then color="purple" end
  36.  
  37. xPos=math.fmod(pos,4)
  38. if xPos==0 then xPos=4 end
  39. yPos=math.ceil(pos/4)
  40. paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)+1,((xPos-1)*7)+7,((yPos-1)*5)+4,colors[color])
  41.  
  42. l=string.len(tostring(number))
  43.  
  44. term.setCursorPos(((xPos-1)*7)+4-math.floor(l/2),((yPos-1)*5)+2)
  45. term.setTextColor(colors.white)
  46. term.setBackgroundColor(colors[color])
  47. write(number)
  48.  
  49. if not(number==0) then
  50. Squares[pos]=number
  51. end
  52.  
  53.  
  54.  
  55. end
  56.  
  57.  
  58. spawnSquare=function()
  59.  
  60. pos=math.random(1,16)
  61. number=math.random(1,2)
  62.  
  63. while not(Squares[pos]==nil) do
  64. pos=math.random(1,16)
  65. end
  66.  
  67. drawSquare(number,pos)
  68. score=score+number
  69. writeScore(score)
  70.  
  71. end
  72.  
  73. moveSquares=function(direction)
  74.  
  75. if direction=="up" then
  76. finished={}
  77. for i=1,4 do
  78. for i=5,16 do
  79. if not(Squares[i]==nil or finished[i]==true) then
  80. index=Squares[i]
  81. if Squares[i-4]==nil then
  82. Squares[i]=nil
  83. drawSquare(0,i)
  84. animation(index,i-4,"up")
  85. drawSquare(index,i-4)
  86. if not(ani2==0) then sleep(ani2) end
  87. elseif Squares[i-4]==Squares[i] then
  88. Squares[i]=nil
  89. drawSquare(0,i)
  90. drawSquare(index*2,i-4)
  91. finished[i]=true
  92. score=score+(index*2)
  93. writeScore(score)
  94. end
  95.  
  96. end
  97. end
  98. end
  99. end
  100.  
  101. if direction=="down" then
  102. finished={}
  103. for i=1,4 do
  104. for i=1,12 do
  105. if not(Squares[i]==nil or finished[i]==true) then
  106. index=Squares[i]
  107. if Squares[i+4]==nil then
  108. Squares[i]=nil
  109. drawSquare(0,i)
  110. animation(index,i+4,"down")
  111. drawSquare(index,i+4)
  112. if not(ani2==0) then sleep(ani2) end
  113. elseif Squares[i+4]==Squares[i] then
  114. Squares[i]=nil
  115. drawSquare(0,i)
  116. drawSquare(index*2,i+4)
  117. finished[i]=true
  118. score=score+(index*2)
  119. writeScore(score)
  120. end
  121.  
  122. end
  123. end
  124. end
  125. end
  126.  
  127. if direction=="left" then
  128. finished={}
  129. for i=1,4 do
  130. for i=2,16 do
  131. if not(i==5 or i==9 or i==13 or finished[i]==true) then
  132. if not(Squares[i]==nil) then
  133. index=Squares[i]
  134. if Squares[i-1]==nil then
  135. drawSquare(0,i)
  136. Squares[i]=nil
  137. animation(index,i-1,"left")
  138. drawSquare(index,i-1)
  139. if not(ani2==0) then sleep(ani2) end
  140. elseif Squares[i-1]==Squares[i] then
  141. Squares[i]=nil
  142. drawSquare(0,i)
  143. drawSquare(index*2,i-1)
  144. finished[i]=true
  145. score=score+(index*2)
  146. writeScore(score)
  147. end
  148.  
  149. end
  150. end
  151. end
  152. end
  153. end
  154.  
  155. if direction=="right" then
  156. finshed={}
  157. for i=1,4 do
  158. for i=1,15 do
  159. if not(i==4 or i==8 or i==12 or finshed[i]==true) then
  160. if not(Squares[i]==nil) then
  161. index=Squares[i]
  162. if Squares[i+1]==nil then
  163. Squares[i]=nil
  164. drawSquare(0,i)
  165. animation(index,i+1,"right")
  166. drawSquare(index,i+1)
  167. if not(ani2==0) then sleep(ani2) end
  168. elseif Squares[i+1]==Squares[i] then
  169. Squares[i]=nil
  170. drawSquare(0,i)
  171. drawSquare(index*2,i+1)
  172. finished[i]=true
  173. score=score+(index*2)
  174. writeScore(score)
  175. end
  176.  
  177. end
  178. end
  179. end
  180. end
  181. end
  182.  
  183. end
  184.  
  185. ---------Design-----------
  186. resetDesign=function()
  187. term.setBackgroundColor(colors.gray)
  188. term.clear()
  189. paintutils.drawFilledBox(1,0,29,20,colors.white)
  190. paintutils.drawBox(1,0,29,20,colors.black)
  191. paintutils.drawBox(8,0,8,20,colors.black)
  192. paintutils.drawBox(15,0,15,20,colors.black)
  193. paintutils.drawBox(22,0,22,20,colors.black)
  194. paintutils.drawBox(1,5,29,5,colors.black)
  195. paintutils.drawBox(1,10,29,10,colors.black)
  196. paintutils.drawBox(1,15,29,15,colors.black)
  197. term.setTextColor(colors.black)
  198. term.setBackgroundColor(colors.gray)
  199. term.setCursorPos(34,11)
  200. write("move: arrow Keys")
  201. term.setCursorPos(34,13)
  202. write("enter: restart")
  203. term.setCursorPos(34,15)
  204. write("backSpace: quit")
  205. term.setCursorPos(34,17)
  206. write("animation: "..ani+ani2.."s")
  207.  
  208. term.setCursorPos(33,4)
  209. write("score: ")
  210. term.setCursorPos(33,6)
  211. write("HighScore: "..tostring(highScore))
  212.  
  213. term.setTextColor(colors.purple)
  214. term.setCursorPos(31,1)
  215. write("--2048 by Maxintosh--")
  216.  
  217.  
  218.  
  219. end
  220.  
  221. drawGutter=function()
  222. paintutils.drawBox(1,0,29,20,colors.black)
  223. paintutils.drawBox(8,0,8,20,colors.black)
  224. paintutils.drawBox(15,0,15,20,colors.black)
  225. paintutils.drawBox(22,0,22,20,colors.black)
  226. paintutils.drawBox(1,5,29,5,colors.black)
  227. paintutils.drawBox(1,10,29,10,colors.black)
  228. paintutils.drawBox(1,15,29,15,colors.black)
  229. end
  230.  
  231. animation=function(number,pos,dir)
  232.  
  233. if number==1 then color="lightGray" end
  234. if number==2 then color="gray" end
  235. if number==4 then color="lightBlue" end
  236. if number==8 then color="blue" end
  237. if number==16 then color="lime" end
  238. if number==32 then color="green" end
  239. if number==64 then color="orange" end
  240. if number==128 then color="yellow" end
  241. if number==256 then color="red" end
  242. if number==512 then color="pink" end
  243. if number==1024 then color="magenta" end
  244. if number==2048 then color="purple" end
  245.  
  246. l=string.len(tostring(number))
  247. xPos=math.fmod(pos,4)
  248. if xPos==0 then xPos=4 end
  249. yPos=math.ceil(pos/4)
  250.  
  251. if dir=="up" then
  252. paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)+4,((xPos-1)*7)+7,((yPos-1)*5)+7,colors[color])
  253. if not(ani==0) then sleep(ani) end
  254. paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)+4,((xPos-1)*7)+7,((yPos-1)*5)+7,colors.white)
  255. drawGutter()
  256. end
  257.  
  258. if dir=="down" then
  259. paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)-2,((xPos-1)*7)+7,((yPos-1)*5)+1,colors[color])
  260. if not(ani==0) then sleep(ani) end
  261. paintutils.drawFilledBox(((xPos-1)*7)+2,((yPos-1)*5)-2,((xPos-1)*7)+7,((yPos-1)*5)+1,colors.white)
  262. drawGutter()
  263. end
  264.  
  265. if dir=="left" then
  266. paintutils.drawFilledBox(((xPos-1)*7)+4,((yPos-1)*5)+1,((xPos-1)*7)+9,((yPos-1)*5)+4,colors[color])
  267. if not(ani==0) then sleep(ani) end
  268. paintutils.drawFilledBox(((xPos-1)*7)+4,((yPos-1)*5)+1,((xPos-1)*7)+9,((yPos-1)*5)+4,colors.white)
  269. drawGutter()
  270. end
  271.  
  272. if dir=="right" then
  273. paintutils.drawFilledBox(((xPos-1)*7)+0,((yPos-1)*5)+1,((xPos-1)*7)+5,((yPos-1)*5)+4,colors[color])
  274. if not(ani==0) then sleep(ani) end
  275. paintutils.drawFilledBox(((xPos-1)*7)+0,((yPos-1)*5)+1,((xPos-1)*7)+5,((yPos-1)*5)+4,colors.white)
  276. drawGutter()
  277. end
  278. end
  279.  
  280.  
  281. writeScore=function(s)
  282. paintutils.drawBox(41,4,51,4,colors.gray)
  283. term.setBackgroundColor(colors.gray)
  284. term.setTextColor(colors.black)
  285. term.setCursorPos(41,4)
  286. write(score)
  287.  
  288. if score>highScore then
  289. f=fs.open("2048score","w")
  290. f.writeLine(score)
  291. f.flush()
  292. end
  293.  
  294.  
  295. end
  296. ---------Program-----------
  297. resetDesign()
  298. spawnSquare()
  299.  
  300. while true do
  301. event,k=os.pullEvent("key")
  302. if k==200 then --up
  303. moveSquares("up")
  304. spawnSquare()
  305. elseif k==208 then --down
  306. moveSquares("down")
  307. spawnSquare()
  308. elseif k==203 then --left
  309. moveSquares("left")
  310. spawnSquare()
  311. elseif k==205 then --right
  312. moveSquares("right")
  313. spawnSquare()
  314. elseif k==14 then --leave
  315. error()
  316. elseif k==28 then --down
  317. shell.run(shell.getRunningProgram())
  318. error()
  319. 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
  320. if k==2 then ani=0.05 ani2=0 end
  321. if k==3 then ani=0.05 ani2=0.05 end
  322. if k==4 then ani=0.1 ani2=0.05 end
  323. if k==5 then ani=0.1 ani2=0.1 end
  324. if k==6 then ani=0.15 ani2=0.1 end
  325. if k==7 then ani=0.15 ani2=0.15 end
  326. if k==8 then ani=0.2 ani2=0.15 end
  327. if k==9 then ani=0.2 ani2=0.2 end
  328. if k==10 then ani=0.25 ani2=0.2 end
  329. if k==11 then ani=0 ani2=0 end
  330. term.setTextColor(colors.black)
  331. paintutils.drawBox(34,17,50,17,colors.gray)
  332. term.setBackgroundColor(colors.gray)
  333. term.setCursorPos(34,17)
  334. write("animation: "..ani+ani2.."s")
  335.  
  336.  
  337. end
  338. end
Advertisement
Add Comment
Please, Sign In to add comment