Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

A Simple Clock »

You do not own this project, so changes will not be saved

/**
 * A simple clock outline
 */

onEachFrame() do
    fill( :blue )
    
    // get current time
    now    = new Time()
    hour   = (now.hours() % 12) / 12
    minute = now.minutes() / 60
    second = (now.seconds() + now.milliseconds()/1000) / 60
    
    // currently, time is in whole units.
    // This ensures hours work as a fraction
    minute = minute + second/60
    hour = hour + minute/12

    translate( getScreenWidth()/2, getScreenHeight()/2 )
    
    renderClock() do
        drawCircle( 1, 1, 300, 300, true )
        
        drawHand( second, 188, 1, 45, 90, 155 )
        drawHand( minute, 155, 2, 30, 30, 160 )
        drawHand( hour, 100, 4, 20, 10, 165 )
    end
end

/**
 * Yields twice. First time is for drawing the shadow,
 * the second is to draw the clock proper.
 */
def renderClock()
    setColor( :black, 0.4 ) do
        translate( 1, 1 )
        
        yield
        
        undoTransform()
    end
    
    setColor( :white ) do
        yield
    end
end

/**
 * Draws a clock hand.
 * 
 * @param hand This is how far the hand is around the clock, from 0.0 to 1.0.
 * @param length The clock hand length, in pixels.
 * @param width The width of the clock hand, in pixels.
 */
def drawHand( hand, length, width, back, swipeAngle, outerDist )
    // the -90 is because 0 on a clock is at the top,
    // but 0 on an angle is on the right
    angle = (hand * 360.toRadians()) - 90.toRadians()
    
    NUM_INCREMENTS = swipeAngle / 2
    swipeAngle = swipeAngle.toRadians()
    inc = swipeAngle / NUM_INCREMENTS

    NUM_INCREMENTS.times() do |i|
        setAlpha( (getAlpha()/2) * (1 - (i/NUM_INCREMENTS)) ) do
            endAngle = angle - inc*i
            start = endAngle - inc
            
            fillSegment( 0, 0, length-back, start, endAngle, true )
        end
    end
    
    fillInnerSegment( 0, 0, outerDist, outerDist+3, angle-swipeAngle/2, angle, true )

    translate( -back*angle.cos(), -back*angle.sin() )
    
    drawLine(
            0, 0,
            length*angle.cos(), length*angle.sin(),
            width
    )
    undoTransform()
end

ERRORS

YOUR BROWSER DOES NOT SUPPORT HTML5!

Please use one of these instead

Our games cannot run in your browser