Want to add a timer which counts how many time take to complete a level
Posted: Sun Jan 22, 2023 4:49 pm
I want to add a clock which shows how many minutes or seconds past to complete a level. How can I achieve this?
Code: Select all
local time = 0
love.update = function(dt)
time = time + dt -- counted in miliseconds. 1 = 1 real seconds
end
Code: Select all
print(time / 60) --floor it or ceil?
Code: Select all
print((time / 60) / 60)
Code: Select all
local t_start = os.clock()
for i = 1, 10000 do end -- "simulate" the game
local t_end = os.clock()
local total = t_end - t_start
print(string.format("%.6f Sec",total)) -- `.6f` means 6 digits after the decimal
print(math.floor(total+.5)) -- Or we round the number
Code: Select all
local seconds = 59 -- We want it to be worth 1 minutes
local minutes = math.floor(seconds/60+.01666666666666672) -- because 59/60=0.98333... S/60+(1-59/60)
-- Note: But it can be redundant to search for the right value
-- for and it will only work for a value less than 60
-- so you can also use the modulo operator with a condition
minutes = math.floor(seconds / 60)
if seconds % 60 == 59 then
minutes = minutes + 1
end
Code: Select all
function getTimeFromSec(seconds)
local hours = math.floor(seconds/3600)
seconds = seconds-(hours*3600)
local minutes = math.floor(seconds/60)
seconds = seconds-(minutes*60)
return string.format("%d:%02d:%02d", hours, minutes, seconds) -- Displays as follows: H:M:S -- %02d is used to display "00" if empty or "01" for 1.
end
Code: Select all
function love.load()
-- Initialize everything
timer = 0
maxTimer = 1
hour = 0
minute = 0
seconds = 0
end
function love.update(dt)
increaseTime(dt) -- call the function increaseTime
end
function love.draw()
end
function increaseTime(dt)
timer = timer + dt -- increase timer every frame
if timer >= maxTimer then -- if 1 second past reset timer and increase seconds
seconds = seconds + 1
timer = 0
end
if seconds >= 60 then -- if 1 minute past reset seconds and increase minute
seconds = 0
minute = minute + 1
end
if minute >= 60 then -- if 1 hour past reset minute and increase hour
minute = 0
hour = hour + 1
end
-- print(timer)
print(hour,minute,seconds) -- print(hour,minute,seconds)
end
Code: Select all
function increaseTime(dt)
timer = timer + dt
if timer >= maxTimer then
seconds = seconds + 1
timer = timer - maxTimer
end
if seconds >= 60 then
seconds = seconds - 60
minute = minute + 1
end
if minute >= 60 then
minute = minute - 60
hour = hour + 1
end
print(hour,minute,seconds)
end
Code: Select all
function love.mousepressed(x, y, button, istouch)
T = love.timer.getTime()
end
Code: Select all
function love.mousereleased(x, y, button)
DT = love.timer.getTime() - T
end
Code: Select all
function love.draw()
if T then
love.graphics.print(T, 0, 0)
end
if DT then
love.graphics.print(DT, 0, 20)
end
end
Code: Select all
function disp_time(time)
local days = math.floor(time/86400)
local hours = math.floor(math.mod(time, 86400)/3600)
local minutes = math.floor(math.mod(time,3600)/60)
local seconds = math.floor(math.mod(time,60))
return string.format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
end
Bigfoot71 wrote: ↑Mon Jan 23, 2023 3:48 pm Your code may produce slight inaccuracies over time, I'd rather do something like this:
But if you're not up to the milliseconds your code already makes coffeeCode: Select all
function increaseTime(dt) timer = timer + dt if timer >= maxTimer then seconds = seconds + 1 timer = timer - maxTimer end if seconds >= 60 then seconds = seconds - 60 minute = minute + 1 end if minute >= 60 then minute = minute - 60 hour = hour + 1 end print(hour,minute,seconds) end