This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-03-04 20:53:42

coolhogs
Scratcher
Registered: 2011-07-26
Posts: 1000+

How do you make a clock in scratch?

How do you make one?


Get ready for domination of:  tongue

Offline

 

#2 2012-03-04 21:04:46

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: How do you make a clock in scratch?

Digital or analog?

Offline

 

#3 2012-03-04 21:40:14

coolhogs
Scratcher
Registered: 2011-07-26
Posts: 1000+

Re: How do you make a clock in scratch?

Greenatic wrote:

Digital or analog?

digital


Get ready for domination of:  tongue

Offline

 

#4 2012-03-04 22:23:16

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How do you make a clock in scratch?

You'd need a way to print the numbers and a way to get the starting time.  Many simply ask their users to put in the current time.  I'd then reset the timer and do this:

set [hours] to ((original hour) + (round( (timer/3600)-.5) ) mod [12]
set [minute] to ( ( (original minute) + (round((timer/60)-.5) ) ) mod [60]

I think that's the script  hmm

I'd put it in scratchblocks, but I guess it doesn't work for that many operators.

Last edited by MoreGamesNow (2012-03-04 22:24:07)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2012-04-03 18:01:36

aryabtsev
Scratcher
Registered: 2012-02-05
Posts: 81

Re: How do you make a clock in scratch?

Or, if you use timer for something else, use

when flag clicked
   forever
      change [seconds v] by (1)
      if <(seconds) > (59)>
         set [seconds v] to (0)
         change [minutes v] by (1)
      end
      if <(minutes) > (59)>
         set [minutes v] to (0)
         change [hours v] by (1)
      end
      if <(hours) > (23)>
         set [hours v] to (0)
      end
      wait (0.99) secs
   end
Although this clock can be a little off.

Last edited by aryabtsev (2012-04-03 18:01:56)


http://i.imgur.com/NX7AO.jpg

Offline

 

#6 2012-04-09 15:46:51

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: How do you make a clock in scratch?

If you are going to do a regular (old-style, non-digital) clock, create a minute hand sprite and a second hand sprite, along with a sensor sprite positioned at the twelve.
Second hand:

when gf clicked
forever
 repeat until < (touching [sensorsprite v]) >
  if <(timer)> > (1)
   reset timer
   turn (6) degrees
end
if < (touching [sensorsprite v]) >
 broadcast [minute]
 
And then for the minute hand:
forever 
 when i receive [minute]
 turn (6) degrees
 
If you wanted to do hours, you'd do, in the "minute hand" sprite, a "broadcast hour" block
broadcast [hour]
 
And then you'd do for the hour scripts exactly what you did for the minute hand, except for the hour hand you'd turn 30 (360/12) degrees instead of 6 degrees.

I hope you found my information helpful. Unless, of course, you are not doing a normal clock and are in fact doing a digital clock. In that case, I just wasted about twenty minutes. JK  smile


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#7 2012-04-09 16:10:42

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: How do you make a clock in scratch?

mythbusteranimator wrote:

If you are going to do a regular (old-style, non-digital) clock, create a minute hand sprite and a second hand sprite, along with a sensor sprite positioned at the twelve.
Second hand:

when gf clicked
forever
 repeat until < touching [sensorsprite v]? >
  if <(timer) > (1)>
   reset timer
   turn cw (6) degrees
end
if < touching [sensorsprite v]? >
 broadcast [minute  v]
 
And then for the minute hand:
forever 
 when i receive [minute  v]
 turn cw (6) degrees
 
If you wanted to do hours, you'd do, in the "minute hand" sprite, a "broadcast hour" block
broadcast [hour  v]
 
And then you'd do for the hour scripts exactly what you did for the minute hand, except for the hour hand you'd turn 30 (360/12) degrees instead of 6 degrees.

I hope you found my information helpful. Unless, of course, you are not doing a normal clock and are in fact doing a digital clock. In that case, I just wasted about twenty minutes. JK  smile

Scratchblocks fixed.   wink

Offline

 

#8 2012-04-09 16:27:34

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: How do you make a clock in scratch?

MoreGamesNow wrote:

You'd need a way to print the numbers and a way to get the starting time.  Many simply ask their users to put in the current time.  I'd then reset the timer and do this:

set [hours] to ((original hour) + (round( (timer/3600)-.5) ) mod [12]
set [minute] to ( ( (original minute) + (round((timer/60)-.5) ) ) mod [60]

I think that's the script  hmm

I'd put it in scratchblocks, but I guess it doesn't work for that many operators.

You don't need a way to print.
You could always just have 4 each number place in the clock. First and third sprite would have costumes that have the numbers 1 through 5.
And the second and fourth would have costumes that have the numbers 1 through 9.

Seems like a lot, but it works.  tongue

Offline

 

#9 2012-04-09 23:22:30

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How do you make a clock in scratch?

ProgrammingFreak wrote:

MoreGamesNow wrote:

You'd need a way to print the numbers and a way to get the starting time.  Many simply ask their users to put in the current time.  I'd then reset the timer and do this:

set [hours] to ((original hour) + (round( (timer/3600)-.5) ) mod [12]
set [minute] to ( ( (original minute) + (round((timer/60)-.5) ) ) mod [60]

I think that's the script  hmm

I'd put it in scratchblocks, but I guess it doesn't work for that many operators.

You don't need a way to print.
You could always just have 4 each number place in the clock. First and third sprite would have costumes that have the numbers 1 through 5.
And the second and fourth would have costumes that have the numbers 1 through 9.

Seems like a lot, but it works.  tongue

The first only needs a one. xD

Offline

 

#10 2012-04-09 23:28:05

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: How do you make a clock in scratch?

ProgrammingFreak wrote:

MoreGamesNow wrote:

You'd need a way to print the numbers and a way to get the starting time.  Many simply ask their users to put in the current time.  I'd then reset the timer and do this:

set [hours v] to <<(original hour) + <round <<<timer>/[3600]>-[0.5]>> > mod [12]>
set [minute v] to << (original minute) + <round<<<timer>/[60]>-[.5]> > > mod [60]>
I think that's the script  hmm

I'd put it in scratchblocks, but I guess it doesn't work for that many operators.

You don't need a way to print.
You could always just have 4 each number place in the clock. First and third sprite would have costumes that have the numbers 1 through 5.
And the second and fourth would have costumes that have the numbers 1 through 9.

Seems like a lot, but it works.  tongue

It does work with that many operators. Never underestimate the power of scratchblocks!

Offline

 

#11 2012-04-10 07:57:57

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: How do you make a clock in scratch?

well, you could do this for the counting:

when gf clicked
set [secs v] to (0)
set [mins v] to (0)
set [hrs v] to (0) // you can set them to something else or let the user enter values
forever
 wait (1) secs
 change [secs v] by (1)
 if <(secs) = (60)>
  set [secs v] to (0)
  change [mins v] by (1)
  if <(mins) = (60)>
   set [mins v] to (0)
   change [hrs v] by (1) 
   if <(hrs) = (24)>
    set [hrs v] to (0)
   end
  end
 end
 broadcast [display v]
If you want it more accurate, or if you wanted it to be a stopwatch,  you can use the timer:

when gf clicked 
set [set secs v] to (0) //these are the variables you need to 
set [set mins v] to (0) //change if you want to set the time yourself
set [set hrs v] to (0)
reset timer 
forever
 set [secs v] to ((round ((timer) - (5))) + (set secs)) // round down to nearest integer
 set [millisecs v] to (((timer) - (secs)) * (1000)) 
 set [mins v] to (round ((((secs)/(60)) - (5)) + (set mins))
 set [secs v] to ((secs) mod (60))
 set [hrs v] to (round ((((mins)/(60)) - (5)) + (set hrs))
 set [mins v] to ((mins) mod (60))
 set [hrs v] to ((hrs) mod (24))
 broadcast [display v] and wait

If you wanted it to be a stopwatch counting down,
when gf clicked
all of the old code, 
replacing every (timer)
with ((((set hrs) * (3600)) + (((set mins) * (60)) + (set secs))) - (timer))
Note that changing the time is more complicated than before, so do

when i receive [change time v] 
ask [new hours? (-1 to keep it the same)] and wait
if <(answer) > [0]>
 set [set hrs v] to (answer)
else
 set [set hrs v] to (hrs)
end
ask [new minuites? (-1 to keep it the same)] and wait
if <(answer) > [0]>
 set [set mins v] to (answer)
else
 set [set mins v] to (mins)
end
ask [new seconds? (-1 to keep it the same)] and wait
if <(answer) > [0]>
 set [set secs v] to (answer)
else
 set [set secs v] to (secs)
end
reset timer

Now for the display if you want analogue, you need 3 sprites, for the hour hand, minute hand, and second hand. They should all be straight lines pointing up with the hour hand shorter than the rest and the second hand a different colour to the rest. Set the costume centre of each one to the end of the line, and put them all at the same position. Give each one a LOCAL (for this sprite only)  variable called point at.

In the hour hand:
when i receive [display v]
set [point at v] to ((hrs) * (30))
set [point at v] to ((point at) + (((mins) / (60)) * (30)))
point in direction (point at)
In the minute hand:
when i receive [display v]
set [point at v] to ((mins) * (6))
set [point at v] to ((point at) + (((secs) / (60)) * (6)))
point in direction (point at)
In the second hand:
when i receive [display v]
set [point at v] to ((secs) * (6))
point in direction (point at)
Now, if you want digital, it is slightly easier.

when gf clicked // This script is so the colons in the display flash each second
set [colons? v] to (0) //if you don't want that, 
forever
 set [colons? v] to ((1) - (colons?)
 wait (1) secs

when i receive [display v]
if <(display) = [24hour]>
 if <(colons?) = (1)>
  set [time v] to (join (hrs) (join [:] (join (mins) (join [:] (secs))
 else
  set [time v] to(join (hrs) (join [ ] (join (mins) (join [ ] (secs))
 end
 stop script
end
if <(display) = [stopwatch]>
  if <(colons?) = (1)>
  set [time v] to (join (hrs) (join [:] (join (mins) (join [:] (join (secs) (join [:] (milisecs)))))))
 else
  set [time v] to (join (hrs) (join [ ] (join (mins) (join [ ] (join (secs) (join [ ] (milisecs))))))) 
 end
 stop script
end
if <(display) = [12hour]>
if <(colons?) = (1)>
 if <<(hrs) = (0)> or <(hrs) = (12)>>
  set [time v] to [12]
 else
  set [time v] to (hrs)
 end
 set [time v] to (join (time) (join [:] (join (mins) (join [:] (secs))
 if <(hrs) < (12)>
  set [time v] to (join (time) [ AM])
 else
  set [time v] to (join (time) [ PM])
 end
else
if <<(hrs) = (0)> or <(hrs) = (12)>>
   set [time v] to [12]
  else
   set [time v] to (hrs)
  end
  set [time v] to (join (time) (join [ ] (join (mins) (join [ ] (secs))
  if <(hrs) < (12)>
   set [time v] to (join (time) [ AM])
  else
   set [time v] to (join (time) [ PM])
  end
end
end
Then you can just show the variable time. If you want it bigger, get a sprite with a costume for each digit plus a colon, and add this script to the end of the above script:

set [index v] to (0)
go to x:(xpositionoftime) y:(ypositionoftime)
repeat until <(index) = (length of (time))>
 change [index v] by (1)
 if <(letter (index) of (time)) = [ ]>
  move (20) steps
  stop script
 end
 if <(letter (index) of (time)) = [0]>// costumes with numerical names automaticly 
  // change to "01", "11", "21", ect... so there is no costume 0.
  switch to costume [01 v]
 else
  switch to costume (letter (index) of (time))
 end
 stamp
 move (20) steps
end 
Hope that helps!

(don't you wish scratch had a (date/time) block?)

Last edited by joefarebrother (2012-04-11 06:06:36)


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#12 2012-04-20 12:53:58

jgbuddy
Scratcher
Registered: 2011-12-31
Posts: 2

Re: How do you make a clock in scratch?

3600 seconds in a hour if that helps  hmm

Offline

 

Board footer