Code: Select all
print("Logan's Tank Game")
enemy = {}
enemies_controller = {}
enemies_controller.enemies = {}
background = love.graphics.newImage("background.png")
-- Collisions --
function checkCollisions(enemies, bullets)
for i,e in ipairs(enemies) do
for _,b in ipairs(bullets) do
if b.y <= e.y + e.height and b.y > e.x and b.x < e.x + e.width then
table.remove(enemies, i)
end
end
end
end
-- Player Functions --
function love.load()
player = {}
player.x = 400
player.y = 10
player.bullets = {}
player.cooldown = 50
player.speed = 1
player.image = love.graphics.newImage('player.png')
player.fire = function()
if player.cooldown <= 0 then
player.cooldown = 50
bullet = {}
bullet.x = player.x + 21
bullet.y = player.y + 100
table.insert(player.bullets, bullet)
end
end
enemies_controller:spawnEnemy(400, 600)
enemies_controller:spawnEnemy(300, 500)
enemies_controller:spawnEnemy(200, 600)
enemies_controller:spawnEnemy(100, 500)
end
-- Enemy Functions --
function enemies_controller:spawnEnemy(x, y)
enemy = {}
enemies_controller.image = love.graphics.newImage('enemy.png')
enemy.x = x
enemy.y = y
enemy.width = 10
enemy.height = 15
enemy.bullets = {}
enemy.cooldown = 20
enemy.speed = 1
table.insert(self.enemies, enemy)
end
-- Enemy Fire --
function enemy:fire()
if self.cooldown <= 0 then
self.cooldown = 20
bullet = {}
bullet.x = self.x + 21
bullet.y = self.y
table.insert(self.bullets, bullet)
end
end
-- Player Controls --
function love.update(dt)
player.cooldown = player.cooldown - 1
if love.keyboard.isDown("right") then
player.x = player.x + player.speed
elseif love.keyboard.isDown("left") then
player.x = player.x - player.speed
end
if love.keyboard.isDown("up") then
player.y = player.y - player.speed
elseif love.keyboard.isDown("down") then
player.y = player.y + player.speed
end
if love.keyboard.isDown("f") then
player.fire()
end
if player.x < 0 then
player.x = 0
elseif player.x > 755 then
player.x = 755
elseif player.y < 0 then
player.y = 0
end
for _,e in pairs(enemies_controller.enemies) do
e.y = e.y - 0.5
if e.y < -10 then
table.remove(enemies_controller.enemies)
end
end
for i,b in ipairs(player.bullets) do
if b.y > 650 then
table.remove(player.bullets, i)
end
b.y = b.y + 3
end
checkCollisions(enemies_controller.enemies, player.bullets)
end
-- Graphics --
function love.draw()
for i = 0, love.graphics.getWidth() / background:getWidth() do
for j = 0, love.graphics.getHeight() / background:getHeight() do
love.graphics.draw(background, i * background:getWidth(), j * background:getHeight())
end
end
-- Player Tank --
love.graphics.draw(player.image) do
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.image, player.x, player.y, 0, .2)
end
-- Enemy Tank --
for _,e in pairs(enemies_controller.enemies) do
love.graphics.setColor(255, 255, 255)
love.graphics.draw(enemies_controller.image, e.x, e.y, 9.41, .2)
end
-- Weapon --
for _,b in pairs(player.bullets) do
love.graphics.setColor(255, 100, 0)
love.graphics.circle("fill", b.x, b.y, 4, 4)
end
end