JustDoesGames

WORM

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