Enemy Bullet Error
Posted: Sat Jan 13, 2018 11:10 pm
Hello, I am having trouble coding my game so the enemy will shoot bullets towards the player. I have looked through videos and the forum to no avail. If you could help me out, I'd appreciate it.
Here is my code:
Thanks in advance.
Here is my code:
Code: Select all
platform = {}
player = {}
enemy = {}
enemies = {}
enemies_controller = {}
enemies_controller.enemies = {}
function love.load()
background = love.graphics.newImage('background.png')
platform.width = love.graphics.getWidth()
platform.height = love.graphics.getHeight()
platform.x = 0
platform.y = platform.height / 2
player.x = love.graphics.getWidth() / 2
player.y = love.graphics.getHeight() / 2.05
player.speed = 200
player.img = love.graphics.newImage('sprite.png')
player.ground = player.y
player.y_velocity = 0
player.jump_height = -300
player.gravity = -500
enemy.x = love.graphics.getWidth() / 2.05
enemy.y = love.graphics.getHeight() / 2.05
enemy.img = love.graphics.newImage('sprite2.png')
enemy.ground = player.y
enemy.y_velocity = 0
enemy.bullets = {}
enemy.cooldown = 30
enemy.fire = function()
bullet = {}
bullet.x = enemy.x
bullet.y = 243
table.insert(enemy.bullets, bullet)
end
end
function love.update(dt)
if love.keyboard.isDown('d') then
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
player.x = player.x + (player.speed * dt)
end
elseif love.keyboard.isDown('a') then
if player.x > 0 then
player.x = player.x - (player.speed * dt)
end
end
if love.keyboard.isDown('space') then
if player.y_velocity == 0 then
player.y_velocity = player.jump_height
end
end
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - player.gravity * dt
end
if player.y > player.ground then
player.y_velocity = 0
player.y = player.ground
end
end
if love.keyboard.isDown('e') then
enemy.fire()
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(background, 1, 1, 0, 1.6, 1.2, 3)
love.graphics.setColor(50, 100, 150)
love.graphics.rectangle('fill', platform.x, platform.y, platform.width, platform.height)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.img, player.x, player.y, 0, 1, 1.4, 0, 32)
love.graphics.draw(enemy.img, 10, 243, 0, 1.1, 1.5)
-- draw bullets
love.graphics.setColor(255, 255, 255)
for _,v in pairs(enemy.bullets) do
love.graphics.rectangle("fill", v.x, v.y, 10, 5)
end
end