I recently needed a countdown timer for a game I wanted to port but wasn't able to find a tutorial online. The code wasn't exactly difficult, but it would have saved me some time, so I figured I would upload the template I made for myself so others could use it.
This will show the timer on screen in total seconds (set to the highest maximum number by default) and in digital clock format (00:00:00).
-- countdown timer -- by ryanb_dev -- hours:minutes:seconds -- max 32767 seconds -- 9:06:06 function _init() timer = { text = "", hours = 0, minutes = 0, seconds = 0, active = true, seconds_left = 32767, math_seconds = 0, } last_time = time() end function _update60() if (timer.active) then timer_update() end end function _draw() cls() //actual seconds print(timer.seconds_left) //clock format print(timer.text) end function timer_update() timer.text = "" timer.seconds_left -= time() - last_time timer.math_seconds = timer.seconds_left last_time = time() timer.hours = flr(timer.math_seconds / 3600) timer.math_seconds -= timer.hours * 3600 timer.minutes = flr(timer.math_seconds / 60) timer.seconds = flr(timer.math_seconds % 60) timer.text = timer.text.."0"..tostr(timer.hours)..":" if timer.minutes < 10 then timer.text = timer.text.."0"..tostr(timer.minutes)..":" else timer.text = timer.text..tostr(timer.minutes)..":" end if timer.seconds < 10 then timer.text = timer.text.."0"..tostr(timer.seconds) else timer.text = timer.text..tostr(timer.seconds) end if (timer.seconds_left <= 0) then timer.active = false end end |
[Please log in to post a comment]