Want to add a timer which counts how many time take to complete a level
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Want to add a timer which counts how many time take to complete a level
I want to add a clock which shows how many minutes or seconds past to complete a level. How can I achieve this?
Re: Want to add a timer which counts how many time take to complete a level
Best way that i can think of:
Then you can translate this time to minutes, hours, etc:
To get amount of minutes
Amount of hours
And so on
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)
And so on
Re: Want to add a timer which counts how many time take to complete a level
If there is no need to continuously display the timer you can also just retrieve the time at departure and arrival and subtract the departure time from the arrival time.
You can then round the result if you want to display it as an integer or if you want to display it as a floating number you can use string.format. Here is a more complete example:
You can then convert also to minutes, hours etc as GVovkiv said
Edit: to round the minutes "closer to reality" you can round as I showed in my example.
Sorry, I don't have the right term in English, but otherwise, to be completely accurate, you should just use math.floor if 59 seconds isn't a minute for you. You know everything like that
As a bonus, a function to correctly convert seconds into hours, minutes, seconds. I don't want to confuse you with my rounding stories, so everything is clear for you!
You can then round the result if you want to display it as an integer or if you want to display it as a floating number you can use string.format. Here is a more complete example:
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
Edit: to round the minutes "closer to reality" you can round as I showed in my example.
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
As a bonus, a function to correctly convert seconds into hours, minutes, seconds. I don't want to confuse you with my rounding stories, so everything is clear for you!
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
Re: Want to add a timer which counts how many time take to complete a level
Thank you all.... Here is my solution...
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
Re: Want to add a timer which counts how many time take to complete a level
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 coffee
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
Re: Want to add a timer which counts how many time take to complete a level
Set time
Get delta time:
Show time and delta time:
Also:
https://stackoverflow.com/a/45376848
(not tested)
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
https://stackoverflow.com/a/45376848
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
Re: Want to add a timer which counts how many time take to complete a level
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
Thank you, this works like a charm
Who is online
Users browsing this forum: Google [Bot], lenlenlL6, MrFariator and 7 guests