Advertisement
Phins

DoodleGame

Dec 13th, 2015
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. tArgs = {...}
  2. config = tArgs[1]
  3.  
  4. function testfor(x1,y1,x2,y2,ex,ey)
  5. for i=x1,x2 do
  6. for o=y1,y2 do
  7. if i == ex and o == ey then
  8. return true
  9. end
  10. end
  11. end
  12. return false
  13. end
  14.  
  15. if not fs.exists(config) then
  16. file = fs.open(config,"w")
  17. file.write("{\n")
  18. file.write("Difficulty = normal;\n")
  19. file.write("Startstep = 0;")
  20. file.write("Record = 0;")
  21. file.write("}")
  22. file.close()
  23. difi = "normal"
  24. end
  25.  
  26. data = complile(config)
  27. data[3][2] = tonumber(data[3][2])
  28. difi = data[1][2]
  29. platnum = tonumber(data[2][2])
  30.  
  31. function editsettings(edit)
  32. file = fs.open(config,"w")
  33. file.write("{\n")
  34. if not edit then
  35. data[1][2] = difi
  36. data[2][2] = platnum
  37. end
  38. for i=1,#data do
  39. file.write(data[i][1].." = "..data[i][2]..";\n")
  40. end
  41. file.write("}")
  42. file.close()
  43. end
  44.  
  45. function coloredsteps(num)
  46. if num < 50 then return blue
  47. elseif num < 100 then return green
  48. elseif num < 150 then return yellow
  49. elseif num < 200 then return orange
  50. elseif num < 250 then return red
  51. elseif num < 300 then return lightGray
  52. elseif num < 350 then return gray
  53. elseif num < 400 then return black
  54. elseif num < 450 then return white
  55. elseif num >= 450 then return 2^math.random(1,15) end
  56. end
  57.  
  58. function gameover()
  59. sel = 1
  60. while true do
  61. Clear()
  62. scp(20,8)
  63. write((sel == 1 and ">" or sel == 2 and " ").."Restart"..(sel == 1 and "<" or sel == 2 and " "))
  64. scp(21,10)
  65. write((sel == 2 and ">" or sel == 1 and " ").."CLOSE"..(sel == 2 and "<" or sel == 1 and " "))
  66. repeat event,key = os.pullEvent("key_up") until key == up or key == down or key == enter
  67. if key == up then sel = 1 end
  68. if key == down then sel = 2 end
  69. if key == enter then
  70. if sel == 1 then shell.run("Doodle/game",config) end
  71. break
  72. end
  73. end
  74. end
  75.  
  76.  
  77. function settings()
  78. while true do
  79. Clear(lightBlue)
  80. scp(2,6)
  81. write("!Every 50th Step new color!")
  82. scp(2,18)
  83. write("Record: "..data[3][2])
  84. scp(2,2)
  85. write("Difficulty: ")
  86. drawPixel(14,2,(difi == "normal" and yellow or difi == "easy" and green or difi == "hard" and red))
  87. sbc(lightBlue)
  88. write(" "..data[1][2])
  89. scp(2,4)
  90. write("Starting at step: "..platnum)
  91. drawPixel(20,4,coloredsteps(platnum))
  92. drawBox(45,16,50,18,green)
  93. scp(46,17)
  94. write("Back")
  95. event,key,x,y = os.pullEvent("mouse_click")
  96. if testfor(45,16,50,18,x,y) then os.startTimer(0.001) break end
  97. if testfor(2,2,12,2,x,y) then
  98. if difi == "normal" then
  99. difi = "hard"
  100. elseif difi == "hard" then
  101. difi = "easy"
  102. elseif difi == "easy" then
  103. difi = "normal"
  104. end
  105. editsettings()
  106. end
  107. if testfor(2,4,18,4,x,y) then
  108. if platnum + 50 <= data[3][2] and platnum < 450 then
  109. platnum = platnum + 50
  110. else
  111. platnum = 0
  112. end
  113. editsettings()
  114. end
  115. end
  116. end
  117.  
  118. function smenu()
  119. os.startTimer(0.001)
  120. menu = true
  121. while menu do
  122. if event == "timer" then
  123. os.startTimer(3)
  124. Clear(lightBlue)
  125. for i=1,7 do
  126. n = math.random(1,48)
  127. drawLine(n,i*2,n+3,i*2,blue)
  128. end
  129. x = math.random(3,48)
  130. y = math.random(3,10)
  131. drawPixel(x,y,green)
  132. drawBox(24,16,33,18)
  133. scp(25,17)
  134. write("Settings")
  135. drawBox(16,16,22,18,green)
  136. scp(17,17)
  137. write("Start")
  138. end
  139. event,key,x,y = os.pullEvent()
  140. if testfor(16,16,22,18,x,y) and event == "mouse_click" then break end
  141. if testfor(24,16,33,18,x,y) and event == "mouse_click" then settings() end
  142. end
  143. end
  144. smenu()
  145.  
  146. if difi == "normal" then difi = 8
  147. elseif difi == "hard" then difi = 6
  148. elseif difi == "easy" then difi = 10
  149. else error("Unable to read Difficulty",0) end
  150.  
  151.  
  152. addlog("Game Started")
  153. doodle = {20,10}
  154. mot = 10
  155. plat = {}
  156.  
  157. function generatePlat()
  158. addlog("All Platforms generated")
  159. for i=1,10 do
  160. n = math.random(1,48)
  161. table.insert(plat,1,{n,n+3,i*2,platnum})
  162. end
  163. end
  164.  
  165. function addPlat()
  166. platnum = platnum + 1
  167. addlog("Added a new Platform")
  168. n = math.random(1,48)
  169. table.insert(plat,1,{n,n+3,1,platnum})
  170. table.remove(plat,20)
  171. end
  172.  
  173. timer = os.startTimer(0.03)
  174. os.startTimer(0.2)
  175. generatePlat()
  176. while true do
  177. if event == "timer" and key ~= timer then
  178. for i=1,#plat do
  179. for o=plat[i][1],plat[i][2] do
  180. if doodle[1] == o and doodle[2]+1 == plat[i][3] and mot < 1 then
  181. mot = difi
  182. end
  183. end
  184. end
  185.  
  186. if mot > 0 then
  187. doodle[2] = doodle[2] - 1
  188. if doodle[2] < 4 then
  189. doodle[2] = doodle[2] + 1
  190. for i=1,#plat do
  191. plat[i][3] = plat[i][3] + 2
  192. end
  193. addPlat()
  194. end
  195. else
  196. doodle[2] = doodle[2] + 1
  197. end
  198.  
  199. if doodle[2] > 19 then
  200. Clear(black)
  201. if platnum - data[2][2] > data[3][2] then
  202. data[3][2] = platnum - data[2][2]
  203. editsettings(true)
  204. printError("!NEW RECORD!")
  205. print("")
  206. printError("SCORE: "..data[3][2])
  207. os.sleep(3)
  208. gameover()
  209. os.shutdown()
  210. else
  211. printError("GAME OVER")
  212. os.sleep(1)
  213. gameover()
  214. os.reboot()
  215. end
  216. end
  217.  
  218. mot = mot - 1
  219.  
  220. addlog("DrawScreen")
  221. Clear(lightBlue)
  222. for i=1,#plat do
  223. drawLine(plat[i][1],plat[i][3],plat[i][2],plat[i][3],coloredsteps(plat[i][4]))
  224. scp(plat[i][1],plat[i][3])
  225. write("----")
  226. end
  227. drawPixel(doodle[1],doodle[2],green)
  228. sbc(lightBlue)
  229. scp(43 - string.len(tostring(platnum)),1)
  230. write("Score: "..platnum-data[2][2])
  231. os.startTimer(0.3)
  232.  
  233. elseif key == timer then
  234. if move then
  235. drawPixel(doodle[1],doodle[2],lightBlue)
  236. if doodle[1] == 1 and move == -1 then doodle[1],move = 51,nil
  237. elseif doodle[1] == 51 and move == 1 then doodle[1],move = 1,nil
  238. else doodle[1],move = doodle[1] + move,nil end
  239. drawPixel(doodle[1],doodle[2],green)
  240. end
  241. timer = os.startTimer(0.1)
  242. end
  243.  
  244. event,key = os.pullEvent()
  245.  
  246. if event == "key" then
  247. if key == left then addlog("Pressed Left") move = -1 end
  248. if key == right then addlog("pressed Right") move = 1 end
  249. if key == keys.enter then addlog("pressed Enter") Clear(black) break end
  250. end
  251.  
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement