(Solved) No objects are being drawn
Posted: Mon May 28, 2018 10:06 am
Code: Select all
enemy = {}
enemies_controller = {}
enemies_controller.enemies = {}
function love.load()
player = {}
player.x = 0
player.y = 575
player.bullets = {}
player.cooldown = 40
player.speed = 5
player.fire = function()
if player.cooldown <= 0 then
player.cooldown = 40
bullet = {}
bullet.x = player.x + 35
bullet.y = player.y - 5
table.insert(player.bullets, bullet)
end
end
enemies_controller:spawnEnemy()
end
function enemies_controller:spawnEnemy()
enemy = {}
enemy.x = 0
enemy.y = 0
enemy.bullets = {}
enemy.cooldown = 40
enemy.speed = 5
table.insert(self.enemies, enemy)
end
function enemy:fire()
if self.cooldown <= 0 then
self.cooldown = 40
bullet = {}
bullet.x = self.x + 35
bullet.y = self.y - 5
table.insert(player.bullets, bullet)
end
function love.draw()
-- Draw the player
love.graphics.setColor(0, 0, 255)
love.graphics.rectangle("fill", player.x, player.y, 80, 20)
-- Draw Bullets
love.graphics.setColor(255, 255, 255)
for _, b in pairs(player.bullets) do
love.graphics.rectangle("fill", b.x, b.y, 10, 10)
end
end
function love.update(dt)
player.cooldown = player.cooldown - 1
if love.keyboard.isDown("right") then
player.x = player.x + player.speed
end
if love.keyboard.isDown("left") then
player.x = player.x - player.speed
end
if love.keyboard.isDown("space") then
player.fire()
end
for i, b in ipairs(player.bullets) do
if b.y < -10 then
table.remove(player.bullets, i)
end
b.y = b.y - 5
end
end
end