Advertisement
Virgilcore

RobcOS R-Snake

Jul 9th, 2022
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 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.green
  8.     headingColour = colours.green
  9.     textColour = colours.green
  10.     wormColour = colours.green
  11.     fruitColour = colours.lime
  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. local 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.setBackgroundColor(colors.black)
  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.     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)
  289.  
  290.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement