Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Tank
- --[[ Info:
- Welcome to World of Tanks. Your objective is to
- eliminate all tanks from the playing field.
- Enemys:
- Red - Moves
- Green - Shoots
- Orange - Shoots and moves
- Help:
- Ammo Packs
- Health Packs
- Extra Support (Not Implemented Yet)
- ]]--
- w,h = term.getSize()
- running = true
- refresh = 0.10
- gtId = os.startTimer(refresh)
- hrefresh = 3
- gtId4 = os.startTimer(hrefresh)
- player1deaths = 0
- player2d = 0
- playercolor = {
- colors.red,
- colors.cyan
- }
- randomColor1 = math.random(1,2)
- if randomColor1 == 1 then
- randomColor2 = 2
- else
- randomColor2 = 1
- end
- player1 = {
- x = 22,
- y = h - 5,
- hp = 15,
- maxhp = 15,
- ammo = 10,
- color = playercolor[randomColor1],
- extraHelp = false
- }
- player2 = {
- x = 22,
- y = 5,
- hp = 15,
- maxhp = 15,
- ammo = 10,
- color = playercolor[randomColor2],
- extraHelp = false
- }
- bullets = { }
- ammopack = { }
- healthpack = { }
- barr = { }
- function createBullets(x, y, xvel, yvel, typeBullets, bdmg)
- table.insert(bullets, {
- x = x,
- y = y,
- xstep = xvel,
- ystep = yvel,
- type = typeBullets,
- dmg = bdmg
- })
- end
- function createAmmo(x, y, amount)
- table.insert(ammopack, {
- x = x,
- y = y,
- amount = amount
- })
- end
- function createHPack(x, y, amount)
- table.insert(healthpack, {
- x = x,
- y = y,
- amount = amount
- })
- end
- function createBarr(x, y, barrlen, color)
- table.insert(barr, {
- x = x,
- y = y,
- barrlen = barrlen,
- color = color
- })
- end
- function tank()
- term.setTextColor(player1.color)
- term.setCursorPos(player1.x, player1.y)
- print(" | ")
- term.setCursorPos(player1.x, player1.y + 1)
- print("*-*")
- term.setCursorPos(player1.x, player1.y + 2)
- print("| |")
- term.setCursorPos(player1.x, player1.y + 3)
- print("*-*")
- end
- function tank2()
- term.setTextColor(player2.color)
- term.setCursorPos(player2.x, player2.y)
- print(" | ")
- term.setCursorPos(player2.x, player2.y - 1)
- print("*-*")
- term.setCursorPos(player2.x, player2.y - 2)
- print("| |")
- term.setCursorPos(player2.x, player2.y - 3)
- print("*-*")
- end
- function ammoPack()
- for _, v in pairs(ammopack) do
- term.setTextColor(colors.yellow)
- term.setCursorPos(v.x, v.y)
- print("[]")
- end
- end
- function healthPack()
- for _, v in pairs(healthpack) do
- term.setTextColor(colors.red)
- term.setCursorPos(v.x, v.y)
- print("[]")
- end
- end
- function checkForCollison()
- 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
- running = false
- endGame("Tip: You should not run head first into battle")
- end
- end
- function barrCollision()
- for _, v in pairs(barr) do
- if player1.x >= v.x and player1.x <= v.x + 11 and (player1.y == v.y) then
- player1.x = 22
- player1.y = h - 5
- elseif player2.x >= v.x and player2.x <= v.x + 11 and player2.y == v.y then
- player2.x = 22
- player2.y = 5
- end
- end
- end
- function updateAmmo()
- for _, v in pairs(ammopack) do
- if not (v.x > player1.x or v.x + 1 < player1.x or v.y > player1.y + 3 or v.y < player1.y) then
- player1.ammo = player1.ammo + v.amount
- table.remove(ammopack, 1)
- break
- elseif not (v.x > player2.x or v.x + 1 < player2.x or v.y < player2.y - 3 or v.y > player2.y) then
- player2.ammo = player2.ammo + v.amount
- table.remove(ammopack, 1)
- break
- end
- end
- end
- function updateHealth()
- for _, v in pairs(healthpack) do
- if not (v.x > player1.x or v.x + 1 < player1.x or v.y > player1.y + 3 or v.y < player1.y) then
- if player1.hp <= 5 then
- player1.hp = player1.hp + v.amount
- table.remove(healthpack, 1)
- break
- end
- elseif not (v.x > player2.x or v.x + 1 < player2.x or v.y < player2.y - 3 or v.y > player2.y) then
- if player2.hp <= 5 then
- player2.hp = player2.hp + v.amount
- table.remove(healthpack, 1)
- break
- end
- end
- end
- end
- function endGame(deathmsg)
- local select = 1
- function optionsdraw()
- term.clear()
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.clear()
- bg = paintutils.loadImage("Tanks/bgt")
- paintutils.drawImage(bg, 1,1)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(w/2 - 4, 11)
- print("GAME OVER")
- term.setCursorPos(4, 13)
- print(deathmsg)
- term.setCursorPos(w/2 - 5, 15)
- print("Keep Going")
- term.setCursorPos(w/2 - 4, 17)
- print("Quit Game")
- if select == 1 then
- term.setCursorPos(w/2 - 6, 15)
- print("[")
- term.setCursorPos(w/2 + 5, 15)
- print("]")
- else
- term.setCursorPos(w/2 - 5, 17)
- print("[")
- term.setCursorPos(w/2 + 5, 17)
- print("]")
- end
- end
- optionsdraw()
- while true do
- local event, p1 = os.pullEventRaw()
- if event == "key" then
- if p1 == keys.w or p1 == keys.s or p1 == keys.up or p1 == keys.down then
- select = -select
- optionsdraw()
- elseif p1 == keys.enter or p1 == keys.space then
- if select == 1 and (player1deaths < 3 or player2d < 3) then
- running = true
- player1.x = 22
- player1.y = h - 5
- player1.hp = player1.maxhp
- player1.ammo = 10
- player2.x = 22
- player2.y = 7
- player2.hp = player1.maxhp
- player2.ammo = 10
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- for i = 1, #healthpack do
- table.remove(healthpack, i)
- end
- gtId = os.startTimer(refresh)
- gtId4 = os.startTimer(hrefresh)
- drawBg()
- break
- else
- if player1deaths == 3 then
- playerwin = "Player 2"
- elseif player2deaths == 3 then
- playerwin = "Player 1"
- else
- playerwin = "Nobody"
- end
- term.clear()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- print(playerwin.." won the game! Congrats!")
- return
- end
- end
- end
- end
- end
- function drawBg()
- term.clear()
- w,h = term.getSize()
- for i = 1, #barr do
- term.setTextColor(colors.white)
- term.setCursorPos(barr[i].x + i, barr[i].y)
- write("-----------")
- i = i + 1
- term.setTextColor(colors.white)
- end
- for i = 1, w do
- term.setTextColor(colors.white)
- term.setCursorPos(w - (i), h - 1)
- write("-")
- end
- for i = 1, h do
- term.setTextColor(colors.white)
- term.setCursorPos(5, h - (i))
- write("|")
- end
- for i = 1, h do
- term.setTextColor(colors.white)
- term.setCursorPos((w-4), (h - (i)))
- write("|")
- end
- function player1hpbar()
- for i = 1, (player1.hp/player1.maxhp)*5 do
- term.setBackgroundColor(colors.red)
- term.setCursorPos(2, 16 - (i))
- write(" ")
- term.setBackgroundColor(colors.black)
- end
- end
- function player2hpbar()
- for i = 1, (player2.hp/player2.maxhp)*5 do
- term.setBackgroundColor(colors.red)
- term.setCursorPos(w-2, 16 - (i))
- write(" ")
- term.setBackgroundColor(colors.black)
- end
- end
- term.setCursorPos(1, 10)
- print("*--*")
- term.setCursorPos((w-3), 10)
- print("*--*")
- for i = 1, 5 do
- term.setTextColor(colors.white)
- term.setCursorPos(1, 16 - (i))
- write("| |")
- player1hpbar()
- end
- for i = 1, 5 do
- term.setTextColor(colors.white)
- term.setCursorPos((w-3), (16 - (i)))
- write("| |")
- player2hpbar()
- end
- term.setCursorPos(1,16)
- print("*--*")
- term.setCursorPos(w-3, 16)
- print("*--*")
- term.setCursorPos(1,2)
- print("Tank")
- print(" 1 ")
- print("Ammo")
- print(player1.ammo)
- print(" ")
- print(" HP ")
- term.setCursorPos(2,8)
- print(player1.hp)
- term.setCursorPos(w-3,2)
- print("Tank")
- term.setCursorPos(w-3,3)
- print(" 2 ")
- term.setCursorPos(w-3,4)
- print("Ammo")
- term.setCursorPos(w-3,5)
- print(player2.ammo)
- term.setCursorPos(w-3,6)
- print(" ")
- term.setCursorPos(w-3,7)
- print(" HP ")
- term.setCursorPos(w-2,8)
- print(player2.hp)
- term.setCursorPos(1,h)
- print("Tank Info - Playing: Team Deathmatch")
- term.setCursorPos( w - 20, h - 1)
- tank()
- tank2()
- for _, v in pairs(bullets) do
- term.setCursorPos(v.x, v.y)
- term.setTextColor(v.type)
- term.write(".")
- term.setTextColor(colors.black)
- end
- --Draws all the desired enemies on the screen
- ammoPack()
- healthPack()
- end
- barrnumrand = math.random(1,5)
- for i = 1, barrnumrand do
- xrand = math.random(16, w - 6)
- yrand = math.random(7, h - 7)
- barrlen2 = math.random(3,6)
- createBarr(xrand, yrand, barrlen2, 1)
- end
- tank()
- drawBg()
- tank2()
- function input()
- local event, key = os.pullEvent()
- if event == "key" then
- if key == keys.q then
- running = false
- endGame("Do you wish to quit?")
- elseif key == keys.w then
- player1.y = player1.y - 1
- elseif key == keys.s then
- player1.y = player1.y + 1
- elseif key == keys.a then
- player1.x = player1.x - 1
- elseif key == keys.d then
- player1.x = player1.x + 1
- elseif key == keys.down then
- player2.y = player2.y - 1
- elseif key == keys.up then
- player2.y = player2.y + 1
- elseif key == keys.left then
- player2.x = player2.x - 1
- elseif key == keys.right then
- player2.x = player2.x + 1
- elseif key == keys.f then
- if player1.ammo > 0 then
- createBullets(player1.x + 1, player1.y - 1, 0, -1, player1.color,5)
- player1.ammo = player1.ammo - 1
- end
- elseif key == keys.space then
- if player2.ammo > 0 then
- createBullets(player2.x + 1, player2.y + 1, 0, 1, player2.color, 5)
- player2.ammo = player2.ammo - 1
- end
- drawBg()
- tank()
- tank2()
- updateBullets()
- checkForCollison()
- updateAmmo()
- end
- term.setBackgroundColor(colors.black)
- term.setCursorPos(player1.x, player1.y)
- term.clearLine()
- term.setCursorPos(player1.x, player1.y + 1)
- term.clearLine()
- term.setCursorPos(player1.x, player1.y + 2)
- term.clear()
- tank()
- drawBg()
- if player1.x == w - 6 then
- player1.x = 22
- player1.y = h - 5
- elseif player1.x == 5 then
- player1.x = 22
- player1.y = h - 5
- elseif player1.y == h - 3 then
- player1.x = 22
- player1.y = h - 5
- elseif player1.y == 1 then
- player1.x = 22
- player1.y = h - 5
- elseif player2.y == 1 then
- player2.x = 22
- player2.y = 5
- elseif player2.y == h - 3 then
- player2.x = 22
- player2.y = 5
- elseif player2.x == 5 then
- player2.x = 22
- player2.y = 5
- elseif player2.x == w - 6 then
- player2.x = 22
- player2.y = 5
- end
- elseif event == "timer" then
- if key == gtId then
- updateBullets()
- updateAmmo()
- updateHealth()
- checkForCollison()
- barrCollision()
- gtId = os.startTimer(refresh)
- drawBg()
- if player1.hp <= 0 then
- running = false
- endGame("Player 2 shot Player 1")
- player1deaths = player1deaths + 1
- player2d = player2d + 0
- end
- if player2.hp <= 0 then
- running = false
- endGame("Player 1 shot Player 2")
- player2d = player2d + 1
- player1deaths = player1deaths + 0
- end
- if player1.ammo <= 3 or player2.ammo <= 3 then
- if #ammopack < 1 then
- xrand = math.random(7,44)
- yrand = math.random(3,h-6)
- arand = math.random(1,20)
- createAmmo(xrand, yrand, arand)
- end
- end
- elseif key == gtId4 then
- updateHealth()
- if player1.hp <= 5 or player2.hp <= 5 then
- chances = math.random(0, 50)
- if #healthpack < 1 then
- if chances < 15 then
- xrand = math.random(7, 44)
- yrand = math.random(3, (h - 6))
- createHPack(xrand, yrand, 5)
- end
- end
- end
- gtId4 = os.startTimer(hrefresh)
- end
- end
- end
- function updateBullets()
- for _, v in pairs(bullets) do
- v.x = v.x + v.xstep
- v.y = v.y + v.ystep
- if v.y == player1.y and (v.x >= player1.x and v.x <= player1.x + 2) then
- if player1.hp > 0 then
- player1.hp = player1.hp - v.dmg
- break
- elseif player1.hp == 0 then
- running = false
- endGame("Player 2 shot Player 1")
- end
- elseif v.y == player2.y and (v.x >= player2.x and v.x <= player2.x + 2) then
- if player2.hp > 0 then
- player2.hp = player2.hp - v.dmg
- elseif player2.hp == 0 then
- running = false
- endGame("Player 1 shot Player 2")
- end
- end
- end
- local blistl = #bullets
- for i = 1, blistl do
- for j, k in pairs(barr) do
- local v = bullets[i]
- 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
- table.remove(bullets, i)
- blistl = blistl - 1
- i = i - 1
- break
- end
- end
- end
- end
- while running do
- input()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement