Advertisement
Guest User

lmptdm

a guest
May 5th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.04 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
  9.   Orange - Shoots and moves
  10.  
  11.   Help:
  12.   Ammo Packs
  13.   Health Packs
  14.   Extra Support (Not Implemented Yet)
  15. ]]--
  16.  
  17. w,h = term.getSize()
  18.  
  19. running = true
  20.  
  21. refresh = 0.10
  22. gtId = os.startTimer(refresh)
  23.  
  24. hrefresh = 3
  25. gtId4 = os.startTimer(hrefresh)
  26.  
  27. player1deaths = 0
  28. player2d = 0
  29.  
  30. playercolor = {
  31. colors.red,
  32. colors.cyan
  33. }
  34.  
  35. randomColor1 = math.random(1,2)
  36. if randomColor1 == 1 then
  37.     randomColor2 = 2
  38. else
  39.     randomColor2 = 1
  40. end
  41.  
  42. player1 = {
  43.   x = 22,
  44.   y = h - 5,
  45.   hp = 15,
  46.   maxhp = 15,
  47.   ammo = 10,
  48.   color = playercolor[randomColor1],
  49.   extraHelp = false
  50. }
  51.  
  52. player2 = {
  53.   x = 22,
  54.   y = 5,
  55.   hp = 15,
  56.   maxhp = 15,
  57.   ammo = 10,
  58.   color = playercolor[randomColor2],
  59.   extraHelp = false
  60. }
  61.  
  62. bullets = { }
  63. ammopack = { }
  64. healthpack = { }
  65. barr = { }
  66.  
  67. function createBullets(x, y, xvel, yvel, typeBullets, bdmg)
  68.   table.insert(bullets, {
  69.     x = x,
  70.     y = y,
  71.     xstep = xvel,
  72.     ystep = yvel,
  73.     type = typeBullets,
  74.     dmg = bdmg
  75.    })
  76. end
  77.  
  78. function createAmmo(x, y, amount)
  79.   table.insert(ammopack, {
  80.   x = x,
  81.   y = y,
  82.   amount = amount
  83.   })
  84. end
  85.  
  86. function createHPack(x, y, amount)
  87.   table.insert(healthpack, {
  88.   x = x,
  89.   y = y,
  90.   amount = amount
  91.   })
  92. end
  93.  
  94. function createBarr(x, y, barrlen, color)
  95.     table.insert(barr, {
  96.     x = x,
  97.     y = y,
  98.     barrlen = barrlen,
  99.     color = color
  100.     })
  101. end
  102.  
  103. function tank()
  104. term.setTextColor(player1.color)
  105.   term.setCursorPos(player1.x, player1.y)
  106.   print(" | ")
  107.   term.setCursorPos(player1.x, player1.y + 1)
  108.   print("*-*")
  109.   term.setCursorPos(player1.x, player1.y + 2)
  110.   print("| |")
  111.   term.setCursorPos(player1.x, player1.y + 3)
  112.   print("*-*")
  113. end
  114.  
  115. function tank2()
  116. term.setTextColor(player2.color)
  117.   term.setCursorPos(player2.x, player2.y)
  118.   print(" | ")
  119.   term.setCursorPos(player2.x, player2.y - 1)
  120.   print("*-*")
  121.   term.setCursorPos(player2.x, player2.y - 2)
  122.   print("| |")
  123.   term.setCursorPos(player2.x, player2.y - 3)
  124.   print("*-*")
  125. end
  126.  
  127. function ammoPack()
  128. for _, v in pairs(ammopack) do
  129.   term.setTextColor(colors.yellow)
  130.   term.setCursorPos(v.x, v.y)
  131.   print("[]")
  132. end  
  133. end
  134.  
  135. function healthPack()
  136. for _, v in pairs(healthpack) do
  137.   term.setTextColor(colors.red)
  138.   term.setCursorPos(v.x, v.y)
  139.   print("[]")
  140. end
  141. end
  142.  
  143. function checkForCollison()
  144.     if not (player2.x > player1.x or player2.x + 2 < player1.x or player2.y - 3 > player1.y + 3 or player2.y < player1.y) then
  145.         running = false
  146.         endGame("Tip: You should not run head first into battle")
  147.     end
  148. end
  149.  
  150. function barrCollision()
  151.     for _, v in pairs(barr) do
  152.         if player1.x >= v.x and player1.x <= v.x + 11 and (player1.y == v.y) then
  153.             player1.x = 22
  154.             player1.y = h - 5
  155.         elseif player2.x >= v.x and player2.x <= v.x + 11 and player2.y == v.y then
  156.             player2.x = 22
  157.             player2.y = 5
  158.         end
  159.     end
  160. end
  161.  
  162. function updateAmmo()
  163.   for _, v in pairs(ammopack) do
  164.     if not (v.x > player1.x or v.x + 1 < player1.x or v.y > player1.y + 3 or v.y < player1.y) then
  165.       player1.ammo = player1.ammo + v.amount
  166.       table.remove(ammopack, 1)
  167.       break
  168.     elseif not (v.x > player2.x or v.x + 1 < player2.x or v.y < player2.y - 3 or v.y > player2.y) then
  169.     player2.ammo = player2.ammo + v.amount
  170.       table.remove(ammopack, 1)
  171.       break
  172.     end
  173.   end
  174. end
  175.  
  176. function updateHealth()
  177.   for _, v in pairs(healthpack) do
  178.     if not (v.x > player1.x or v.x + 1 < player1.x or v.y > player1.y + 3 or v.y < player1.y) then
  179.     if player1.hp <= 5 then
  180.       player1.hp = player1.hp + v.amount
  181.       table.remove(healthpack, 1)
  182.       break
  183.     end
  184.     elseif not (v.x > player2.x or v.x + 1 < player2.x or v.y < player2.y - 3 or v.y > player2.y) then
  185.     if player2.hp <= 5 then
  186.     player2.hp = player2.hp + v.amount
  187.       table.remove(healthpack, 1)
  188.       break
  189.     end
  190.     end
  191.   end
  192. end
  193.  
  194. function endGame(deathmsg)
  195.     local select = 1
  196.   function optionsdraw()
  197.     term.clear()
  198.     term.setTextColor(colors.black)
  199.     term.setBackgroundColor(colors.white)
  200.     term.clear()
  201.     bg = paintutils.loadImage("Tanks/bgt")
  202.     paintutils.drawImage(bg, 1,1)
  203.     term.setBackgroundColor(colors.white)
  204.     term.setCursorPos(w/2 - 4, 11)
  205.     print("GAME OVER")
  206.     term.setCursorPos(4, 13)
  207.     print(deathmsg)
  208.     term.setCursorPos(w/2 - 5, 15)
  209.     print("Keep Going")
  210.     term.setCursorPos(w/2 - 4, 17)
  211.     print("Quit Game")
  212.       if select == 1 then
  213.         term.setCursorPos(w/2 - 6, 15)
  214.         print("[")
  215.         term.setCursorPos(w/2 + 5, 15)
  216.         print("]")
  217.       else
  218.         term.setCursorPos(w/2 - 5, 17)
  219.         print("[")
  220.         term.setCursorPos(w/2 + 5, 17)
  221.         print("]")
  222.       end
  223.   end
  224.   optionsdraw()
  225.     while true do
  226.       local event, p1 = os.pullEventRaw()
  227.         if event == "key" then
  228.           if p1 == keys.w or p1 == keys.s or p1 == keys.up or p1 == keys.down then
  229.             select = -select
  230.             optionsdraw()
  231.           elseif p1 == keys.enter or p1 == keys.space then
  232.             if select == 1 and (player1deaths < 3 or player2d < 3) then
  233.               running = true
  234.               player1.x = 22
  235.               player1.y = h - 5
  236.               player1.hp = player1.maxhp
  237.               player1.ammo = 10
  238.               player2.x = 22
  239.               player2.y = 7
  240.               player2.hp = player1.maxhp
  241.               player2.ammo = 10
  242.               term.setBackgroundColor(colors.black)
  243.               term.setTextColor(colors.white)
  244.                 for i = 1, #healthpack do
  245.                   table.remove(healthpack, i)
  246.                 end
  247.                 gtId = os.startTimer(refresh)
  248.                 gtId4 = os.startTimer(hrefresh)
  249.               drawBg()
  250.               break  
  251.             else
  252.             if player1deaths == 3 then
  253.                 playerwin = "Player 2"
  254.             elseif player2deaths == 3 then
  255.                 playerwin = "Player 1"
  256.             else
  257.                 playerwin = "Nobody"
  258.             end
  259.               term.clear()
  260.               term.setTextColor(colors.white)
  261.               term.setBackgroundColor(colors.black)
  262.               term.clear()
  263.               term.setCursorPos(1,1)
  264.               print(playerwin.." won the game! Congrats!")
  265.               return
  266.             end
  267.           end
  268.         end
  269.     end
  270.   end
  271.  
  272. function drawBg()
  273. term.clear()
  274. w,h = term.getSize()
  275. for i = 1, #barr do
  276.     term.setTextColor(colors.white)
  277.     term.setCursorPos(barr[i].x + i, barr[i].y)
  278.     write("-----------")
  279.     i = i + 1
  280.     term.setTextColor(colors.white)
  281. end
  282.   for i = 1, w do
  283.   term.setTextColor(colors.white)
  284.     term.setCursorPos(w - (i), h - 1)
  285.     write("-")
  286.   end
  287.   for i = 1, h do
  288.     term.setTextColor(colors.white)
  289.       term.setCursorPos(5, h - (i))
  290.       write("|")
  291.   end
  292.   for i = 1, h do
  293.     term.setTextColor(colors.white)
  294.     term.setCursorPos((w-4), (h - (i)))
  295.     write("|")
  296.   end
  297.   function player1hpbar()
  298.     for i = 1, (player1.hp/player1.maxhp)*5 do
  299.         term.setBackgroundColor(colors.red)
  300.         term.setCursorPos(2, 16 - (i))
  301.         write("  ")
  302.         term.setBackgroundColor(colors.black)
  303.     end
  304.   end
  305.   function player2hpbar()
  306.     for i = 1, (player2.hp/player2.maxhp)*5 do
  307.         term.setBackgroundColor(colors.red)
  308.         term.setCursorPos(w-2, 16 - (i))
  309.         write("  ")
  310.         term.setBackgroundColor(colors.black)
  311.     end
  312.   end
  313.   term.setCursorPos(1, 10)
  314.   print("*--*")
  315.   term.setCursorPos((w-3), 10)
  316.   print("*--*")
  317.   for i = 1, 5 do
  318.     term.setTextColor(colors.white)
  319.     term.setCursorPos(1, 16 - (i))
  320.     write("|  |")
  321.     player1hpbar()
  322.   end
  323.   for i = 1, 5 do
  324.     term.setTextColor(colors.white)
  325.     term.setCursorPos((w-3), (16 - (i)))
  326.     write("|  |")
  327.     player2hpbar()
  328.   end
  329.   term.setCursorPos(1,16)
  330.   print("*--*")
  331.  term.setCursorPos(w-3, 16)
  332.   print("*--*")
  333.   term.setCursorPos(1,2)
  334.   print("Tank")
  335.   print("  1 ")
  336.   print("Ammo")
  337.   print(player1.ammo)
  338.   print("    ")
  339.   print(" HP ")
  340.   term.setCursorPos(2,8)
  341.   print(player1.hp)
  342.   term.setCursorPos(w-3,2)
  343.   print("Tank")
  344.   term.setCursorPos(w-3,3)
  345.   print("  2 ")
  346.   term.setCursorPos(w-3,4)
  347.   print("Ammo")
  348.   term.setCursorPos(w-3,5)
  349.   print(player2.ammo)
  350.   term.setCursorPos(w-3,6)
  351.   print("    ")
  352.   term.setCursorPos(w-3,7)
  353.   print(" HP ")
  354.   term.setCursorPos(w-2,8)
  355.   print(player2.hp)
  356. term.setCursorPos(1,h)
  357. print("Tank Info -  Playing: Team Deathmatch")
  358. term.setCursorPos( w - 20, h - 1)
  359. tank()
  360. tank2()
  361.  
  362.   for _, v in pairs(bullets) do
  363.     term.setCursorPos(v.x, v.y)
  364.     term.setTextColor(v.type)
  365.     term.write(".")
  366.     term.setTextColor(colors.black)
  367.   end
  368.   --Draws all the desired enemies on the screen
  369.     ammoPack()
  370.     healthPack()
  371. end
  372.  
  373. barrnumrand = math.random(1,5)
  374. for i = 1, barrnumrand do
  375.     xrand = math.random(16, w - 6)
  376.     yrand = math.random(7, h - 7)
  377.     barrlen2 = math.random(3,6)
  378.     createBarr(xrand, yrand, barrlen2, 1)
  379. end
  380. tank()
  381. drawBg()
  382.   tank2()
  383.  
  384. function input()
  385. local event, key = os.pullEvent()
  386.   if event == "key" then
  387.     if key == keys.q then
  388.     running = false
  389.     endGame("Do you wish to quit?")
  390.     elseif key == keys.w then
  391.       player1.y = player1.y - 1
  392.     elseif key == keys.s then
  393.       player1.y = player1.y + 1
  394.     elseif key == keys.a then
  395.       player1.x = player1.x - 1
  396.     elseif key == keys.d then
  397.       player1.x = player1.x + 1
  398.     elseif key == keys.down then
  399.       player2.y = player2.y - 1
  400.     elseif key == keys.up then
  401.       player2.y = player2.y + 1
  402.     elseif key == keys.left then
  403.       player2.x = player2.x - 1
  404.     elseif key == keys.right then
  405.       player2.x = player2.x + 1
  406.     elseif key == keys.f then
  407.       if player1.ammo > 0 then
  408.         createBullets(player1.x + 1, player1.y - 1, 0, -1, player1.color,5)
  409.             player1.ammo = player1.ammo - 1
  410.       end
  411.     elseif key == keys.space then
  412.         if player2.ammo > 0 then
  413.           createBullets(player2.x + 1, player2.y + 1, 0, 1, player2.color, 5)
  414.           player2.ammo = player2.ammo - 1
  415.         end
  416.       drawBg()
  417.       tank()
  418.         tank2()
  419.         updateBullets()
  420.         checkForCollison()
  421.         updateAmmo()
  422.     end
  423.     term.setBackgroundColor(colors.black)
  424.     term.setCursorPos(player1.x, player1.y)
  425.     term.clearLine()
  426.     term.setCursorPos(player1.x, player1.y + 1)
  427.     term.clearLine()
  428.     term.setCursorPos(player1.x, player1.y + 2)
  429.     term.clear()
  430.     tank()
  431.     drawBg()
  432.     if player1.x == w - 6 then
  433.       player1.x = 22
  434.       player1.y = h - 5
  435.     elseif player1.x == 5 then
  436.       player1.x = 22
  437.       player1.y = h - 5
  438.     elseif player1.y == h - 3 then
  439.       player1.x = 22
  440.       player1.y = h - 5
  441.     elseif player1.y == 1 then
  442.       player1.x = 22
  443.       player1.y = h - 5
  444.     elseif player2.y == 1 then
  445.       player2.x = 22
  446.       player2.y = 5
  447.     elseif player2.y == h - 3 then
  448.       player2.x = 22
  449.       player2.y = 5
  450.     elseif player2.x == 5 then
  451.       player2.x = 22
  452.       player2.y = 5
  453.     elseif player2.x == w - 6 then
  454.       player2.x = 22
  455.       player2.y = 5
  456.     end
  457.   elseif event == "timer" then
  458.     if key == gtId then
  459.       updateBullets()
  460.       updateAmmo()
  461.       updateHealth()
  462.       checkForCollison()
  463.       barrCollision()
  464.       gtId = os.startTimer(refresh)
  465.       drawBg()
  466.       if player1.hp <= 0 then
  467.         running = false
  468.         endGame("Player 2 shot Player 1")
  469.         player1deaths = player1deaths + 1
  470.         player2d = player2d + 0
  471.       end
  472.       if player2.hp <= 0 then
  473.         running = false
  474.         endGame("Player 1 shot Player 2")
  475.         player2d = player2d + 1
  476.         player1deaths = player1deaths + 0
  477.       end
  478.         if player1.ammo <= 3 or player2.ammo <= 3 then
  479.           if #ammopack < 1 then
  480.           xrand = math.random(7,44)
  481.           yrand = math.random(3,h-6)
  482.             arand = math.random(1,20)
  483.             createAmmo(xrand, yrand, arand)
  484.         end
  485.       end
  486.     elseif key == gtId4 then
  487.       updateHealth()
  488.       if player1.hp <= 5 or player2.hp <= 5 then
  489.         chances = math.random(0, 50)
  490.         if #healthpack < 1 then
  491.             if chances < 15 then
  492.               xrand = math.random(7, 44)
  493.               yrand = math.random(3, (h - 6))
  494.               createHPack(xrand, yrand, 5)
  495.             end
  496.         end
  497.       end
  498.       gtId4 = os.startTimer(hrefresh)
  499.     end
  500.   end
  501. end
  502.  
  503. function updateBullets()
  504.   for _, v in pairs(bullets) do
  505.     v.x = v.x + v.xstep
  506.     v.y = v.y + v.ystep
  507.       if v.y == player1.y and (v.x >= player1.x and v.x <= player1.x + 2) then
  508.         if player1.hp > 0 then
  509.               player1.hp = player1.hp - v.dmg
  510.           break
  511.         elseif player1.hp == 0 then
  512.           running = false
  513.           endGame("Player 2 shot Player 1")
  514.         end
  515.       elseif v.y == player2.y and (v.x >= player2.x and v.x <= player2.x + 2) then
  516.         if player2.hp > 0 then
  517.             player2.hp = player2.hp - v.dmg
  518.         elseif player2.hp == 0 then
  519.             running = false
  520.             endGame("Player 1 shot Player 2")
  521.         end
  522.       end
  523.     end
  524.  
  525.   local blistl = #bullets
  526.   for i = 1, blistl do
  527.   for j, k in pairs(barr) do
  528.     local v = bullets[i]
  529.     if v and (v.y < 1 or v.y > h or v.y > h - 4 or ((v.y == k.y) and v.x >= k.x and v.x <= k.x + 11)) then
  530.       table.remove(bullets, i)
  531.       blistl = blistl - 1
  532.       i = i - 1
  533.       break
  534.     end
  535.   end
  536.   end
  537. end
  538.  
  539.   while running do
  540.     input()
  541.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement