'Love2d countdown and stopwatch

As the title suggests I want to create a love2d countdown as well as a stop watch. I previously tried doing this as follows:

  1. Countdown:
function love.load()
   timer = 3
end

function love.update(dt)
   if timer >= 0 then
      timer = timer - dt
   else
      printMsg = true
   end
end

function love.draw()
   if printMsg == true then
      love.graphics.print('time over')
   end
end
  1. Stopwatch:
function love.load()
   timer = 0
   lives = 3
end

function love.update(dt)
   if lives <= 0 then
      timer = timer + dt
   else
      printMsg = true
   end
end

function love.draw()
   if printMsg == true then
      love.graphics.print('you have survived for '..timer..' seconds')
   end
end

function love.mousepressed(x, y, button)
   if button == 1 then  --if left mouse button is pressed then 1 is subtracted from lives
      lives = lives - 1
   end
end

But the problem is that dt is not a constant so therefore the increase or decrease in timer is not even resulting in faster or slower countdown or stopwatch value(mostly faster).

Also I tried using love.timer.getTime() and still its the same as with dt. Any help is highly appreciated. Thanks in advance to any answers.



Solution 1:[1]

try:

function love.update(dt)
    cffcgf = cffcgf + dt -- i slammed my head against my keyboard for that
    if timer >= 0 then
        if cffcgf >= 1 then -- checking if a second passed
            cffcgf = 0
            timer = timer - 1
        end
    else
        printMsg = true
    end
end

try applying the same thing for the stopwatch.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 freeve4