Page 1 of 1

Timer to end game

Posted: Thu Jan 23, 2020 5:38 am
by DKipppp
How do I make an easy timer to end a game after like 2 seconds. I made a small asteroids game and once you win you can shoot into the air. I just want to make a timer so you can only shoot in the air for 2 seconds and then the window closes

Re: Timer to end game

Posted: Fri Jan 24, 2020 3:15 pm
by pgimeno
You can use a timer library or make your own. If you decide to use one, hump.timer is good. It can be overkill for just one timer, though, so if you decide to make your own, it's easy. Have a variable that tells if the timer is active. If it is, accumulate dt in another variable until the value exceeds the set timer. For example:

Code: Select all

local timerActive = false
local timerDuration = 0

function love.update(dt)
  if timerActive then
    timerDuration = timerDuration + dt
    -- after 2 seconds, quit
    if timerDuration >= 2 then
      love.event.quit()
    end
  end

  ... rest of love.update here

end
You then set timerActive = true when the player wins, so that the timer starts counting.