The source for a cube timer application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Cube Timer | |
Version 3.1 | |
]] | |
local start_keys = {"lctrl", "ralt"} | |
local start_keys_down = {false, false} | |
local keys_down = false | |
local keys_down_last = false | |
local elapsed = 0 | |
local start_keys_pressed = false | |
local timing = false | |
local timer_armed = false | |
local time_font | |
local detail_font | |
function format_time(time) | |
local seconds = tostring(math.floor(time % 60)) | |
local decimal = tostring(time % 60):match("%.%d%d") | |
local minutes = tostring(math.floor(time / 60)) | |
if (decimal) then | |
if (decimal:len() < 2) then | |
decimal = decimal .. "0" | |
end | |
else | |
decimal = ".00" | |
end | |
if (seconds:len() < 2) then | |
seconds = "0" .. seconds | |
end | |
if (minutes:len() < 2) then | |
minutes = "0" .. minutes | |
end | |
return minutes .. ":" .. seconds .. decimal | |
end | |
function love.load() | |
time_font = love.graphics.newFont("pixel_arial.ttf", 72) | |
detail_font = love.graphics.newFont("pixel_arial.ttf", 12) | |
stop_font = love.graphics.newFont("pixel_arial.ttf", 26) | |
end | |
function love.update(delta) | |
keys_down_last = keys_down | |
keys_down = true | |
for index, key in next, start_keys do | |
local down = love.keyboard.isDown(key) | |
start_keys_down[index] = down | |
if (not down) then | |
keys_down = false | |
end | |
end | |
if (timing) then | |
elapsed = elapsed + delta | |
if (keys_down and not keys_down_last) then | |
timing = false | |
end | |
else | |
if (keys_down and not keys_down_last) then | |
elapsed = 0 | |
timer_armed = true | |
elseif (not keys_down and timer_armed) then | |
timer_armed = false | |
timing = true | |
end | |
end | |
end | |
function love.draw() | |
if (timing) then | |
love.graphics.setColor(0, 200, 0) | |
else | |
love.graphics.setColor(200, 0, 0) | |
end | |
love.graphics.setFont(time_font) | |
love.graphics.print(format_time(elapsed), 8, 8) | |
for index, value in next, start_keys_down do | |
if (value) then | |
love.graphics.setColor(0, 200, 0) | |
else | |
love.graphics.setColor(200, 0, 0) | |
end | |
love.graphics.rectangle("fill", 8 + (60 * index) - 60, 90, 50, 30) | |
love.graphics.setColor(255, 255, 255) | |
love.graphics.setFont(detail_font) | |
love.graphics.print(start_keys[index], 20 + (60 * index) - 60, 97) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yay.