Advertisement
incinirate

fd

Jun 5th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.54 KB | None | 0 0
  1. hs=0
  2. cash=0
  3.  
  4. shell.run("clear")
  5. if os.loadAPI(shell.resolve("").."/".."gc") == false then
  6.   print("Unable to load GC api")
  7.   if http then
  8.     write("Install for you? (y/n): ")
  9.     response = read()
  10.     if response == "y" then
  11.       gcapi = http.get("http://pastebin.com/raw.php?i=K7k6MLds")
  12.       if gcapi == nil then
  13.         print("Unable to connect, make sure you are connected to the internet.")
  14.         error()
  15.       end
  16.       gcapitxt = gcapi.readAll()
  17.       gcf = fs.open(shell.resolve("").."/".."gc","w")
  18.       gcf.write(gcapitxt)
  19.       gcf.close()
  20.       print("Done! Please restart now.")
  21.       error()
  22.     else
  23.       print("OK, please install to continue.")
  24.       error()
  25.     end
  26.   else
  27.     print("HTTP API is not enabled, please install gc api manually")
  28.     error()
  29.   end
  30. end
  31. termx,termy = term.getSize()
  32.  
  33. gameRunning = true
  34. objects = {}
  35. gameWidth = 20
  36. gameHeight = termy-3
  37. gameX = termx/2-gameWidth/2
  38. gameY = 1
  39. pathing = 4
  40. cpath = 1
  41. paused = false
  42.  
  43. function rep(str,nm)
  44.   for i=1,nm do
  45.     write(str)
  46.   end
  47. end
  48.  
  49. function clearsc(x1,x2,y1,y2)
  50.   for i=1,y2-y1 do
  51.     rep(" ",x2-x1)
  52.     term.setCursorPos(x1,y1+i)
  53.   end
  54. end
  55.  
  56. function draw()
  57.   term.setBackgroundColor(colors.black)
  58.   clearsc(gameX,gameY,gameX+gameWidth-1,gameY+gameHeight-1)
  59.   shell.run("clear")
  60.   term.setTextColor(colors.blue)
  61.   text("SCORE: "..scr,termx/2,gameY+gameHeight,"middle")
  62.   text("CASH: "..cash,termx/2,gameY+gameHeight+1,"middle")
  63.   text("HIGH SCORE: "..hs,termx/2,gameY+gameHeight+2,"middle")
  64.   term.setBackgroundColor(colors.black)
  65.   term.setCursorPos(gameX + objects.ball.x - 1,gameY + objects.ball.y - 1)
  66.   term.setTextColor(colors.blue)
  67.   write("O")
  68.   for v,k in pairs(objects.blocks) do
  69.     if k.y == gameHeight + gameY - 1 then
  70.     else
  71.       if k.y <= gameY then
  72.       else
  73.         term.setCursorPos(gameX + k.x - 1,gameY + k.y - 1)
  74.         term.setBackgroundColor(colors.yellow)
  75.         write(" ")
  76.       end
  77.     end
  78.   end
  79.   for v,k in pairs(objects.coins) do
  80.     if k.y == gameHeight + gameY - 1 then
  81.     else
  82.       if k.y <=gameY then
  83.       else
  84.         term.setCursorPos(gameX + k.x - 1,gameY + k.y - 1)
  85.         term.setTextColor(colors.yellow)
  86.         term.setBackgroundColor(colors.black)
  87.         write("o")
  88.       end
  89.     end
  90.   end
  91.   gc.drawBoxOutlineBackColor(gameX,gameY,gameWidth,gameHeight,colors.green)
  92. end
  93.  
  94. function initvars()
  95.   objects.ball = {}
  96.   objects.blocks = {}
  97.   objects.coins = {}
  98.   objects.ball.x = math.ceil(gameWidth/2)
  99.   objects.ball.y = 2
  100.   objects.ball.v = 1
  101.   lost = false
  102.   scr = 0
  103.   time = 0
  104. end
  105.  
  106. function slp()
  107.   while time ~= 5 do
  108.     sleep(0.1)
  109.     time = time + 1
  110.   end
  111.   time = 0
  112.   return
  113. end
  114.  
  115. function runActions()
  116.   term.setBackgroundColor(colors.black)
  117.   term.setTextColor(colors.blue)
  118.   text("SCORE: "..scr,termx/2,gameY+gameHeight,"middle")
  119.   text("CASH: "..cash,termx/2,gameY+gameHeight+1,"middle")
  120.   text("HIGH SCORE: "..hs,termx/2,gameY+gameHeight+2,"middle")
  121.   if objects.ball.y == gameY then
  122.     gameRunning = false
  123.     lost = true    
  124.   end
  125.   local dest = nil
  126.   local dest = {}
  127.   for v,k in pairs(objects.blocks) do
  128.     if k ~= nil then
  129.       if k.x == objects.ball.x then
  130.         if k.y == objects.ball.y + 1 then
  131.           objects.ball.y = objects.ball.y - 2
  132.         end
  133.       end
  134.       k.y = k.y - 1
  135.     end
  136.     if k.y < gameY then
  137.       table.insert(dest,k)
  138.     end
  139.   end
  140.   for i=1,#dest do
  141.     objects.blocks[dest[i]]=nil
  142.   end
  143.   if objects.ball.y ~= gameHeight - 1 then
  144.     objects.ball.y = objects.ball.y + objects.ball.v
  145.   end
  146.   local dest=nil
  147.   for v,k in pairs(objects.coins) do
  148.     k.y=k.y-1
  149.     if k ~= nil then
  150.       if k.x == objects.ball.x then
  151.         if k.y == objects.ball.y then
  152.           cash=cash+1
  153.           dest=v
  154.         end
  155.       end
  156.     end
  157.   end
  158.   if dest~=nil then
  159.     objects.coins[dest]=nil
  160.   end
  161.   if cpath == pathing then
  162.     scr = scr+10
  163.     if scr>hs then
  164.       hs=scr
  165.     end
  166.     open = math.random(1,gameWidth-2)
  167.     coin = math.random(1,gameWidth-2)
  168.     table.insert(objects.coins,{["x"]=coin,["y"]=gameHeight-1})
  169.  
  170.     for i=1,gameWidth-2 do
  171.       if i ~= open then
  172.         table.insert(objects.blocks,{})
  173.         for v,k in ipairs(objects.blocks) do
  174.           if k.x == nil then
  175.             k.x = i+1
  176.             k.y = gameHeight
  177.             break
  178.           end
  179.         end
  180.         for v,k in pairs(objects.blocks) do
  181.           if k.y < gameY then
  182.             v = nil
  183.             k = nil
  184.           end
  185.         end
  186.         cpath = 1
  187.       end
  188.     end
  189.   else
  190.     cpath = cpath + 1
  191.   end
  192. end
  193.  
  194. function text(txt,x,y,format)
  195.   format=foramt or "normal"
  196.   formats={
  197.   ["normal"]=function()
  198.     term.setCursorPos(x,y)
  199.   end,
  200.   ["middle"]=function()
  201.     term.setCursorPos(termx/2-(#txt/2),y)
  202.   end,
  203.   }
  204.   formats["middle"]()
  205.   write(txt)
  206. end
  207.  
  208. function key()
  209.   e,par1 = os.pullEvent("key")
  210.   if tonumber(par1) == 203 then
  211.     if objects.ball.x > 2 then
  212.       objects.ball.x = objects.ball.x - 1
  213.     end
  214.     sleep(0.1)
  215.   elseif tonumber(par1) == 205 then
  216.     if objects.ball.x < gameWidth-1 then
  217.       objects.ball.x = objects.ball.x + 1
  218.     end
  219.     sleep(0.1)
  220.   elseif tonumber(par1) == 25 then
  221.     paused = true
  222.   end
  223.   local dest=nil
  224.   for v,k in pairs(objects.coins) do
  225.     if k ~= nil then
  226.       if k.x == objects.ball.x then
  227.         if k.y == objects.ball.y then
  228.           cash=cash+1
  229.           dest=v
  230.         end
  231.       end
  232.     end
  233.   end
  234.   if dest~=nil then
  235.     objects.coins[dest]=nil
  236.   end
  237. end
  238.  
  239. initvars()
  240. draw()
  241. runActions()
  242.  
  243. while gameRunning do
  244.   action = 2
  245.   action = parallel.waitForAny(slp,key)
  246.   if paused then
  247.     term.setCursorPos(gameX+(gameWidth/2)-3,gameY+gameHeight/2-1)
  248.     term.setTextColor(colors.white)
  249.     term.setBackgroundColor(colors.red)
  250.     write("Paused")
  251.     while true do
  252.       e,p1 = os.pullEvent("key")
  253.       if p1 == 25 then
  254.         sleep(0.1)
  255.         paused = false
  256.         break
  257.       end
  258.     end
  259.   end
  260.   draw()
  261.   if action == 1 then runActions() end
  262. end
  263. term.setCursorPos(1,gameY+gameHeight+2)
  264. term.setTextColor(colors.red)
  265. if lost then
  266.   print("YOU LOST! D:")
  267.   local fil=fs.open(shell.dir().."/"..shell.getRunningProgram(),"r")
  268.     local prgm = {}
  269.     while true do
  270.       local ln=fil.readLine()
  271.       if ln==nil then break end
  272.       table.insert(prgm,ln)
  273.     end
  274.     fil.close()
  275.     local fil=fs.open(shell.dir().."/"..shell.getRunningProgram(),"w")
  276.     fil.writeLine("hs="..hs)
  277.   fil.writeLine("cash="..cash)
  278.     for i=3,#prgm do
  279.       fil.writeLine(prgm[i])
  280.     end
  281.     fil.close()
  282. else
  283.   print("Hmm. I think I just crashed, oh well.")
  284. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement