Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Formatting a timer with 0’s prefacing single digits

in General Posts: 5

Title pretty much says it all. I need help prefacing single digits with 0’s to help minimize text size changing for my game

function setup()

    CountdownSetup()

end

function draw()

    background(40, 40, 50)

    CountdownUpdate()

end

function CountdownSetup()

    Sec, Min, Hour = 0,0,0

    Time = Hour..": "..Min.."' "..string.format("%.0f",Sec)..'"'

    parameter.boolean("Countdown_Status", false)
    parameter.integer("Speed", 1, 1000, 1)

end

function CountdownUpdate()

    Time = Hour..": "..Min.."' "..string.format("%.0f",Sec)..'"'

    if Countdown_Status then

        Sec = Sec + Speed/60

    end

    if Sec >= 59 then

        Sec = 0
        Min = Min + 1

    elseif

        Min >= 59 then
        Min = 0
        Hour = Hour + 1

    end

    pushStyle()

    fill(255)
    stroke(255)
    strokeWidth(2)

    font("AmericanTypewriter")
    text(Time, WIDTH/2, HEIGHT/2)
    text("00: 00' "..'00"',WIDTH/2, HEIGHT/2 - 30)
    text("01: 07' "..'39"',WIDTH/2, HEIGHT/2 - 50)

    line(WIDTH/2-100,HEIGHT/2-15,WIDTH/2+100,HEIGHT/2-15)

    popStyle()

end

function StopTimer()

    Sec, Min, Hour = 0,0,0
    Countdown_Status = false

end

Comments

  • SimeonSimeon Admin Mod
    Posts: 5,054

    Interesting project. I think this is what you want:

    Time = string.format("%02d: %02d' %02.0f", Hour, Min, Sec)

  • Posts: 5

    Worked like a charm @Simeon Thank you!

  • dave1707dave1707 Mod
    edited March 6 Posts: 7,836

    The “ after seconds isn’t included in the above fix. Here is a correction for the “ to show after the seconds. I added \” to the string.

    `Time = string.format("%02d: %02d' %02.0f\"", Hour, Min, Sec)`
    

    Also, there is a slight shift in the display as the timer runs because of the AmericanTypewriter font. Selecting some of the other fonts will eliminate that slight shift. It just depends on how the font displays each character.

    There’s a problem when switching seconds to minutes and minutes to hours. A second is gained going to the next minute and a minute is gained going to the next hour.

  • dave1707dave1707 Mod
    Posts: 7,836

    @Spartan Just to show you a different way of doing a timer.

    displayMode(FULLSCREEN)
    
    function setup()
        fill(255, 0, 0, 255)
        run=0
        msg="Tap to start timer"
        font("Baskerville-SemiBoldItalic")
    end
    
    function draw()
        background(204, 226, 200, 255)    
        if run==0 then
            a=ElapsedTime
            t=0
        elseif run==1 then
            t=(ElapsedTime-a)
        end
        s=t%60
        m=(t//60)%60
        h=t//3600
        str=string.format("%02.f : %02.f : %04.1f",h,m,s)
        fontSize(130)
        text(str,WIDTH/2,HEIGHT/2) 
        fontSize(20)
        text(msg,WIDTH/2,HEIGHT/2-80)
        fontSize(15)
        text("H",WIDTH/2-200,HEIGHT/2+45)
        text("M",WIDTH/2+35,HEIGHT/2+45)
        text("S",WIDTH/2+265,HEIGHT/2+45)
    end
    
    function touched(t)
        if t.state==BEGAN then
            run=(run+1)%3
            if run==0 then
                msg="Tap to start timer"
            elseif run==1 then
                msg="Tap to stop  timer"
            elseif run==2 then
                msg="Tap to reset timer"
            end
        end
    end
    
  • Posts: 5

    @dave1707 I addressed the slight shift by switching to a fixed-pitch font like courier, so its only a noticeable issue when it hits 100 hours, which I doubt will ever happen

Sign In or Register to comment.