Skip to content

Instantly share code, notes, and snippets.

@LPGhatguy
Created June 19, 2013 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LPGhatguy/5810682 to your computer and use it in GitHub Desktop.
Save LPGhatguy/5810682 to your computer and use it in GitHub Desktop.
The source for a cube timer application.
--[[
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
@Dekkonot
Copy link

Yay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment