5 second timer with reset parameter

You can copy this code and change the time!

--Here is the code:
function setup()
   q = 1
end
function draw()
    background(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    font("Copperplate")
    fill(255, 0, 0, 255)
    q = q + 1
    fontSize(40)
    text("Timer: " ..q, 400, 500)
    if q >499 then
        q = q - 1
        fontSize(50)
        font("AmericanTypewriter-Bold")
        text("TIME UP!", 400, 300)
        parameter.action("Restart", function() setup() end)
    end
end

Just remember! Put a return under ‘–Here is the code:’

@code_maker, try using os.difftime if a countdown timer is what you’re going after

Thanks!

@code_maker If you’re going to put text on the screen, you should look into how to use the “string.format” command. See the update I made to your code.


--Here is the code:
function setup()
   q = 1
end

function draw()
    background(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    font("Copperplate")
    fill(255, 0, 0, 255)
    q = q + 1
    fontSize(40)

    text(string.format("Timer: %.2f",q/100),400,500)

    --text("Timer: " ..q, 400, 500)
    if q >499 then
        q = q - 1
        fontSize(50)
        font("AmericanTypewriter-Bold")
        text("TIME UP!", 400, 300)
        parameter.action("Restart", function() setup() end)
    end
end

Let me try that!

Here’s 3.0 plus classes, I finally figured out an easy way to make classes work together


--# Main

function setup()
    supportedOrientations(LANDSCAPE_ANY)
    q = 0
    Paramaters:setup()
end

function draw()
    background(0, 0, 0, 0)    
    TimeCounter:draw()
    fill(255, 0, 0, 255)
    fontSize(60)
    text("Timer: " ..q, 400, 700)    
end

--# Paramaters

Paramaters = class()

function Paramaters:setup()
    parameter.integer("Time", 499, 12000, 499)    
    parameter.action("Restart", function() setup() end)        
    parameter.boolean("Pause", false)    
end

function Paramaters:draw()   
end

function Paramaters:touched(touch)
    -- Codea does not automatically call this method
end

--# TimeCounter

TimeCounter = class()

function TimeCounter:draw()     
       q = q + 1    
       if Pause == true then
          q = q - 1
       end    
    if q >Time then       
        q = q -1
        text("TIME UP!", 400, 300)
        fontSize(20)
        text("Tap the restart button to restart", 500, 100)
    end
end


function TimeCounter:touched(touch)
    -- Codea does not automatically call this method
end

@code_maker It’s good to see that you’re changing your code to make it better, but I don’t think you understand classes yet. While you can use classes the way you have them coded, an important way to use them is to have multiple instances of that class running at the same time. Because of the way a class is written, the same code can run different instances at the same time. Below is a small example of 7 timer running at the same time. I didn’t add the “restart” and “pause” you have in yours, I just wanted you to see an example of multiple instances running at the same time.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    t={}  -- table of timers
    table.insert(t,timer(200,700,200))  -- x,y position, time limit
    table.insert(t,timer(200,600,300))
    table.insert(t,timer(200,500,415))
    table.insert(t,timer(200,400,525))
    table.insert(t,timer(200,300,630))
    table.insert(t,timer(200,200,800))
    table.insert(t,timer(200,100,945))
end

function draw()
    background(0, 0, 0, 0)    
    fill(255)
    for a,b in pairs(t) do
        b:draw()
    end
end

timer=class()

function timer:init(x,y,limit)
    self.x=x
    self.y=y
    self.limit=limit
    self.counter=0
end

function timer:draw()
    if self.counter>self.limit then
        str=string.format("Times up:  %.2f",self.limit/100)
    else
        self.counter=self.counter+1
        str=string.format("Timer %.2f:  %.2f",self.limit/100,self.counter/100)
    end
    text(str,self.x,self.y)  
end