Davidgumazon03

Snake [GAME]

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