Advertisement
Saldor010

Missile Interceptor

Aug 8th, 2017
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. -- Missile Interceptor by Saldor010
  2. -- Patch #1
  3. -- See the forum post below for more details
  4. -- http://www.computercraft.info/forums2/index.php?/topic/28897-missile-interceptor/
  5.  
  6. local GAMEOVER = false
  7. local GAMESTARTED = false
  8. local args = {...}
  9. local difficulty = tonumber(args[1])
  10. if not difficulty then difficulty = 1 end
  11. if difficulty < 1 or difficulty > 2 then difficulty = 1 end
  12.  
  13. if not fs.exists("cobalt") and not args[2] then
  14.     term.setTextColor(colors.red)
  15.     print("Cobalt could not be found on this machine. Cobalt is required to run this game.")
  16.     term.setTextColor(colors.lime)
  17.     print("To download cobalt, please press the Y key now.")
  18.     term.setTextColor(colors.blue)
  19.     print("If you already have cobalt, and we just can't find it, please supply the path as the second argument.")
  20.     local ev,p1,p2,p3,p4,p5 = os.pullEvent("char")
  21.     if string.lower(p1) == "y" then
  22.         shell.run("pastebin","run","h5h4fm3t")
  23.     else
  24.         term.setTextColor(colors.red)
  25.         print("Cancelled installation of Cobalt.")
  26.     end
  27.     error()
  28. end
  29.  
  30. local cobalt = nil
  31. if fs.exists("cobalt") then
  32.     cobalt = dofile("cobalt")
  33. elseif args[2] then
  34.     cobalt = dofile(args[2])
  35. end
  36.  
  37. local smokeGrid = {}
  38. local smokeColors = {
  39.     [4] = colors.yellow,
  40.     [3] = colors.orange,
  41.     [2] = colors.red,
  42.     [1] = colors.gray,
  43. }
  44. local missileGrid = {}
  45. local missileTimer = 1
  46. local tick = 0
  47.  
  48. local points = 0
  49.  
  50. local function spawnMissile()
  51.     local x = math.random(3,49)
  52.     table.insert(missileGrid,{
  53.         ["x"] = x,
  54.         ["y"] = 1,
  55.         ["speed"] = math.random(1)
  56.     })
  57. end
  58.  
  59. function cobalt.update( dt )
  60.     if GAMESTARTED then
  61.         if GAMEOVER then
  62.             GAMEOVER = GAMEOVER - dt
  63.             if GAMEOVER <= 0 then
  64.                 cobalt.exit()
  65.             end
  66.             return
  67.         end
  68.         missileTimer = missileTimer - dt
  69.         tick = tick - dt
  70.        
  71.         if missileTimer <= 0 then
  72.             spawnMissile()
  73.             missileTimer = math.random(1,3)
  74.         end
  75.        
  76.         if tick <= 0 then
  77.             local toRemove = {}
  78.             for k,v in pairs(smokeGrid) do
  79.                 v["ct"] = v["ct"] - 1
  80.                 if v["ct"] <= 0 then
  81.                     toRemove[k] = true
  82.                 end
  83.             end
  84.             for k,v in pairs(toRemove) do
  85.                 table.remove(smokeGrid,k)
  86.             end
  87.            
  88.             for k,v in pairs(missileGrid) do
  89.                 v["y"] = v["y"] + v["speed"]
  90.                
  91.                 for i=1,v["speed"] do
  92.                     table.insert(smokeGrid,{
  93.                         ["x"] = v["x"],
  94.                         ["y"] = v["y"]-i,
  95.                         ["ct"] = 4
  96.                     })
  97.                 end
  98.                
  99.                 if v["y"] >= 19 then
  100.                     GAMEOVER = 2
  101.                     return
  102.                 end
  103.             end
  104.            
  105.             tick = 0.2
  106.         end
  107.     end
  108. end
  109.  
  110. function cobalt.draw()
  111.     if GAMESTARTED then
  112.         for k,v in pairs(missileGrid) do
  113.             if v["speed"] == 2 then
  114.                 cobalt.graphics.print("V",v["x"],v["y"],colors.black,colors.red)
  115.             else
  116.                 cobalt.graphics.print("V",v["x"],v["y"],colors.black,colors.white)
  117.             end
  118.         end
  119.  
  120.         for k,v in pairs(smokeGrid) do
  121.             cobalt.graphics.print("@",v["x"],v["y"],colors.black,smokeColors[v["ct"]] or colors.gray)
  122.         end
  123.        
  124.         if GAMEOVER then
  125.             cobalt.graphics.center("GAME OVER",9,0,20,colors.black,colors.red)
  126.             cobalt.graphics.center("Missiles Intercepted: "..points,11,0,50,colors.black,colors.gray)
  127.             if difficulty == 1 then
  128.                 cobalt.graphics.center("On Easy Difficulty",12,0,50,colors.black,colors.yellow)
  129.             else
  130.                 cobalt.graphics.center("On Hard Difficulty",12,0,50,colors.black,colors.red)
  131.             end
  132.         end
  133.        
  134.         local scoreDigit = 0
  135.         if points and points > 0 then
  136.         scoreDigit = tostring(points)
  137.         scoreDigit = #scoreDigit-1
  138.         end
  139.         --cobalt.graphics.print(math.log(points),1,1,colors.black,colors.white)
  140.         cobalt.graphics.print(points,51-scoreDigit,1,colors.black,colors.gray)
  141.     else
  142.         cobalt.graphics.center("MISSILE INTERCEPTOR",9,0,50,colors.black,colors.orange)
  143.         if difficulty == 2 then
  144.             cobalt.graphics.center("- = HARDCORE = -",10,0,50,colors.black,colors.red)
  145.         end
  146.         cobalt.graphics.center("Press any key to start",10+difficulty,0,50,colors.black,colors.gray)
  147.     end
  148. end
  149.  
  150. local function check(x,y,vx,vy)
  151.     if difficulty == 1 then
  152.         for i=-1,1 do
  153.             for j=-1,1 do
  154.                 if vx == x+i and vy == y+j then return true end
  155.             end
  156.         end
  157.         return false
  158.     elseif difficulty == 2 then
  159.         if vx == x and vy == y then return true else return false end
  160.     end
  161. end
  162.  
  163. function cobalt.mousepressed( x, y, button )
  164.     if not GAMEOVER then
  165.         local toRemove = {}
  166.         for k,v in pairs(missileGrid) do
  167.             if check(v["x"],v["y"],x,y) then
  168.                 points = points + 1
  169.                 toRemove[k] = true
  170.                 for i=-1,1 do
  171.                     if true then --v["x"]+i >= 1 and v["x"]+i <= 51 then
  172.                         for j=-1,1 do
  173.                             if true then --v["y"]+j >= 1 and v["y"]+j <= 19 then
  174.                                 table.insert(smokeGrid,{
  175.                                     ["x"] = v["x"]+i,
  176.                                     ["y"] = v["y"]+j,
  177.                                     ["ct"] = math.random(3,4)
  178.                                 })
  179.                             end
  180.                         end
  181.                     end
  182.                 end
  183.             end
  184.         end
  185.         for k,v in pairs(toRemove) do
  186.             table.remove(missileGrid,k)
  187.         end
  188.     end
  189. end
  190.  
  191. function cobalt.mousereleased( x, y, button )
  192.  
  193. end
  194.  
  195. function cobalt.keypressed( keycode, key )
  196.     if not GAMESTARTED then GAMESTARTED = true end
  197. end
  198.  
  199. function cobalt.keyreleased( keycode, key )
  200.  
  201. end
  202.  
  203. function cobalt.textinput( t )
  204.  
  205. end
  206.  
  207. cobalt.initLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement