Advertisement
Guest User

spwaves

a guest
Apr 19th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.13 KB | None | 0 0
  1. --Tank
  2. --[[ Info:
  3.   Welcome to World of Tanks. Your objective is to
  4.   eliminate all tanks from the playing field.
  5.  
  6.   Enemys:
  7.   Red - Moves
  8.   Green - Shoots at you
  9.   Orange - Shoots and moves
  10.  
  11.   Help:
  12.   Ammo Packs
  13.   Health Packs
  14.   Extra Support (Not Yet Implemented)
  15.  
  16.   Information about the player.stat file:
  17.   The player.stat file holds the following info
  18.   in the file in the following order:
  19.     Name
  20.     Campaign Level
  21.     Highscore
  22.     Total Lifetime Shots
  23.     Total Lifetime Enemies Killed
  24.     Total Lifetime Deaths
  25.     Wave Number
  26. ]]--
  27.  
  28. local w,h = term.getSize()
  29.  
  30. running = true
  31.  
  32. refresh = 0.10
  33. gtId = os.startTimer(refresh)
  34.  
  35. brefresh = 1.5
  36. gtId2 = os.startTimer(brefresh)
  37.  
  38. brand = math.random(0.10, 1.5)
  39. gtId3 = os.startTimer(brand)
  40.  
  41. hrefresh = 3
  42. gtId4 = os.startTimer(hrefresh)
  43.  
  44. levelprogress = 1
  45. tries = 1
  46.  
  47. --Opens the player stat file--
  48. h = fs.open("Tanks/player.stat", "r")
  49. name = h.readLine()
  50. level = h.readLine()
  51. highscore = h.readLine()
  52. tlbullets = h.readLine()
  53. tlenemies = h.readLine()
  54. tldeaths = h.readLine()
  55. wavenum = h.readLine()
  56.  
  57. lbullets = tlbullets
  58. lenemies = tlenemies
  59. ldeaths = tldeaths
  60.  
  61. local w,h = term.getSize()
  62.  
  63. wave = 1
  64.  
  65. tankInfo = {
  66.   x = 22,
  67.   y = h - 5,
  68.   hp = 15,
  69.   maxhp = 15,
  70.   ammo = 10
  71. }
  72.  
  73. ammoamount = tostring(tankInfo.ammo)
  74.  
  75. bullets = { }
  76. ent = { }
  77. ammopack = { }
  78. healthpack = { }
  79.  
  80. bulletColor = {
  81.   colors.white,
  82.   colors.gray,
  83.   colors.red,
  84.   colors.green,
  85.   colors.orange
  86. }
  87.  
  88. tankBullet = bulletColor[1]
  89. enemyBullet = bulletColor[4]
  90. ent3Bullet = bulletColor[5]
  91.  
  92. function createBullets(x, y, xvel, yvel, typeBullets, bdmg)
  93.   table.insert(bullets, {
  94.     x = x,
  95.     y = y,
  96.     xstep = xvel,
  97.     ystep = yvel,
  98.     type = typeBullets,
  99.     dmg = bdmg
  100.    })
  101. end
  102.  
  103. function createEnt(x, y, typeEnt, entNumber, xvel, yvel, entId)
  104.   table.insert(ent, {
  105.   x = x,
  106.   y = y,
  107.   color = typeEnt,
  108.   newEnt = entNumber,
  109.   xstep = xvel,
  110.   ystep = yvel,
  111.   ID = entId
  112.   })
  113. end
  114.  
  115. function createAmmo(x, y, amount)
  116.   table.insert(ammopack, {
  117.   x = x,
  118.   y = y,
  119.   amount = amount
  120.   })
  121. end
  122.  
  123. function createHPack(x, y, amount)
  124.   table.insert(healthpack, {
  125.   x = x,
  126.   y = y,
  127.   amount = amount
  128.   })
  129. end
  130.  
  131. function tank()
  132. term.setTextColor(colors.white)
  133.   term.setCursorPos(tankInfo.x, tankInfo.y)
  134.   print(" | ")
  135.   term.setCursorPos(tankInfo.x, tankInfo.y + 1)
  136.   print("*-*")
  137.   term.setCursorPos(tankInfo.x, tankInfo.y + 2)
  138.   print("| |")
  139.   term.setCursorPos(tankInfo.x, tankInfo.y + 3)
  140.   print("*-*")
  141. end
  142.  
  143. function enemy()
  144. for _, v in pairs(ent) do
  145. term.setTextColor(v.color)
  146.   term.setCursorPos(v.x, v.y)
  147.   print(" | ")
  148.   term.setCursorPos(v.x, v.y - 1)
  149.   print("*-*")
  150.   term.setCursorPos(v.x, v.y - 2)
  151.   print("| |")
  152.   term.setCursorPos(v.x, v.y - 3)
  153.   print("*-*")
  154.  end
  155. end
  156.  
  157. function ammoPack()
  158. for _, v in pairs(ammopack) do
  159.   term.setTextColor(colors.yellow)
  160.   term.setCursorPos(v.x, v.y)
  161.   print("[]")
  162. end  
  163. end
  164.  
  165. function healthPack()
  166. for _, v in pairs(healthpack) do
  167.   term.setTextColor(colors.red)
  168.   term.setCursorPos(v.x, v.y)
  169.   print("[]")
  170. end
  171. end
  172.  
  173. function updateEnt()
  174.   if #ent > 0 then
  175.     for i = 1, #ent do
  176.       if ent[i] then
  177.         local ents = ent[i]
  178.         if ents.ID == 1 then
  179.           moveEnemy1(i, 5)
  180.         elseif ents.ID == 3 then
  181.           moveEnemy1(i, 10)
  182.         end
  183.       end
  184.     end
  185.   end
  186. end
  187.  
  188. function updateEnt2()
  189.   if #ent > 0 then
  190.     for i = 1, #ent do
  191.       if ent[i] then
  192.         local ents = ent[i]
  193.         if ents.ID == 2 then
  194.           enemyBullets(i)
  195.         end
  196.       end
  197.     end
  198.   end
  199. end
  200.  
  201. function updateEnt3()
  202.   if #ent > 0 then
  203.     for i = 1, #ent do
  204.       if ent[i] then
  205.         local ents = ent[i]
  206.         if ents.ID == 3 then
  207.           enemy3Bullets(i)
  208.         end
  209.       end
  210.     end
  211.   end
  212. end
  213.  
  214.  
  215. function checkForCollison()
  216.   if #ent > 0 then
  217.     for i = 1, #ent do
  218.       if ent[i] then
  219.         local ents = ent[i]
  220.         if (ents.x <= tankInfo.x + 2 and ents.x + 2 >= tankInfo.x) and (ents.x + 2 <= tankInfo.x + 2 and ents.x >= tankInfo.x)
  221.          and (ents.y >= tankInfo.y and ents.y + 3 <= tankInfo.y - 3) then
  222.           running = false
  223.           endGame("Tip: You should not run head first into battle")
  224.         end
  225.       end
  226.     end
  227.   end
  228. end
  229.  
  230. function updateAmmo()
  231.   for _, v in pairs(ammopack) do
  232.     if v.x > tankInfo.x then
  233.       if tankInfo.x + 2 >= v.x and v.y >= tankInfo.y and v.y <= tankInfo.y + 2 then
  234.         tankInfo.ammo = tankInfo.ammo + v.amount
  235.         table.remove(ammopack, 1)
  236.         break
  237.       end
  238.     elseif v.x < tankInfo.x then
  239.       if tankInfo.x <= v.x and v.y >= tankInfo.y and v.y <= tankInfo.y + 2 then
  240.         tankInfo.ammo = tankInfo.ammo + v.amount
  241.         table.remove(ammopack, 1)
  242.         break
  243.       end
  244.     end
  245.   end
  246. end
  247.  
  248. function updateHealth()
  249.   for _, v in pairs(healthpack) do
  250.     if v.x > tankInfo.x then
  251.       if tankInfo.x + 2 >= v.x and v.y >= tankInfo.y and v.y <= tankInfo.y + 2 then
  252.         tankInfo.hp = tankInfo.hp + v.amount
  253.         table.remove(healthpack, 1)
  254.         break
  255.       end
  256.     elseif v.x < tankInfo.x then
  257.       if tankInfo.x <= v.x and v.y >= tankInfo.y and v.y <= tankInfo.y + 2 then
  258.         tankInfo.hp = tankInfo.hp + v.amount
  259.         table.remove(healthpack, 1)
  260.         break
  261.       end
  262.     end
  263.   end
  264. end
  265.  
  266. function endGame(deathmsg)
  267.     local select = 1
  268.   function optionsdraw()
  269.     term.clear()
  270.     term.setTextColor(colors.black)
  271.     term.setBackgroundColor(colors.white)
  272.     term.clear()
  273.     bg = paintutils.loadImage("Tanks/bgt")
  274.     paintutils.drawImage(bg, 1,1)
  275.     term.setBackgroundColor(colors.white)
  276.     term.setCursorPos(w/2 - 4, 11)
  277.     print("GAME OVER")
  278.     term.setCursorPos(4, 13)
  279.     print(deathmsg)
  280.     term.setCursorPos(w/2 - 8, 15)
  281.     print("Retry Last Level")
  282.     term.setCursorPos(w/2 - 4, 17)
  283.     print("Quit Game")
  284.     term.setTextColor(colors.black)
  285.       if select == 1 then
  286.         term.setCursorPos(w/2 - 9, 15)
  287.         print("[")
  288.         term.setCursorPos(w/2 + 8, 15)
  289.         print("]")
  290.       else
  291.         term.setCursorPos(w/2 - 5, 17)
  292.         print("[")
  293.         term.setCursorPos(w/2 + 5, 17)
  294.         print("]")
  295.       end
  296.   end
  297.   optionsdraw()
  298.     while true do
  299.       local event, p1 = os.pullEventRaw()
  300.         if event == "key" then
  301.           if p1 == keys.w or p1 == keys.s or p1 == keys.up or p1 == keys.down then
  302.             select = -select
  303.             optionsdraw()
  304.           elseif p1 == keys.enter or p1 == keys.space then
  305.             if select == 1 and tries <= 2 then
  306.               running = true
  307.               tankInfo.x = w/2
  308.               tankInfo.y = h - 5
  309.               tankInfo.hp = tankInfo.maxhp
  310.               tankInfo.ammo = 10
  311.               term.setBackgroundColor(colors.black)
  312.               term.setTextColor(colors.white)
  313.                 for i = 1, #bullets do
  314.                   table.remove(bullets, i)
  315.                 end
  316.                 for i = 1, #healthpack do
  317.                   table.remove(healthpack, i)
  318.                 end
  319.                 gtId = os.startTimer(refresh)
  320.                 gtId2 = os.startTimer(brefresh)
  321.                 gtId3 = os.startTimer(brand)
  322.                 gtId4 = os.startTimer(hrefresh)
  323.               drawBg()
  324.               tries = tries + 1
  325.               ldeaths = ldeaths + 1
  326.               break  
  327.             else
  328.             ldeaths = ldeaths + 1
  329.             w = fs.open("Tanks/player.stat", "w")
  330.             w.writeLine(name)
  331.             w.writeLine(levelprogress)
  332.             w.writeLine(highscore)
  333.             w.writeLine(lbullets)
  334.             w.writeLine(lenemies)
  335.             w.writeLine(ldeaths)
  336.             w.writeLine(wavenum)
  337.             w.flush()
  338.             w.close()
  339.               term.clear()
  340.               term.setTextColor(colors.white)
  341.               term.setBackgroundColor(colors.black)
  342.               term.clear()
  343.               term.setCursorPos(1,1)
  344.               print("Thank you for playing")
  345.               return
  346.             end
  347.           end
  348.         end
  349.     end
  350.   end
  351.  
  352. function drawBg()
  353. term.clear()
  354.   for i = 1, w do
  355.   term.setTextColor(colors.white)
  356.     term.setCursorPos(w - (i), h - 1)
  357.     write("-")
  358.   end
  359.   for i = 1, h do
  360.     term.setTextColor(colors.white)
  361.       term.setCursorPos(5, h - (i))
  362.       write("|")
  363.   end
  364.   function hpbar()
  365.   for i = 1, (tankInfo.hp/tankInfo.maxhp)*5 do
  366.     term.setBackgroundColor(colors.red)
  367.     term.setCursorPos(2, 15 - (i))
  368.     write("  ")
  369.     term.setBackgroundColor(colors.black)
  370.   end
  371.   end
  372.   term.setCursorPos(1, 9)
  373.   print("*--*")
  374.   for i = 1, 5 do
  375.     term.setTextColor(colors.white)
  376.     term.setCursorPos(1, 15 - (i))
  377.     write("|  |")
  378.     hpbar()
  379.   end
  380.   term.setCursorPos(1,15)
  381.   print("*--*")
  382.   term.setCursorPos(1,2)
  383.   print("Tank")
  384.   print("    ")
  385.   print("Ammo")
  386.   print(tankInfo.ammo)
  387.   print("    ")
  388.   print("HP ")
  389.   term.setCursorPos(2,8)
  390.   print(tankInfo.hp)
  391. term.setCursorPos(1,h)
  392. print("Tank Info -     Name: "..name.." Wave: "..wave)
  393. tank()
  394.  
  395.   for _, v in pairs(bullets) do
  396.     term.setCursorPos(v.x, v.y)
  397.     term.setTextColor(v.type)
  398.     term.write(".")
  399.     term.setTextColor(colors.black)
  400.   end
  401.   --Draws all the desired enemies on the screen
  402.     enemy()
  403.     ammoPack()
  404.     healthPack()
  405. end
  406.  
  407. function createNewWave()
  408. wave = wave + 1
  409. wavenum = wave
  410. entrand = math.random(1,8)
  411.  for i = 1, entrand do
  412.    entnum = 1
  413.    idrand = math.random(1,3)
  414.    xrand = math.random(5, 40)
  415.      if idrand == 1 then
  416.      xrand = math.random(5, 40)
  417.        createEnt(xrand, 5, colors.red, entnum, 1,0,1)
  418.      elseif idrand == 2 then
  419.        createEnt(xrand, 10, colors.green, entnum, 0,0,2)
  420.      elseif idrand == 3 then
  421.        createEnt(xrand, 10, colors.orange, entnum, 1,0,3)
  422.      end
  423.      entnum = entnum + 1
  424.  end
  425. end
  426.  
  427. tank()
  428. drawBg()
  429.   enemy()
  430.  
  431. function input()
  432. local event, key = os.pullEvent()
  433.   if event == "key" then
  434.     if key == keys.q then
  435.     running = false
  436.     elseif key == keys.w then
  437.       tankInfo.y = tankInfo.y - 1
  438.     elseif key == keys.s then
  439.       tankInfo.y = tankInfo.y + 1
  440.     elseif key == keys.a then
  441.       tankInfo.x = tankInfo.x - 1
  442.     elseif key == keys.d then
  443.       tankInfo.x = tankInfo.x + 1
  444.     elseif key == keys.up then
  445.       tankInfo.y = tankInfo.y - 1
  446.     elseif key == keys.down then
  447.       tankInfo.y = tankInfo.y + 1
  448.     elseif key == keys.left then
  449.       tankInfo.x = tankInfo.x - 1
  450.     elseif key == keys.right then
  451.       tankInfo.x = tankInfo.x + 1
  452.     elseif key == keys.space then
  453.       if tankInfo.ammo > 0 then
  454.         createBullets(tankInfo.x + 1, tankInfo.y - 1, 0, -1, tankBullet,0)
  455.         tankInfo.ammo = tankInfo.ammo - 1
  456.         lbullets = lbullets + 1
  457.       end
  458.       drawBg()
  459.       tank()
  460.         enemy()
  461.         updateBullets()
  462.         checkForCollison()
  463.         updateAmmo()
  464.     end
  465.     term.setBackgroundColor(colors.black)
  466.     term.setCursorPos(tankInfo.x, tankInfo.y)
  467.     term.clearLine()
  468.     term.setCursorPos(tankInfo.x, tankInfo.y + 1)
  469.     term.clearLine()
  470.     term.setCursorPos(tankInfo.x, tankInfo.y + 2)
  471.     term.clear()
  472.     tank()
  473.     drawBg()
  474.     if tankInfo.x == w - 3 then
  475.       tankInfo.x = 22
  476.       tankInfo.y = h - 5
  477.     elseif tankInfo.x == 5 then
  478.       tankInfo.x = 22
  479.       tankInfo.y = h - 5
  480.     elseif tankInfo.y == h - 3 then
  481.       tankInfo.x = 22
  482.       tankInfo.y = h - 5
  483.     elseif tankInfo.y == 1 then
  484.       tankInfo.x = 22
  485.       tankInfo.y = h - 5
  486.     end
  487.   elseif event == "timer" then
  488.     if key == gtId then
  489.       updateBullets()
  490.       updateEnt()
  491.       updateAmmo()
  492.       updateHealth()
  493.       checkForCollison()
  494.       gtId = os.startTimer(refresh)
  495.       drawBg()
  496.       if tankInfo.hp <= 0 then
  497.         running = false
  498.         endGame("You kind of got shot")
  499.       end
  500.       if levelprogress == 14 then
  501.         createAmmo(20, 20, 20)
  502.       elseif levelprogress == 16 then
  503.         createAmmo(20, 20, 20)
  504.       else
  505.         if tankInfo.ammo <= 3 then
  506.           if #ammopack == 0 then
  507.           xrand = math.random(7,48)
  508.           yrand = math.random(2,h-5)
  509.           arand = math.random(0,20)
  510.           if arand > 0 then
  511.             createAmmo(xrand, yrand, arand)
  512.           elseif arand == 0 then
  513.             running = false
  514.             endGame("You took all the bullets from your camp")
  515.           end
  516.           end
  517.         end
  518.       end
  519.     elseif key == gtId2 then
  520.       updateEnt2()
  521.       gtId2 = os.startTimer(brefresh)
  522.       if #ent <= 0 then
  523.         createNewWave()
  524.       end
  525.     elseif key == gtId3 then
  526.       updateEnt3()
  527.       brand = math.random(0.10, 2.5)
  528.       gtId3 = os.startTimer(brand)
  529.     elseif key == gtId4 then
  530.       updateHealth()
  531.       if levelprogress == 14 then
  532.         if tankInfo.hp <= 5 then
  533.           createHPack(10, 10, 10)
  534.         elseif tankInfo.hp == 10 then
  535.           createHPack(10, 10, 5)
  536.         end
  537.       elseif levelprogress == 16 then
  538.         if tankInfo.hp <= 5 then
  539.           createHPack(10, 10, 10)
  540.         elseif tankInfo.hp == 10 then
  541.           createHPack(10, 10, 5)
  542.         end
  543.       else
  544.       if tankInfo.hp <= 5 then
  545.         chances = math.random(0, 50)
  546.         if #healthpack == 0 then
  547.           if chances < 20 then
  548.           xrand = math.random(7, 49)
  549.           yrand = math.random(2, (h - 5))
  550.           createHPack(xrand, yrand, 5)
  551.           end
  552.         end
  553.       end
  554.       end
  555.       gtId4 = os.startTimer(hrefresh)
  556.     end
  557.   end
  558. end
  559.  
  560. function updateBullets()
  561.   for _, v in pairs(bullets) do
  562.     v.x = v.x + v.xstep
  563.     v.y = v.y + v.ystep
  564.     for i, k in pairs(ent) do
  565.       if v.y == k.y and (v.x >= k.x - 2 and v.x <= k.x + 2) then
  566.         entnum = #ent
  567.         if entnum > 0 then
  568.           table.remove(ent, i)
  569.           lenemies = lenemies + 1
  570.           break
  571.         else
  572.           ent = { }
  573.         end
  574.       elseif v.y == tankInfo.y and (v.x >= tankInfo.x and v.x <= tankInfo.x + 2) then
  575.         if tankInfo.hp > 0 then
  576.           tankInfo.hp = tankInfo.hp - v.dmg
  577.           break
  578.         elseif tankInfo.hp == 0 then
  579.           running = false
  580.           endGame("Your tank was obliterated")
  581.         end
  582.       end
  583.     end
  584.   end
  585.  
  586.   local blistl = #bullets
  587.   for i = 1, blistl do
  588.     local v = bullets[i]
  589.     if v and (v.y < 1 or v.y > h or v.y > h - 4) then
  590.       table.remove(bullets, i)
  591.       blistl = blistl - 1
  592.       i = i - 1
  593.       break
  594.     end
  595.   end
  596. end
  597.  
  598. function enemyBullets(nr)
  599. local ents = ent[nr]
  600.     createBullets(ents.x + 1, ents.y, 0, 1, enemyBullet, 5)
  601. end
  602.  
  603. function enemy3Bullets(nr)
  604.   local ents = ent[nr]
  605.     createBullets(ents.x + 1, ents.y, 0, 1, ent3Bullet, 5)
  606. end
  607.  
  608. function moveEnemy1(nr, xparam)
  609.  
  610. local ents = ent[nr]
  611.  
  612.   ents.x = ents.x + ents.xstep
  613.   ents.y = ents.y + ents.ystep
  614.   if ents.x == w - 2 then
  615.     ents.xstep = -1
  616.     ents.ystep = 0
  617.     enemy()
  618.   elseif ents.x == xparam then
  619.     ents.xstep = 1
  620.     ents.ystep = 0
  621.   elseif (ents.y >= tankInfo.y and ents.y <= tankInfo.y + 2 and ents.x >= tankInfo.x and ents.x <= tankInfo.x + 2)
  622.    and (ents.y - 2 >= tankInfo.y and ents.y - 2 <= tankInfo.y + 2 and ents.x + 2 >= tankInfo.x and ents.x + 2 <= tankInfo.x + 2) then
  623.     running = false
  624.     endGame("Your tank perished")
  625.   end
  626. end
  627.  
  628.   while running do
  629.     input()
  630.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement