Belzebub

ComputerCraft WORM

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