Advertisement
Guest User

wormCZ

a guest
May 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.80 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.blue
  9.     textColour = colours.blue
  10.     wormColour = colours.red
  11.     fruitColour = colours.white
  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( "Obtiznost ")
  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) - 4, "" )
  175.     printCentred( math.floor(h/2) - 3, " Vyberte Obtiznost " )
  176.     printCentred( math.floor(h/2) - 2, " Preprogramoval : y45xp" )
  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, "Lehka" )
  186.     printCentred( math.floor(h/2) + 1, "Normalka" )
  187.     printCentred( math.floor(h/2) + 2, "Tezka" )
  188.     printCentred( math.floor(h/2) + 3, "Po Cesku" )
  189.  printCentred( math.floor(h/2) + 4, "" )
  190.  
  191.     term.setTextColour( colours.white )
  192. end
  193.  
  194. drawMenu()
  195. drawFrontend()
  196. while true do
  197.     local e,key = os.pullEvent( "key" )
  198.     if key == keys.up or key == keys.w then
  199.         -- Up
  200.         if nDifficulty > 1 then
  201.             nDifficulty = nDifficulty - 1
  202.             drawMenu()
  203.             drawFrontend()
  204.         end
  205.     elseif key == keys.down or key == keys.s then
  206.         -- Down
  207.         if nDifficulty < 3 then
  208.             nDifficulty = nDifficulty + 1
  209.             drawMenu()
  210.             drawFrontend()
  211.         end
  212.     elseif key == keys.enter then
  213.         -- Enter
  214.         break
  215.     end
  216. end
  217.  
  218. local tSpeeds = { 5, 10, 25, 3 }
  219. nSpeed = tSpeeds[nDifficulty]
  220. nInterval = 1 / nSpeed
  221.  
  222. -- Grow the snake to its intended size
  223. term.clear()
  224. drawMenu()
  225. screen[tailX][tailY].snake = true
  226. while nExtraLength > 2 do
  227.     update()
  228. end
  229. addFruit()
  230. addFruit()
  231.  
  232. -- Play the game
  233. local timer = os.startTimer(0)
  234. while bRunning do
  235.     local event, p1, p2 = os.pullEvent()
  236.     if event == "timer" and p1 == timer then
  237.         timer = os.startTimer(nInterval)
  238.         update( false )
  239.    
  240.     elseif event == "key" then
  241.         local key = p1
  242.         if key == keys.up or key == keys.w then
  243.             -- Up
  244.             if yVel == 0 then
  245.                 pxVel,pyVel = 0,-1
  246.             end
  247.         elseif key == keys.down or key == keys.s then
  248.             -- Down
  249.             if yVel == 0 then
  250.                 pxVel,pyVel = 0,1
  251.             end
  252.         elseif key == keys.left or key == keys.a then
  253.             -- Left
  254.             if xVel == 0 then
  255.                 pxVel,pyVel = -1,0
  256.             end
  257.        
  258.         elseif key == keys.right or key == keys.d then
  259.             -- Right
  260.             if xVel == 0 then
  261.                 pxVel,pyVel = 1,0
  262.             end
  263.        
  264.         end
  265.     end
  266. end
  267.  
  268. -- Display the gameover screen
  269. term.setTextColour( headingColour )
  270. printCentred( math.floor(h/2) - 2, "                   " )
  271. printCentred( math.floor(h/2) - 1, " K O N E C   H R Y " )
  272.  
  273. term.setTextColour( textColour )
  274. printCentred( math.floor(h/2) + 0, "                 " )
  275. printCentred( math.floor(h/2) + 1, " FINALNI SCORE "..nScore.." " )
  276. printCentred( math.floor(h/2) + 2, "                 " )
  277. term.setTextColour( colours.white )
  278.  
  279. local timer = os.startTimer(2.5)
  280. repeat
  281.     local e,p = os.pullEvent()
  282.     if e == "timer" and p == timer then
  283.         term.setTextColour( textColour )
  284.         printCentred( math.floor(h/2) + 2, " Zmacknete Libovolnu Klavesu " )
  285.         printCentred( math.floor(h/2) + 3, "               " )
  286.         term.setTextColour( colours.white )
  287.     end
  288. until e == "char"
  289.  
  290. term.clear()
  291. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement