Micheal_Pearce

Worm with CCScores

Dec 8th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.96 KB | None | 0 0
  1.  
  2. function HighScores()
  3.     w,h = term.getSize()
  4.     scores = {}
  5.     if http then
  6.         http.request("http://steamtekk.pchs-players.com/highscore.php?worm|view$")
  7.         local requesting = true
  8.         while requesting do
  9.             local event, url, sourceText = os.pullEvent()
  10.             if event == "http_success" then
  11.                 respondedText = sourceText.readLine()
  12.                 while respondedText do
  13.                     table.insert(scores,respondedText)
  14.                     respondedText = sourceText.readLine()
  15.                 end
  16.                 requesting = false
  17.             elseif event == "http_failure" then
  18.                 scores = {"couldn't reach server"}
  19.                 requesting = false
  20.             end
  21.         end
  22.     else
  23.         scores = {"couldn't reach server"}
  24.     end
  25.     term.clear()
  26.     term.setCursorPos(1,1)
  27.     write("HighScores for worm")
  28.     term.setCursorPos(w-string.len("Hosted by SteamTekk")+1,1)
  29.     write("Hosted by SteamTekk")
  30.     term.setCursorPos(1,2)
  31.     for i = 1,w do
  32.         write("-")
  33.     end
  34.     term.setCursorPos(w-string.len("Hosted by SteamTekk")+1,h)
  35.     write("press enter to exit")
  36.     y = 3
  37.     if #scores ~= 0 then
  38.         u = 10
  39.         if #scores < 10 then
  40.             u = #scores
  41.         end
  42.         for i = 1,u do
  43.             term.setCursorPos(1,y)
  44.             write(i..". "..scores[i])  
  45.             y = y + 1
  46.         end
  47.     else
  48.         term.setCursorPos(1,3)
  49.         print("No scores to get")
  50.     end
  51.     while true do
  52.         _event = os.pullEvent()
  53.         if _event == "key" and p1 == 28 then
  54.             break
  55.         end
  56.     end
  57. end
  58. function uploadscore(Nscore)
  59.     if http then
  60.         os.startTimer(.1)
  61.         letters = {
  62.             "A";
  63.             "B";
  64.             "C";
  65.             "D";
  66.             "E";
  67.             "F";
  68.             "G";
  69.             "H";
  70.             "I";
  71.             "J";
  72.             "K";
  73.             "L";
  74.             "M";
  75.             "N";
  76.             "O";
  77.             "P";
  78.             "Q";
  79.             "R";
  80.             "S";
  81.             "T";
  82.             "U";
  83.             "V";
  84.             "W";
  85.             "X";
  86.             "Y";
  87.             "Z";
  88.         }
  89.         Lsel = 1
  90.         Tname = {[1] = 1,[2] = 1, [3] = 1,[4] = 1}
  91.         while true do
  92.             term.setCursorBlink(true)
  93.             name = letters[Tname[1]]..letters[Tname[2]]..letters[Tname[3]]..letters[Tname[4]]
  94.             term.clear()
  95.             term.setCursorPos(1,1)
  96.             print("name(max of 4 chars)")
  97.             write(name)
  98.             term.setCursorPos(Lsel,2)
  99.             _event,p1 = os.pullEvent()
  100.             if _event == "key" and p1 == 205 then
  101.                 Lsel = Lsel + 1
  102.                 if Lsel > 4 then
  103.                     Lsel = 4
  104.                 end
  105.             elseif _event == "key" and p1 == 203 then
  106.                 Lsel = Lsel - 1
  107.                 if Lsel == 0 then
  108.                     Lsel = 1
  109.                 end
  110.             elseif _event == "key" and p1 == 200 then
  111.                 Tname[Lsel] = Tname[Lsel] - 1
  112.                 if Tname[Lsel] == 0 then
  113.                     Tname[Lsel] = #letters
  114.                 end
  115.             elseif _event == "key" and p1 == 208 then
  116.                 Tname[Lsel] = Tname[Lsel] + 1
  117.                 if Tname[Lsel] > #letters then
  118.                     Tname[Lsel] = 1
  119.                 end
  120.             elseif _event == "key" and p1 == 28 then
  121.                 term.setCursorBlink(false)
  122.                 break
  123.             end
  124.         end
  125.         term.setCursorPos(1,1)
  126.         http.request("http://steamtekk.pchs-players.com/highscore.php?worm|post$"..name.."("..Nscore)
  127.         local requesting = true
  128.         while requesting do
  129.             local event, url, sourceText = os.pullEvent()
  130.             if event == "http_success" then
  131.                 local respondedText = sourceText.readLine()
  132.                 while respondedText do
  133.                     table.insert(scores,respondedText)
  134.                     respondedText = sourceText.readLine()
  135.                 end
  136.                 requesting = false
  137.             elseif event == "http_failure" then
  138.                 scores = {"couldn't reach server"}
  139.                 requesting = false
  140.             end
  141.         end
  142.     else
  143.         print("unable to connect to the internet")
  144.         _event = os.pullEvent()
  145.     end
  146. end
  147. -- Display the start screen
  148. local w,h = term.getSize()
  149.  
  150. local headingColour, textColour, wormColour, fruitColour
  151. if term.isColour() then
  152.     headingColour = colours.yellow
  153.     textColour = colours.white
  154.     wormColour = colours.lime
  155.     fruitColour = colours.red
  156. else
  157.     headingColour = colours.white
  158.     textColour = colours.white
  159.     wormColour = colours.white
  160.     fruitColour = colours.white
  161. end
  162.  
  163. function printCentred( y, s )
  164.     local x = math.floor((w - string.len(s)) / 2)
  165.     term.setCursorPos(x,y)
  166.     --term.clearLine()
  167.     term.write( s )
  168. end
  169.  
  170. local xVel,yVel = 1,0
  171. local xPos, yPos = math.floor(w/2), math.floor(h/2)
  172. local pxVel, pyVel = nil, nil
  173.  
  174. local nLength = 1
  175. local nExtraLength = 6
  176. local bRunning = true
  177.  
  178. local tailX,tailY = xPos,yPos
  179. local nScore = 0
  180. local nDifficulty = 2
  181. local nSpeed, nInterval
  182.  
  183. -- Setup the screen
  184. local screen = {}
  185. for x=1,w do
  186.     screen[x] = {}
  187.     for y=1,h do
  188.         screen[x][y] = {}
  189.     end
  190. end
  191. screen[xPos][yPos] = { snake = true }
  192.  
  193. local nFruit = 1
  194. local tFruits = {
  195.     "A", "B", "C", "D", "E", "F", "G", "H",
  196.     "I", "J", "K", "L", "M", "N", "O", "P",
  197.     "Q", "R", "S", "T", "U", "V", "W", "X",
  198.     "Y", "Z",
  199.     "a", "b", "c", "d", "e", "f", "g", "h",
  200.     "i", "j", "k", "l", "m", "n", "o", "p",
  201.     "q", "r", "s", "t", "u", "v", "w", "x",
  202.     "y", "z",
  203.     "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
  204.     "@", "$", "%", "#", "&", "!", "?", "+", "*", "~"
  205. }
  206.  
  207. local function addFruit()
  208.     while true do
  209.         local x = math.random(1,w)
  210.         local y = math.random(2,h)
  211.         local fruit = screen[x][y]
  212.         if fruit.snake == nil and fruit.wall == nil and fruit.fruit == nil then
  213.             screen[x][y] = { fruit = true }
  214.             term.setCursorPos(x,y)
  215.             term.setBackgroundColour( fruitColour )
  216.             term.write(" ")
  217.             term.setBackgroundColour( colours.black )
  218.             break
  219.         end
  220.     end
  221.    
  222.     nFruit = nFruit + 1
  223.     if nFruit > #tFruits then
  224.         nFruit = 1
  225.     end
  226. end
  227.  
  228. local function drawMenu()
  229.     term.setTextColour( headingColour )
  230.     term.setCursorPos(1,1)
  231.     term.write( "SCORE " )
  232.    
  233.     term.setTextColour( textColour )
  234.     term.setCursorPos(7,1)
  235.     term.write( tostring(nScore) )
  236.  
  237.     term.setTextColour( headingColour )
  238.     term.setCursorPos(w-11,1)
  239.     term.write( "DIFFICULTY ")
  240.  
  241.     term.setTextColour( textColour )
  242.     term.setCursorPos(w,1)
  243.     term.write( tostring(nDifficulty or "?") )
  244.  
  245.     term.setTextColour( colours.white )
  246. end
  247.  
  248. local function update( )
  249.     local x,y = xPos,yPos
  250.     if pxVel and pyVel then
  251.         xVel, yVel = pxVel, pyVel
  252.         pxVel, pyVel = nil, nil
  253.     end
  254.  
  255.     -- Remove the tail
  256.     if nExtraLength == 0 then
  257.         local tail = screen[tailX][tailY]
  258.         screen[tailX][tailY] = {}
  259.         term.setCursorPos(tailX,tailY)
  260.         term.write(" ")
  261.         tailX = tail.nextX
  262.         tailY = tail.nextY
  263.     else
  264.         nExtraLength = nExtraLength - 1
  265.     end
  266.    
  267.     -- Update the head
  268.     local head = screen[xPos][yPos]
  269.     local newXPos = xPos + xVel
  270.     local newYPos = yPos + yVel
  271.     if newXPos < 1 then
  272.         newXPos = w
  273.     elseif newXPos > w then
  274.         newXPos = 1
  275.     end
  276.     if newYPos < 2 then
  277.         newYPos = h
  278.     elseif newYPos > h then
  279.         newYPos = 2
  280.     end
  281.    
  282.     local newHead = screen[newXPos][newYPos]
  283.     term.setCursorPos(1,1);
  284.     print( newHead.snake )
  285.     if newHead.snake == true or newHead.wall == true then
  286.         bRunning = false
  287.        
  288.     else
  289.         if newHead.fruit == true then
  290.             nScore = nScore + 10
  291.             nExtraLength = nExtraLength + 1
  292.             addFruit()
  293.         end
  294.         xPos = newXPos
  295.         yPos = newYPos
  296.         head.nextX = newXPos
  297.         head.nextY = newYPos
  298.         screen[newXPos][newYPos] = { snake = true }
  299.        
  300.     end
  301.    
  302.     term.setCursorPos(xPos,yPos)
  303.     term.setBackgroundColour( wormColour )
  304.     term.write(" ")
  305.     term.setBackgroundColour( colours.black )
  306.  
  307.     drawMenu()
  308. end
  309.  
  310. -- Display the frontend
  311. term.clear()
  312. local function drawFrontend()
  313.     term.setTextColour( headingColour )
  314.     printCentred( math.floor(h/2) - 3, "" )
  315.     printCentred( math.floor(h/2) - 2, " SELECT DIFFICULTY " )
  316.     printCentred( math.floor(h/2) - 1, "" )
  317.    
  318.     printCentred( math.floor(h/2) + 0, "            " )
  319.     printCentred( math.floor(h/2) + 1, "            " )
  320.     printCentred( math.floor(h/2) + 2, "            " )
  321.     printCentred( math.floor(h/2) - 1 + nDifficulty, " [        ] " )
  322.  
  323.     term.setTextColour( textColour )
  324.     printCentred( math.floor(h/2) + 0, "EASY" )
  325.     printCentred( math.floor(h/2) + 1, "MEDIUM" )
  326.     printCentred( math.floor(h/2) + 2, "HARD" )
  327.     printCentred( math.floor(h/2) + 3, "" )
  328.  
  329.     term.setTextColour( colours.white )
  330. end
  331.  
  332. drawMenu()
  333. drawFrontend()
  334. while true do
  335.     local e,key = os.pullEvent( "key" )
  336.     if key == keys.up or key == keys.w then
  337.         -- Up
  338.         if nDifficulty > 1 then
  339.             nDifficulty = nDifficulty - 1
  340.             drawMenu()
  341.             drawFrontend()
  342.         end
  343.     elseif key == keys.down or key == keys.s then
  344.         -- Down
  345.         if nDifficulty < 3 then
  346.             nDifficulty = nDifficulty + 1
  347.             drawMenu()
  348.             drawFrontend()
  349.         end
  350.     elseif key == keys.enter then
  351.         -- Enter
  352.         break
  353.     end
  354. end
  355.  
  356. local tSpeeds = { 5, 10, 25 }
  357. nSpeed = tSpeeds[nDifficulty]
  358. nInterval = 1 / nSpeed
  359.  
  360. -- Grow the snake to its intended size
  361. term.clear()
  362. drawMenu()
  363. screen[tailX][tailY].snake = true
  364. while nExtraLength > 0 do
  365.     update()
  366. end
  367. addFruit()
  368. addFruit()
  369.  
  370. -- Play the game
  371. local timer = os.startTimer(0)
  372. while bRunning do
  373.     local event, p1, p2 = os.pullEvent()
  374.     if event == "timer" and p1 == timer then
  375.         timer = os.startTimer(nInterval)
  376.         update( false )
  377.    
  378.     elseif event == "key" then
  379.         local key = p1
  380.         if key == keys.up or key == keys.w then
  381.             -- Up
  382.             if yVel == 0 then
  383.                 pxVel,pyVel = 0,-1
  384.             end
  385.         elseif key == keys.down or key == keys.s then
  386.             -- Down
  387.             if yVel == 0 then
  388.                 pxVel,pyVel = 0,1
  389.             end
  390.         elseif key == keys.left or key == keys.a then
  391.             -- Left
  392.             if xVel == 0 then
  393.                 pxVel,pyVel = -1,0
  394.             end
  395.        
  396.         elseif key == keys.right or key == keys.d then
  397.             -- Right
  398.             if xVel == 0 then
  399.                 pxVel,pyVel = 1,0
  400.             end
  401.        
  402.         end
  403.     end
  404. end
  405.  
  406. -- Display the gameover screen
  407. term.setTextColour( headingColour )
  408. printCentred( math.floor(h/2) - 2, "                   " )
  409. printCentred( math.floor(h/2) - 1, " G A M E   O V E R " )
  410.  
  411. term.setTextColour( textColour )
  412. printCentred( math.floor(h/2) + 0, "                 " )
  413. printCentred( math.floor(h/2) + 1, " FINAL SCORE "..nScore.." " )
  414. printCentred( math.floor(h/2) + 2, "                 " )
  415. term.setTextColour( colours.white )
  416.  
  417. local timer = os.startTimer(2.5)
  418. Q = true
  419. while Q do
  420.     local e,p = os.pullEvent()
  421.     if e == "timer" and p == timer then
  422.         term.setTextColour( textColour )
  423.         printCentred( math.floor(h/2) + 2, " PRESS P TO POST SCORE " )
  424.         printCentred( math.floor(h/2) + 3, " PRESS ENTER TO SEE HIGHSCORES" )
  425.         term.setTextColour( colours.white )
  426.     elseif e == "key" and p == 25 then
  427.         Q = false
  428.         uploadscore(nScore)
  429.     elseif e == "key" and p == 28 then
  430.         Q = false
  431.     end
  432. end
  433. HighScores()
  434. term.clear()
  435. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment