Advertisement
Guest User

WormGame

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