How to Create a Clock

Материал из Поле цифровой дидактики

Шаблон:See also This is a tutorial on how to create a clock. This clock returns the player's time because this tutorial uses the current [ v] block, which is local.

Analogue Clock

Preparation

To start, create three Sprites: one for the hour hand, one for the minute hand, and one for the second hand.

Шаблон:Warning

Coding

Second Hand

This is the script for the second hand:

when gf clicked
forever
point in direction ((0) + ((current [second v]) * (6))) // x6 because 360/60=6.
end

Minute Hand

This is the script for the minute hand:

when gf clicked
forever
point in direction ((0) + ((current [minute v]) * (6)))
end

Hour Hand

This is the script for the hour hand:

when gf clicked
point in direction (0)
turn cw (((current [hour v]) * (30)) + ((current [minute v]) / (2))) degrees // Calculations: 360/12=30 and 1/2=0.5;0.5*60=30.
forever
wait until <([direction v] of (minute hand v)) = (([direction v] of (minute hand v)) + (6))
turn cw (0.5) degrees
wait (2) secs // This is so it would not turn 15 degrees.

Digital Clock

Preparation

Create only one variable named "Digital Time" or "Time" to start.

Coding

24 Hour

This is the script on how to make a 24 hour digital clock:

when gf clicked
forever
set [Time v] to (join (current [hour v]) (join [ : ] (join (current [minute v]) (join [ : ] (current [second v])))))
end

12 Hour

This is the script for a 12 hour clock:

when gf clicked
forever
if <(current [hour v]) = [0]> then
set [Time v] to (join ((current [hour v]) + (12)) (join [ : ] (join (current [minute v]) (join [ : ] (join (current [second v]) [.am]))))
else
if <<(current [hour v]) > [0]> and <(current [hour v]) < [12]>> then
set [Time v] to (join (current [hour v]) (join [ : ] (join (current [minute v]) (join [ : ] (join (current [second v]) [.am]))))
else
if <(current [hour v]) = [12]> then
set [Time v] to (join (current [hour v]) (join [ : ] (join (current [minute v]) (join [ : ] (join (current [second v]) [.pm]))))
else
if <<(current [hour v]) > [12]> and <(current [hour v]) < [24]>> then
set [time v] to (join ((current [hour v]) - (12)) (join [ : ] (join (current [minute v]) (join [ : ] (join (current [second v]) [.am]))))
end
end
end
end
end

Examples

See Also

nl:Klok