Creeper9207

wormPORTALS

Feb 7th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. --custom code to set the background
  2. term.setBackgroundColour(colors.black)
  3. -- Display the start screen
  4. local w,h = term.getSize()
  5.  
  6. local titleColour, headingColour, textColour, wormColour, fruitColour
  7. if term.isColour() then
  8. titleColour = colours.red
  9. headingColour = colours.yellow
  10. textColour = colours.white
  11. wormColour = colours.green
  12. fruitColour = colours.red
  13. else
  14. titleColour = colours.white
  15. headingColour = colours.white
  16. textColour = colours.white
  17. wormColour = colours.white
  18. fruitColour = colours.white
  19. end
  20.  
  21. function printCentred( y, s )
  22. local x = math.floor((w - string.len(s)) / 2)
  23. term.setCursorPos(x,y)
  24. --term.clearLine()
  25. term.write( s )
  26. end
  27.  
  28. local xVel,yVel = 1,0
  29. local xPos, yPos = math.floor(w/2), math.floor(h/2)
  30. local pxVel, pyVel = nil, nil
  31.  
  32. local nLength = 1
  33. local nExtraLength = 6
  34. local bRunning = true
  35.  
  36. local tailX,tailY = xPos,yPos
  37. local nScore = 0
  38. local nDifficulty = 2
  39. local nSpeed, nInterval
  40.  
  41. -- Setup the screen
  42. local screen = {}
  43. for x=1,w do
  44. screen[x] = {}
  45. for y=1,h do
  46. screen[x][y] = {}
  47. end
  48. end
  49. screen[xPos][yPos] = { snake = true }
  50.  
  51. local nFruit = 1
  52. local tFruits = {
  53. "A", "B", "C", "D", "E", "F", "G", "H",
  54. "I", "J", "K", "L", "M", "N", "O", "P",
  55. "Q", "R", "S", "T", "U", "V", "W", "X",
  56. "Y", "Z",
  57. "a", "b", "c", "d", "e", "f", "g", "h",
  58. "i", "j", "k", "l", "m", "n", "o", "p",
  59. "q", "r", "s", "t", "u", "v", "w", "x",
  60. "y", "z",
  61. "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
  62. "@", "$", "%", "#", "&", "!", "?", "+", "*", "~"
  63. }
  64.  
  65. local function addFruit()
  66. while true do
  67. local x = math.random(1,w)
  68. local y = math.random(2,h)
  69. local fruit = screen[x][y]
  70. if fruit.snake == nil and fruit.wall == nil and fruit.fruit == nil then
  71. screen[x][y] = { fruit = true }
  72. term.setCursorPos(x,y)
  73. term.setBackgroundColour( fruitColour )
  74. term.write(" ")
  75. term.setBackgroundColour( colours.black )
  76. break
  77. end
  78. end
  79.  
  80. nFruit = nFruit + 1
  81. if nFruit > #tFruits then
  82. nFruit = 1
  83. end
  84. end
  85.  
  86. local function drawMenu()
  87. term.setTextColour( headingColour )
  88. term.setCursorPos(1,1)
  89. term.write( "SCORE " )
  90.  
  91. term.setTextColour( textColour )
  92. term.setCursorPos(7,1)
  93. term.write( tostring(nScore) )
  94.  
  95. term.setTextColour( headingColour )
  96. term.setCursorPos(w-11,1)
  97. term.write( "DIFFICULTY ")
  98.  
  99. term.setTextColour( textColour )
  100. term.setCursorPos(w,1)
  101. term.write( tostring(nDifficulty or "?") )
  102.  
  103. term.setTextColour( colours.white )
  104. end
  105.  
  106. local function update( )
  107. local x,y = xPos,yPos
  108. if pxVel and pyVel then
  109. xVel, yVel = pxVel, pyVel
  110. pxVel, pyVel = nil, nil
  111. end
  112.  
  113. -- Remove the tail
  114. if nExtraLength == 0 then
  115. local tail = screen[tailX][tailY]
  116. screen[tailX][tailY] = {}
  117. term.setCursorPos(tailX,tailY)
  118. term.write(" ")
  119. tailX = tail.nextX
  120. tailY = tail.nextY
  121. else
  122. nExtraLength = nExtraLength - 1
  123. end
  124.  
  125. -- Update the head
  126. local head = screen[xPos][yPos]
  127. local newXPos = xPos + xVel
  128. local newYPos = yPos + yVel
  129. if newXPos < 1 then
  130. newXPos = w
  131. elseif newXPos > w then
  132. newXPos = 1
  133. end
  134. if newYPos < 2 then
  135. newYPos = h
  136. elseif newYPos > h then
  137. newYPos = 2
  138. end
  139.  
  140. local newHead = screen[newXPos][newYPos]
  141. term.setCursorPos(1,1);
  142. print( newHead.snake )
  143. if newHead.snake == true or newHead.wall == true then
  144. bRunning = false
  145.  
  146. else
  147. if newHead.fruit == true then
  148. nScore = nScore + 10
  149. nExtraLength = nExtraLength + 1
  150. addFruit()
  151. end
  152. xPos = newXPos
  153. yPos = newYPos
  154. head.nextX = newXPos
  155. head.nextY = newYPos
  156. screen[newXPos][newYPos] = { snake = true }
  157.  
  158. end
  159.  
  160. term.setCursorPos(xPos,yPos)
  161. term.setBackgroundColour( wormColour )
  162. term.write(" ")
  163. term.setBackgroundColour( colours.black )
  164.  
  165. drawMenu()
  166. end
  167.  
  168. -- Display the frontend
  169. term.clear()
  170. local function drawFrontend()
  171. --term.setTextColour( titleColour )
  172. --printCentred( math.floor(h/2) - 4, " W O R M " )
  173.  
  174. term.setTextColour( headingColour )
  175. printCentred( math.floor(h/2) - 3, "" )
  176. printCentred( math.floor(h/2) - 2, " SELECT DIFFICULTY " )
  177. printCentred( math.floor(h/2) - 1, "" )
  178.  
  179. printCentred( math.floor(h/2) + 0, " " )
  180. printCentred( math.floor(h/2) + 1, " " )
  181. printCentred( math.floor(h/2) + 2, " " )
  182. printCentred( math.floor(h/2) - 1 + nDifficulty, " [ ] " )
  183.  
  184. term.setTextColour( textColour )
  185. printCentred( math.floor(h/2) + 0, "EASY" )
  186. printCentred( math.floor(h/2) + 1, "MEDIUM" )
  187. printCentred( math.floor(h/2) + 2, "HARD" )
  188. printCentred( math.floor(h/2) + 3, "" )
  189.  
  190. term.setTextColour( colours.white )
  191. end
  192.  
  193. drawMenu()
  194. drawFrontend()
  195. while true do
  196. local e,key = os.pullEvent( "key" )
  197. if key == keys.up or key == keys.w then
  198. -- Up
  199. if nDifficulty > 1 then
  200. nDifficulty = nDifficulty - 1
  201. drawMenu()
  202. drawFrontend()
  203. end
  204. elseif key == keys.down or key == keys.s then
  205. -- Down
  206. if nDifficulty < 3 then
  207. nDifficulty = nDifficulty + 1
  208. drawMenu()
  209. drawFrontend()
  210. end
  211. elseif key == keys.enter then
  212. -- Enter
  213. break
  214. end
  215. end
  216.  
  217. local tSpeeds = { 5, 10, 25 }
  218. nSpeed = tSpeeds[nDifficulty]
  219. nInterval = 1 / nSpeed
  220.  
  221. -- Grow the snake to its intended size
  222. term.clear()
  223. drawMenu()
  224. screen[tailX][tailY].snake = true
  225. while nExtraLength > 0 do
  226. update()
  227. end
  228. addFruit()
  229. addFruit()
  230.  
  231. -- Play the game
  232. local timer = os.startTimer(0)
  233. while bRunning do
  234. local event, p1, p2 = os.pullEvent()
  235. if event == "timer" and p1 == timer then
  236. timer = os.startTimer(nInterval)
  237. update( false )
  238.  
  239. elseif event == "key" then
  240. local key = p1
  241. if key == keys.up or key == keys.w then
  242. -- Up
  243. if yVel == 0 then
  244. pxVel,pyVel = 0,-1
  245. end
  246. elseif key == keys.down or key == keys.s then
  247. -- Down
  248. if yVel == 0 then
  249. pxVel,pyVel = 0,1
  250. end
  251. elseif key == keys.left or key == keys.a then
  252. -- Left
  253. if xVel == 0 then
  254. pxVel,pyVel = -1,0
  255. end
  256.  
  257. elseif key == keys.right or key == keys.d then
  258. -- Right
  259. if xVel == 0 then
  260. pxVel,pyVel = 1,0
  261. end
  262.  
  263. end
  264. end
  265. end
  266.  
  267. -- Display the gameover screen
  268. term.setTextColour( headingColour )
  269. printCentred( math.floor(h/2) - 2, " " )
  270. printCentred( math.floor(h/2) - 1, " G A M E O V E R " )
  271.  
  272. term.setTextColour( textColour )
  273. printCentred( math.floor(h/2) + 0, " " )
  274. printCentred( math.floor(h/2) + 1, " FINAL SCORE "..nScore.." " )
  275. printCentred( math.floor(h/2) + 2, " " )
  276. term.setTextColour( colours.white )
  277.  
  278. local timer = os.startTimer(2.5)
  279. repeat
  280. local e,p = os.pullEvent()
  281. if e == "timer" and p == timer then
  282. term.setTextColour( textColour )
  283. printCentred( math.floor(h/2) + 2, " PRESS ANY KEY " )
  284. printCentred( math.floor(h/2) + 3, " " )
  285. term.setTextColour( colours.white )
  286. end
  287. until e == "char"
  288.  
  289. term.clear()
  290. term.setCursorPos(1,1)
  291. --custom code to start the os back
  292. shell.run("/.os/gui")
Advertisement
Add Comment
Please, Sign In to add comment