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)
- Information about scoring:
- Scoring is recorded in the user's player.stat
- file. Here is how scoring goes:
- Enemy Red Kill - 10 pt
- Enemy Green Kill - 15 pt
- Enemy Orange Kill - 20 pt
- Headshots - 15 pt
- Ship Level Up - 20 pt
- Ship Extra Exp - 10 pt
- Ammo Pack - -5 pt
- Health Packs - -5 pt
- Information about experience:
- Experience is gathered to ulock other tanks.
- As of now there are certain tanks but in the
- future customization may be an option. Some
- tanks may have higher health ,some may have
- more ammo, some may get more health from
- health packs, some may get kill streaks,
- some may enable more exp and much more:
- Enemy Kills: 1 exp
- Headshots: 2 exp
- Maximum Exp (How much exp you need to level up) :
- 20 exp
- Information about the player.stat file:
- The player.stat file holds the following info
- in the file in the following order:
- Name
- Campaign Level
- Highscore
- Total Lifetime Shots
- Total Lifetime Enemies Killed
- Total Lifetime Deaths
- Waves
- ]]--
- w,h = term.getSize()
- running = true
- refresh = 0.10
- gtId = os.startTimer(refresh)
- brefresh = 1.5
- gtId2 = os.startTimer(brefresh)
- brand = math.random(0.10, 1.5)
- gtId3 = os.startTimer(brand)
- hrefresh = 3
- gtId4 = os.startTimer(hrefresh)
- levelprogress = 0
- tries = 1
- score = 0
- maxexp = 20
- killstreak = 0
- --Opens the player stat file and player info file --
- h = fs.open("Tanks/player.stat", "r")
- p = fs.open("Tanks/player.info", "r")
- name = h.readLine()
- level = h.readLine()
- highscore1 = h.readLine()
- tlbullets = h.readLine()
- tlenemies = h.readLine()
- tldeaths = h.readLine()
- wavenum = h.readLine()
- levelplayer = p.readLine()
- expplayer = p.readLine()
- xplayer = p.readLine()
- yplayer = p.readLine()
- hpplayer = p.readLine()
- maxhpplayer = p.readLine()
- ammoplayer = p.readLine()
- colorplayer = p.readLine()
- lbullets = tlbullets
- lenemies = tlenemies
- ldeaths = tldeaths
- levelpro = tonumber(level)
- playerlevel = tonumber(levelplayer)
- playerexp = tonumber(expplayer)
- playerx = tonumber(xplayer)
- playery = tonumber(yplayer)
- playerhp = tonumber(hpplayer)
- playermaxhp = tonumber(maxhpplayer)
- playerammo = tonumber(ammoplayer)
- playercolor = tostring(colorplayer)
- highscore = tonumber(highscore1)
- w,h = term.getSize()
- tankInfo = {
- x = 22,
- y = h - 5,
- hp = playerhp,
- maxhp = playermaxhp,
- ammo = playerammo,
- exp = playerexp,
- color = playercolor
- }
- bullets = { }
- ent = { }
- ammopack = { }
- healthpack = { }
- bulletColor = {
- colors.white,
- colors.gray,
- colors.red,
- colors.green,
- colors.orange
- }
- tankBullet = bulletColor[1]
- enemyBullet = bulletColor[4]
- ent3Bullet = bulletColor[5]
- 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 createEnt(x, y, typeEnt, entNumber, xvel, yvel, entId)
- table.insert(ent, {
- x = x,
- y = y,
- color = typeEnt,
- newEnt = entNumber,
- xstep = xvel,
- ystep = yvel,
- ID = entId
- })
- 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 createPlayer(playerx, playery, playerhp, playermaxhp, playerammo, playerexp)
- table.insert(tankInfo, {
- x = playerx,
- y = playery,
- hp = playerhp,
- maxhp = playermaxhp,
- ammo = playerammo,
- exp = playerexp
- })
- end
- function tank()
- term.setTextColor(tankInfo.color)
- term.setCursorPos(tankInfo.x, tankInfo.y)
- print(" | ")
- term.setCursorPos(tankInfo.x, tankInfo.y + 1)
- print("*-*")
- term.setCursorPos(tankInfo.x, tankInfo.y + 2)
- print("| |")
- term.setCursorPos(tankInfo.x, tankInfo.y + 3)
- print("*-*")
- end
- function enemy()
- for _, v in pairs(ent) do
- term.setTextColor(v.color)
- term.setCursorPos(v.x, v.y)
- print(" | ")
- term.setCursorPos(v.x, v.y - 1)
- print("*-*")
- term.setCursorPos(v.x, v.y - 2)
- print("| |")
- term.setCursorPos(v.x, v.y - 3)
- print("*-*")
- end
- 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 updateEnt()
- if #ent > 0 then
- for i = 1, #ent do
- if ent[i] then
- local ents = ent[i]
- if ents.ID == 1 then
- moveEnemy1(i, 5)
- elseif ents.ID == 3 then
- moveEnemy1(i, 10)
- end
- end
- end
- end
- end
- function updateEnt2()
- if #ent > 0 then
- for i = 1, #ent do
- if ent[i] then
- local ents = ent[i]
- if ents.ID == 2 then
- enemyBullets(i)
- end
- end
- end
- end
- end
- function updateEnt3()
- if #ent > 0 then
- for i = 1, #ent do
- if ent[i] then
- local ents = ent[i]
- if ents.ID == 3 then
- enemy3Bullets(i)
- end
- end
- end
- end
- end
- function checkForCollison()
- if #ent > 0 then
- for i = 1, #ent do
- if ent[i] then
- local ents = ent[i]
- if not (ents.x > tankInfo.x or ents.x + 2 < tankInfo.x or ents.y - 3 > tankInfo.y + 3 or ents.y < tankInfo.y) then
- running = false
- endGame("Tip: You should not run head first into battle")
- end
- end
- end
- end
- end
- function updateAmmo()
- for _, v in pairs(ammopack) do
- if not (v.x > tankInfo.x or v.x + 1 < tankInfo.x or v.y > tankInfo.y + 3 or v.y < tankInfo.y) then
- tankInfo.ammo = tankInfo.ammo + v.amount
- table.remove(ammopack, 1)
- score = score - 5
- break
- end
- end
- end
- function updateHealth()
- for _, v in pairs(healthpack) do
- if not (v.x > tankInfo.x or v.x + 1 < tankInfo.x or v.y > tankInfo.y + 3 or v.y < tankInfo.y) then
- tankInfo.hp = tankInfo.hp + v.amount
- table.remove(healthpack, 1)
- score = score - 5
- break
- 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 - 8, 15)
- print("Retry Last Level")
- term.setCursorPos(w/2 - 4, 17)
- print("Quit Game")
- if select == 1 then
- term.setCursorPos(w/2 - 9, 15)
- print("[")
- term.setCursorPos(w/2 + 8, 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 tries <= 1 then
- running = true
- tankInfo.x = playerx
- tankInfo.y = playery
- tankInfo.hp = tankInfo.maxhp
- tankInfo.ammo = playerammo
- tankInfo.exp = 0
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- for i = 1, #healthpack do
- table.remove(healthpack, i)
- end
- gtId = os.startTimer(refresh)
- gtId2 = os.startTimer(brefresh)
- gtId3 = os.startTimer(brand)
- gtId4 = os.startTimer(hrefresh)
- drawBg()
- updateBullets()
- tries = tries + 1
- ldeaths = ldeaths + 1
- break
- else
- ldeaths = ldeaths + 1
- filelevel = 0
- filescore = 0
- if levelpro > levelprogress then
- filelevel = levelpro
- elseif levelprogress > levelpro then
- filelevel = levelprogress
- end
- if score > highscore then
- filescore = score
- elseif highscore > score then
- filescore = highscore
- end
- w = fs.open("Tanks/player.stat", "w")
- w.writeLine(name)
- w.writeLine(filelevel)
- w.writeLine(filescore)
- w.writeLine(lbullets)
- w.writeLine(lenemies)
- w.writeLine(ldeaths)
- w.writeLine(wavenum)
- w.flush()
- w.close()
- pl = fs.open("Tanks/player.info", "w")
- pl.writeLine(playerlevel)
- pl.writeLine(playerexp)
- pl.flush()
- pl.close()
- term.clear()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- print("Thank you for playing")
- return
- end
- end
- end
- end
- end
- createPlayer(22, h - 5, 15, 15, 10, playerexp)
- function drawBg()
- term.clear()
- 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
- function hpbar()
- for i = 1, (tankInfo.hp/tankInfo.maxhp)*5 do
- term.setBackgroundColor(colors.red)
- term.setCursorPos(2, 15 - (i))
- write(" ")
- term.setBackgroundColor(colors.black)
- end
- end
- function expbar()
- for i = 1, (tankInfo.exp/maxexp)*10 do
- term.setBackgroundColor(colors.orange)
- term.setCursorPos( (w-13) + (i), h - 1)
- write(" ")
- term.setBackgroundColor(colors.black)
- end
- end
- term.setCursorPos(1, 9)
- print("*--*")
- for i = 1, 5 do
- term.setTextColor(colors.white)
- term.setCursorPos(1, 15 - (i))
- write("| |")
- hpbar()
- end
- term.setCursorPos(1,15)
- print("*--*")
- --term.setCursorPos(w - 15, 1)
- --print("Score: "..score)
- term.setCursorPos(1,2)
- print("Tank")
- print(" ")
- print("Ammo")
- print(tankInfo.ammo)
- print(" ")
- print("HP ")
- term.setCursorPos(2,8)
- print(tankInfo.hp)
- term.setCursorPos(1,15)
- print("Score")
- print(score)
- term.setCursorPos(1,h)
- print("Tank Info - Name: "..name.." Exp: Level "..playerlevel)
- term.setCursorPos( w - 20, h - 1)
- expbar()
- tank()
- 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
- enemy()
- ammoPack()
- healthPack()
- end
- function createNewLevel()
- if levelprogress == 0 then
- createEnt(10, 5, colors.red,1,1,0,1)
- levelprogress = levelprogress + 1
- elseif levelprogress == 1 then
- createEnt(10, 5, colors.green,1,0,0,2)
- levelprogress = 2
- elseif levelprogress == 2 then
- createEnt(10, 5, colors.orange,1,1,0,3)
- levelprogress = 3
- elseif levelprogress == 3 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 10, colors.green,2,0,0,2)
- levelprogress = 4
- elseif levelprogress == 4 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 10, colors.orange,2,1,0,3)
- levelprogress = 5
- elseif levelprogress == 5 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 10, colors.green, 2,0,0,2)
- createEnt(40, 10, colors.green, 3,0,0,2)
- levelprogress = 6
- elseif levelprogress == 6 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 10, colors.green, 2,0,0,2)
- createEnt(40, 10, colors.green, 3,0,0,2)
- createEnt(30, 10, colors.green, 4,0,0,2)
- levelprogress = 7
- elseif levelprogress == 7 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 5, colors.red, 2,1,0,1)
- createEnt(30, 5, colors.red, 3,1,0,1)
- createEnt(40, 5, colors.red, 4,1,0,1)
- createEnt(25, 10, colors.red, 5,1,0,1)
- createEnt(35, 10, colors.red, 6,1,0,1)
- levelprogress = 8
- elseif levelprogress == 8 then
- createEnt(10, 5, colors.green, 1,0,0,2)
- createEnt(20, 5, colors.green, 2,0,0,2)
- createEnt(30, 5, colors.green, 3,0,0,2)
- createEnt(40, 5, colors.green, 4,0,0,2)
- levelprogress = 9
- elseif levelprogress == 9 then
- createEnt(10, 10, colors.orange, 1,1,0,3)
- createEnt(20, 10, colors.orange, 2,1,0,3)
- levelprogress = 10
- elseif levelprogress == 10 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 5, colors.red, 2,1,0,1)
- createEnt(30, 5, colors.red, 3,1,0,1)
- createEnt(25, 10, colors.green, 4,0,0,2)
- createEnt(15, 10, colors.green, 5,0,0,2)
- levelprogress = 11
- elseif levelprogress == 11 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 5, colors.red, 2,1,0,1)
- createEnt(30, 5, colors.red, 3,1,0,1)
- createEnt(20, 10, colors.orange, 4,1,0,3)
- levelprogress = 12
- elseif levelprogress == 12 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 5, colors.red, 2,1,0,1)
- createEnt(30, 5, colors.red, 3,1,0,1)
- createEnt(40, 5, colors.red, 4,1,0,1)
- createEnt(25, 10, colors.green, 5,0,0,2)
- createEnt(35, 10, colors.green, 6,0,0,2)
- levelprogress = 13
- elseif levelprogress == 13 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 5, colors.red, 2,1,0,1)
- createEnt(30, 5, colors.red, 3,1,0,1)
- createEnt(40, 5, colors.red, 4,1,0,1)
- createEnt(25, 10, colors.orange, 4,1,0,3)
- levelprogress = 14
- elseif levelprogress == 14 then
- createEnt(25, 5, colors.red, 1,1,0,1)
- levelprogress = 15
- elseif levelprogress == 15 then
- createEnt(10, 10, colors.orange, 1,1,0,3)
- createEnt(20, 10, colors.orange, 2,1,0,3)
- createEnt(30, 10, colors.orange, 3,1,0,3)
- levelprogress = 16
- elseif levelprogress == 16 then
- createEnt(20, 10, colors.green, 1,0,0,2)
- levelprogress = 17
- elseif levelprogress == 17 then
- createEnt(10, 5, colors.red, 1,1,0,1)
- createEnt(20, 5, colors.red, 2,1,0,1)
- createEnt(30, 5, colors.red, 3,1,0,1)
- createEnt(40, 5, colors.red, 4,1,0,1)
- createEnt(25, 10, colors.orange, 5,1,0,3)
- createEnt(35, 10, colors.orange, 6,1,0,3)
- levelprogress = 18
- elseif levelprogress == 18 then
- createEnt(20, 10, colors.green, 1,0,0,2)
- levelprogress = 19
- elseif levelprogress == 19 then
- createEnt(10, 10, colors.orange, 1,1,0,3)
- createEnt(20, 10, colors.orange, 2,1,0,3)
- createEnt(30, 10, colors.orange, 3,1,0,3)
- createEnt(40, 10, colors.orange, 4,1,0,3)
- levelprogress = 20
- elseif levelprogress == 20 then
- endGame("Congratulations! You completed the campaign! Now try to beat your score, level up, or play unlimited waves!")
- running = false
- end
- end
- tank()
- drawBg()
- enemy()
- function input()
- local event, key = os.pullEvent()
- if event == "key" then
- if key == keys.q then
- running = false
- elseif key == keys.w then
- tankInfo.y = tankInfo.y - 1
- elseif key == keys.s then
- tankInfo.y = tankInfo.y + 1
- elseif key == keys.a then
- tankInfo.x = tankInfo.x - 1
- elseif key == keys.d then
- tankInfo.x = tankInfo.x + 1
- elseif key == keys.up then
- tankInfo.y = tankInfo.y - 1
- elseif key == keys.down then
- tankInfo.y = tankInfo.y + 1
- elseif key == keys.left then
- tankInfo.x = tankInfo.x - 1
- elseif key == keys.right then
- tankInfo.x = tankInfo.x + 1
- elseif key == keys.space then
- if tankInfo.ammo > 0 then
- createBullets(tankInfo.x + 1, tankInfo.y - 1, 0, -1, tankBullet,0)
- tankInfo.ammo = tankInfo.ammo - 1
- lbullets = lbullets + 1
- end
- drawBg()
- tank()
- enemy()
- updateBullets()
- checkForCollison()
- updateAmmo()
- end
- term.setBackgroundColor(colors.black)
- term.setCursorPos(tankInfo.x, tankInfo.y)
- term.clearLine()
- term.setCursorPos(tankInfo.x, tankInfo.y + 1)
- term.clearLine()
- term.setCursorPos(tankInfo.x, tankInfo.y + 2)
- term.clear()
- tank()
- drawBg()
- if tankInfo.x == w - 3 then
- tankInfo.x = 22
- tankInfo.y = h - 5
- elseif tankInfo.x == 5 then
- tankInfo.x = 22
- tankInfo.y = h - 5
- elseif tankInfo.y == h - 3 then
- tankInfo.x = 22
- tankInfo.y = h - 5
- elseif tankInfo.y == 1 then
- tankInfo.x = 22
- tankInfo.y = h - 5
- end
- elseif event == "timer" then
- if key == gtId then
- updateBullets()
- updateEnt()
- updateAmmo()
- updateHealth()
- checkForCollison()
- gtId = os.startTimer(refresh)
- drawBg()
- for _, v in pairs(bullets) do
- for i, k in pairs(ent) do
- if #ent > 0 then
- if v.x == k.x + 1 and v.y <= k.y then
- score = score + 15
- tankInfo.exp = tankInfo.exp + 3
- table.remove(ent, i)
- lenemies = lenemies + 1
- if k.ID == 1 then
- score = score + 10
- elseif k.ID == 2 then
- score = score + 15
- elseif k.ID == 3 then
- score = score + 20
- end
- break
- end
- if v.x >= k.x and v.x <= k.x + 2 and v.y <= k.y then
- table.remove(ent, i)
- lenemies = lenemies + 1
- tankInfo.exp = tankInfo.exp + 1
- if k.ID == 1 then
- score = score + 10
- elseif k.ID == 2 then
- score = score + 15
- elseif k.ID == 3 then
- score = score + 20
- end
- break
- end
- end
- end
- end
- if tankInfo.exp >= maxexp then
- tankInfo.exp = 0
- playerlevel = playerlevel + 1
- end
- if tankInfo.hp <= 0 then
- running = false
- endGame("You kind of got shot")
- end
- if tankInfo.ammo <= 3 then
- if #ammopack == 0 then
- xrand = math.random(7,47)
- yrand = math.random(3,h-5)
- arand = math.random(0,20)
- if arand > 0 then
- createAmmo(xrand, yrand, arand)
- elseif arand == 0 then
- running = false
- endGame("You took all the bullets from your camp")
- end
- end
- end
- elseif key == gtId2 then
- updateEnt2()
- gtId2 = os.startTimer(brefresh)
- if #ent <= 0 then
- createNewLevel()
- end
- elseif key == gtId3 then
- updateEnt3()
- brand = math.random(0.10, 2.5)
- gtId3 = os.startTimer(brand)
- elseif key == gtId4 then
- updateHealth()
- if tankInfo.hp <= 5 then
- chances = math.random(0, 50)
- if #healthpack == 0 then
- if chances < 15 then
- xrand = math.random(7, 47)
- yrand = math.random(3, (h - 5))
- 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
- for i, k in pairs(ent) do
- if #ent > 0 then
- if v.y == tankInfo.y and (v.x >= tankInfo.x and v.x <= tankInfo.x + 2) then
- if tankInfo.hp > 0 then
- tankInfo.hp = tankInfo.hp - v.dmg
- break
- elseif tankInfo.hp == 0 then
- running = false
- endGame("Your tank was obliterated")
- end
- end
- end
- end
- end
- local blistl = #bullets
- for i = 1, blistl do
- local v = bullets[i]
- if v and (v.y < 1 or v.y > h or v.y > h - 4) then
- table.remove(bullets, i)
- blistl = blistl - 1
- i = i - 1
- break
- end
- end
- end
- function enemyBullets(nr)
- local ents = ent[nr]
- createBullets(ents.x + 1, ents.y, 0, 1, enemyBullet, 5)
- end
- function enemy3Bullets(nr)
- local ents = ent[nr]
- createBullets(ents.x + 1, ents.y, 0, 1, ent3Bullet, 5)
- end
- function moveEnemy1(nr, xparam)
- local ents = ent[nr]
- ents.x = ents.x + ents.xstep
- ents.y = ents.y + ents.ystep
- if ents.x == w - 2 then
- ents.xstep = -1
- ents.ystep = 0
- enemy()
- elseif ents.x == xparam then
- ents.xstep = 1
- ents.ystep = 0
- elseif (ents.y >= tankInfo.y and ents.y <= tankInfo.y + 2 and ents.x >= tankInfo.x and ents.x <= tankInfo.x + 2)
- 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
- running = false
- endGame("Your tank perished")
- end
- end
- while running do
- input()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement