Hi everyone,
I've seen in some researches on Google that this forum is about helping people programming. I may be wrong and in that case please let me know. Otherwise, here is something I'm bothered with :
I've been learning and following this YouTube tutorials to learn the basics of Lua and LOVE2D. In this video in particular ( https://www.youtube.com/watch?v=FUiz1kL0QtI&list=PLz-rYTT-2nItPbZaj8pt10h49HofbWVQT ) the author tells us how to spawn bullets in between a cooldown condition, make those bullets move from the player's body to the top of the window and then make them disappear.
To do this, the method he's used - and that I've followed - is by adding the bullets (as tables) in a table (player.bullets) every time the user presses "space". Then, if I get everything right, the program checks if there is any bullet item in the player.bullets table and draw them. Then, it updates itself with the conditions we've wirtten, making them go up until they reach y = -10 and then delete them from the table.
Here is the code he's used :
function love.load()
player = {}
player.x = 0
player.y = 550
player.bullets = {}
player.cooldown = 20
player.speed = 10
player.fire = function()
if player.cooldown <= 0 then
player.cooldown = 20
bullet = {}
bullet.x = player.x + 35
bullet.y = player.y
table.insert(player.bullets, bullet)
end
end
end
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(" ") 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 - 10
end
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
I wanted to play around with the code to better understand its mechanics. Thus, I tried to play with the player.bullets table and add two types of items in it, just to check what it would do. So I decided to make "multi bullets" spawn with a longer cooldown and make them have a different moving pattern.
Thus I made this code : (note that there is more code not used, it's because there is a bit of the tutorial's next lesson in there, but not much and I believe most of it is in commentary)
function love.load()
--
player = {}
player.x = 0
player.y=575
player.speed=7.5
player.bullets={}
player.cooldown=0
player.cooldownMulti=0
player.fire = function()
if player.cooldown <= 0 then
-- + long cooldown = + lent shooting
player.cooldown=10
bullet = {}
bullet.x = player.x + 7.5
bullet.y = player.y
table.insert (player.bullets, bullet)
end
if player.cooldownMulti <= 0 then
player.cooldownMulti=50
multiBulletL = {}
multiBulletL.x = player.x - 15
multiBulletL.y = player.y
table.insert (player.bullets, multiBulletL)
multiBulletR = {}
multiBulletR.x = player.x + 30
multiBulletR.y = player.y
table.insert (player.bullets, multiBulletR)
end
end
--y = 0
ennemies_controller = {}
ennemies_controller.ennemies = {}
end
--[[function ennemies_controller:spawnEnnemy()
enemy = {}
ennemy.x = 0
ennemy.y=0
ennemy.speed=7.5
ennemy.bullets={}
ennemy.cooldown=0
end
function ennemy:fire()
if self.cooldown <= 0 then
-- cooldown <=> vitesse
self.cooldown=10
bullet = {}
bullet.x = self.x + 7.5
bullet.y = self.y
table.insert (self.bullets, bullet)
end
end]]
-- dt=delta time
function love.update(dt)
player.cooldown=0 --player.cooldown-1
player.cooldownMulti=0 --player.cooldownMulti-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
-- gauche droite
--[[if love.keyboard.isDown("up") then
player.y = player.y-1
end
if love.keyboard.isDown("down") then
player.y = player.y+1
end]]
if love.keyboard.isDown("space") then
player.fire()
end
for i,v in ipairs (player.bullets) do
-- vitesse bullets
if v==bullet then
v.y = v.y-8
end
if v==multiBulletL then
v.x = v.x-1
v.y = v.y-6.5
end
if v==multiBulletR then
v.x = v.x+1
v.y = v.y-6.5
end
if v.y < -10 then
table.remove(player.bullets, i)
end
end
end
function love.draw()
-- r v b (max 255)
love.graphics.setColor(1, 0, 0)
-- x y l h
love.graphics.rectangle("fill", player.x, player.y, 30, 30)
for _,v in pairs(player.bullets) do
if v==bullet then
love.graphics.setColor(200, 1, 1)
love.graphics.rectangle("fill", v.x, v.y, 15, 15)
end
if v==multiBulletL then
love.graphics.setColor(0, 1, 1)
love.graphics.rectangle("fill", v.x, v.y, 15, 15)
end
if v==multiBulletR then
love.graphics.setColor(0, 1, 1)
love.graphics.rectangle("fill", v.x, v.y, 15, 15)
end
end
end
But the behaviour of the bullets is weird now. It actually draws the new bullets and they have the moving pattern I wanted but something made that :
- they don't launch until we release the space (instead of shooting while we press space) ,
- they disappear every time we press space.
If someone would be kind enough to try to explain to me what went wrong when I tried to upgrade this code, I'd glady appreciate.
Bye ! pl4typus

1 comment
Whenever the game updates the frame, it's checking whether the spacebar is down, not when it is pressed / released. I'm no expert in Love, but maybe there is a function that checks if the spacebar is released and not down.