right now I am trying to recreate just the part where the bullets are fired.
I have some code which fires 5 bullets at the same time (that's actually the code for the invaders but I adapted it a bit so it works the other way around).
But what I really want is to fire one bullet on the press of spacebar (or mouse click) and fire another shot when the spacebar is pressed again.
However, if I use "love.keyboard.isDown" or "love.keypressed", when I press space the bullet only moves a little bit. After the keypress it should move regardless if I'm pressing space or not. Despite the tutorial and trying it for myself, I can't grasp how to do this in Love2d. Well, I can of course copy the tutorial, but that doesn't mean I understand the how and why of this.
Side question:
I'm also a bit confused with the proper 'place' of the functions: sometimes I see people put them in love.draw, sometimes in love.update and sometimes in love.load. Sometimes people create other lua files (e.g. "player.lua" or "room.lua") and put functions there. I try to put my functions as much as possible in love.load, but that doesn't always give the result I want. Does that mean it doesn't really matter where functions are, or are there 'best practices'?
Code: Select all
function love.load()
hero = {}
hero.x = 300
hero.y = 450
hero.speed = 300
magazine={}
for i=0,5 do
bullet = {}
bullet.width = 5
bullet.height = 10
bullet.x = hero.x + 20*i + bullet.width
bullet.y = hero.y + 10*i - bullet.height
bullet.speed = 300
table.insert(magazine, bullet)
end
end
function love.update(dt)
if love.keyboard.isDown("left") then
hero.x = hero.x - hero.speed * dt
elseif love.keyboard.isDown("right") then
hero.x = hero.x + hero.speed * dt
end
for i,v in ipairs(magazine) do
if v.y > 40 then
v.y = v.y - bullet.speed * dt
else
table.remove(magazine, i)
end
end
end
function love.draw()
-- draw the ground
love.graphics.setColor(0,255,0,255)
love.graphics.rectangle("fill", 0, 465, 800, 150)
-- draw the hero
love.graphics.setColor(255,255,0,255)
love.graphics.rectangle("fill", hero.x, hero.y, 30, 15)
-- draw the bullets
for i,v in ipairs(magazine) do
love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
end
end