# Pressed key doesn't run out of special powerups

So I'm making a game in Lua/Love2D and have a problem with my game logic. Mainly I have a spaceship that can use boost and stuff. That part is fine, I click space and it boosts up to speed quickly.

The problem is that if the person holds down space, they stay boosted, which is not the intended behavior. How do I make sure that once boost runs out, it decelerates to normal speed? This is how it is currently:

Note: Normal speed, 1000. Max boost possible speed, 3000.

if love.keyboard.isDown('space') then
if player.v < 3000 and UICounters.currentboost > 0 then
player.v = player.v + 200
if player.v > 3000 then
player.v = 3000
end
UICounters.currentboost = UICounters.currentboost - 1
end
else
if player.v > 1000 then
player.v = player.v - 100
end
if UICounters.currentboost < UICounters.maxboost then
UICounters.currentboost = UICounters.currentboost + dt
end
end


While the player holds down the space key, the else part of your code is never reached, so the deceleration is not applied. Basically, you'd have to execute this if UICounters.currentboost is <= 0, even if space is being held down. You could achieve that by duplicating parts of your code, or you could change your initial check to something like this:

if love.keyboard.isDown('space') and player.v < 3000 and UICounters.currentboost > 0 then
player.v = player.v + 200
if player.v > 3000 then
player.v = 3000
end
UICounters.currentboost = UICounters.currentboost - 1
else
if player.v > 1000 then
player.v = player.v - 100
end
if UICounters.currentboost < UICounters.maxboost then
UICounters.currentboost = UICounters.currentboost + dt
end
end


Now, your booster only fires if the space key is pressed AND the velocity is below the max AND the player has boost left. Otherwise it falls into the else part and the player decelerates. This might lead to some back and forth between 3000 and 2900 in certain situations, but I think this should be a good starting point for you to further finetune the behaviour.

• This works perfectly! Thank you so much. :) Can't believe I didn't think of making the initial if statement. – Rivasa Feb 21 '17 at 13:44

I think it is because you are using the keyboard.isDown which basically means if the Space bar is "held down" it will keep adding 200 to player.v untill you release it.

I suggest that you instead use:

function love.keypressed( key )
if key == "return" then
text = "RETURN has been pressed!"
end
end