[solved] clock code that doesn't work

Discussions related to graphics (2D and 3D), animation and games programming
ron77
Posts: 6
Joined: Sun 11 Jul 2021, 09:58

[solved] clock code that doesn't work

Post by ron77 »

hello i need some help with this code converted from QB - it's an analog clock code yet it doesn't move and the time is incorrect :(

any help appreciated - I've new to BBC BASIC and to graphics

Code: Select all

      rem _Title "THE MATHEMATICAL ANALOG CLOCK VERSION 3 - WITHOUT CALIBRATION"
      rem Screen 12
      rem 'DEVELOPED 2/17/2019 IN QBASIC BY RON77 AND MISHKA AND ITAY :)
      mode 21
      origin 800,600
      rem hour minute sec pi
      time$="hh:mm:ss"

      hour = val(left$(time$, 2))
      minute = val(mid$(time$, 4, 2))
      sec = val(right$(time$, 2))

      hour = hour + minute / 60 + sec / 3600
      minute = minute + sec / 60

      pi1 = pi



      repeat
  
        cls
        print time$
        print "press ESC to exit  "
  
        circle 320, 240, 200
        circle 320, 240, 215
        for j% = 0 to 59
          line (320 + 190 * cos((90 - j% * 6) * 2 * pi1 / 360)), (240 - 190 * sin((90 - j% * 6) * 2 * pi1 / 360)),((320 + 200 * cos((90 - j% * 6) * 2 * pi1 / 360))), (240 - 200 * sin((90 - j% * 6) * 2 * pi1 / 360))
        next
        for i% = 0 to 11
          line (320 + 205 * cos((90 - i% * 30) * 2 * pi1 / 360)), (240 - 205 * sin((90 - i% * 30) * 2 * pi1 / 360)),((320 + 210 * cos((90 - i% * 30) * 2 * pi1 / 360))), (240 - 210 * sin((90 - i% * 30) * 2 * pi1 / 360))
        next
  
        t = 90 - sec * 6
        t1 = 90 - minute * 6
        t2 = 90 - hour * 30
  
        line 320, 240,(320 + 200 * cos(t * 2 * pi1 / 360)), (240 - 200 * sin(t * 2 * pi1 / 360))
        line 320, 240,(320 + 180 * cos(t1 * 2 * pi1 / 360)), (240 - 180 * sin(t1 * 2 * pi1 / 360))
        line 320, 240,(320 + 120 * cos(t2 * 2 * pi1 / 360)), (240 - 120 * sin(t2 * 2 * pi1 / 360))
        wait 100
  
        sec = sec + 1
        minute = minute + (1 / 60)
        hour = hour + (1 / 3600)
        k$ = get$
  
        If k$ = Chr$(27) Then End
      until
      End Sub
Last edited by ron77 on Mon 12 Jul 2021, 15:38, edited 1 time in total.
ron77
Posts: 6
Joined: Sun 11 Jul 2021, 09:58

Re: [help] clock code that doesn't work

Post by ron77 »

well I got it to move BUT the clock display is upside down (12 o'clock is where 6 o'clock ) :-/ need some help

Code: Select all

   10 rem _Title "THE MATHEMATICAL ANALOG CLOCK VERSION 3 - WITHOUT CALIBRATION"
   20 rem Screen 12
   30 rem 'DEVELOPED 2/17/2019 IN QBASIC BY RON77 AND MISHKA AND ITAY :)
   40 mode 21
   50 origin 800,600
   60 rem hour minute sec pi
      input "what hour is it?: " hour
      input "what minute is it?: " minute
      sec = 0
  120 rem Time$ = ""
      rem Time$ = time$
      rem hour% = valmid$(Time$,17,2) mod 12
      rem minute% = valmid$(Time$,20,2)
      rem sec% = valmid$(Time$,23,2)

  130 hour = hour + minute / 60 + sec / 3600
  140 minute = minute + sec / 60
  150 
  160 pi1 = pi
  170 
  180 
  190 
  200 rem repeat
  210 
  220 clg
  230 print time$
  240 print "press ESC to exit  "
  250 
  260 circle 320, 240, 200
  270 circle 320, 240, 215
  280 for j% = 0 to 59
  290   line (320 + 190 * cos((90 - j% * 6) * 2 * pi1 / 360)), (240 - 190 * sin((90 - j% * 6) * 2 * pi1 / 360)),((320 + 200 * cos((90 - j% * 6) * 2 * pi1 / 360))), (240 - 200 * sin((90 - j% * 6) * 2 * pi1 / 360))
  300 next
  310 for i% = 0 to 11
  320   line (320 + 205 * cos((90 - i% * 30) * 2 * pi1 / 360)), (240 - 205 * sin((90 - i% * 30) * 2 * pi1 / 360)),((320 + 210 * cos((90 - i% * 30) * 2 * pi1 / 360))), (240 - 210 * sin((90 - i% * 30) * 2 * pi1 / 360))
  330 next
  340 
  350 t = 90 - sec * 6
  360 t1 = 90 - minute * 6
  370 t2 = 90 - hour * 30
  380 
  390 line 320, 240,(320 + 200 * cos(t * 2 * pi1 / 360)), (240 - 200 * sin(t * 2 * pi1 / 360))
  400 line 320, 240,(320 + 180 * cos(t1 * 2 * pi1 / 360)), (240 - 180 * sin(t1 * 2 * pi1 / 360))
  410 line 320, 240,(320 + 120 * cos(t2 * 2 * pi1 / 360)), (240 - 120 * sin(t2 * 2 * pi1 / 360))
  420 wait 100
  430 
  440 sec = sec - 1
  450 minute = minute - (1 / 60)
  460 hour = hour - (1 / 3600)
  470 rem k$ = get$
  480 goto 210
  490 
  500 rem until k$ = chr$(27)
  510 
ron77
Posts: 6
Joined: Sun 11 Jul 2021, 09:58

Re: [solved] clock code that doesn't work

Post by ron77 »

okay got help from a friend who told me BBC basic coordination are upsidedown (y=0 means bottom) and suggested how to fix the code here it is :)

Code: Select all

   10 rem _Title "THE MATHEMATICAL ANALOG CLOCK VERSION 3 - WITHOUT CALIBRATION"
   20 rem originally developed in QB AND QB64
   30 rem 'DEVELOPED 2/17/2019 IN QBASIC BY RON77 AND MISHKA AND ITAY :)
   40 rem mod to BBC BASIC BY RON77 IN 12/07/2021
   50 
   60 mode 21
   70 origin 800,600
   80 rem
   90 rem input "what hour is it?: " hour
  100 rem input "what minute is it?: " minute
  110 rem sec = 0
  120 Time$ = time$
  130 hour = valmid$(Time$,17,2) mod 12
  140 minute = valmid$(Time$,20,2)
  150 sec = valmid$(Time$,23,2)
  160 
  170 
  180 
  190 hour = hour + minute / 60 + sec / 3600
  200 minute = minute + sec / 60
  210 
  220 pi1 = pi
  230 
  240 clg
  250 print time$
  260 print "press ESC to exit  "
  270 
  280 circle 320, 240, 200
  290 circle 320, 240, 215
  300 for j% = 0 to 59
  310   line (320 + 190 * cos((90 - j% * 6) * 2 * pi1 / 360)), (240 - 190 * sin((90 - j% * 6) * 2 * pi1 / 360)),((320 + 200 * cos((90 - j% * 6) * 2 * pi1 / 360))), (240 - 200 * sin((90 - j% * 6) * 2 * pi1 / 360))
  320 next
  330 for i% = 0 to 11
  340   line (320 + 205 * cos((90 - i% * 30) * 2 * pi1 / 360)), (240 - 205 * sin((90 - i% * 30) * 2 * pi1 / 360)),((320 + 210 * cos((90 - i% * 30) * 2 * pi1 / 360))), (240 - 210 * sin((90 - i% * 30) * 2 * pi1 / 360))
  350 next
  360 
  370 t = 90 - sec * 6
  380 t1 = 90 - minute * 6
  390 t2 = 90 - hour * 30
  400 
  410 line 320, 240,(320 + 200 * cos(t * 2 * pi1 / 360)), (240 + 200 * sin(t * 2 * pi1 / 360))
  420 line 320, 240,(320 + 180 * cos(t1 * 2 * pi1 / 360)), (240 + 180 * sin(t1 * 2 * pi1 / 360))
  430 line 320, 240,(320 + 120 * cos(t2 * 2 * pi1 / 360)), (240 + 120 * sin(t2 * 2 * pi1 / 360))
  440 wait 100
  450 
  460 sec = sec + 1
  470 minute = minute + (1 / 60)
  480 hour = hour + (1 / 3600)
  490 
  500 
  510 goto 230
  520 
johnno56
Posts: 15
Joined: Tue 13 Jul 2021, 20:15

Re: [solved] clock code that doesn't work

Post by johnno56 »

Cool clock...

Does it come in blue? Nah. Kidding. Good job!

J
DDRM

Re: [solved] clock code that doesn't work

Post by DDRM »

Nice!

I had a little play with your code: I hope you might find it useful. I removed line numbers, made it more block-structured, and spun a few bits out into procedures for clarity. I've tried to make it more "efficient" by only drawing the clock face once, and using XOR plotting to draw the hands (basically you draw them, do your wait, then draw them again, which erases them, before drawing them in their new positions). There's a small "deliberate" error in the way I've done it: can you spot it? (Hint: think about what order things are drawn and cleared, and how XOR plotting works). I've also calculated some things only once (for example, 2*PI/360) by making a variable for them.

I thought it was a bit redundant to have an ORIGIN statement and an offset for the clock, so I've removed the latter. I've also used PRINT TAB so that the text version of the time is always in the same place rather than moving down, which I found distracting.

In deference to Johnno I've made it blue... ;-)

Code: Select all

      REM _Title "THE MATHEMATICAL ANALOG CLOCK VERSION 3 - WITHOUT CALIBRATION"
      REM originally developed in QB AND QB64
      REM 'DEVELOPED 2/17/2019 IN QBASIC BY RON77 AND MISHKA AND ITAY :)
      REM mod to BBC BASIC BY RON77 IN 12/07/2021
      REM Played with by David Marples 14/07/2021

      MODE 21
      ORIGIN 800,600
      piconv = 2*PI/360
      blue%=4:white%=15
      PROCDrawFace(blue%,white%)
      GCOL 3,11  :REM Can you work out why I've gone for colour 11 to get white (colour 15) here?  ;-)
      REPEAT
        Time$ = TIME$
        sec = VALMID$(Time$,23,2)
        minute = VALMID$(Time$,20,2) + sec / 60
        hour = (VALMID$(Time$,17,2) MOD 12) + minute / 60
  
        PRINT TAB(0,1);TIME$
        PRINT "press ESC to exit  "
  
        t = 90 - sec * 6
        t1 = 90 - minute * 6
        t2 = 90 - hour * 30
  
        PROCDrawLine(t,180,1)
        PROCDrawLine(t1,160,2)
        PROCDrawLine(t2,120,3)
        WAIT 100
        PROCDrawLine(t,180,1)
        PROCDrawLine(t1,160,2)
        PROCDrawLine(t2,120,3)
  
      UNTIL FALSE
      END
      :
      DEFPROCDrawFace(backcol,forcol)
      GCOL backcol
      CIRCLE FILL 0, 0, 215
      GCOL forcol
      CIRCLE 0, 0, 215
      CIRCLE 0, 0, 200
      FOR j% = 0 TO 59*6 STEP 6
        LINE (190 * COS((90 - j%) * piconv)), (-190 * SIN((90 - j%) * piconv)),((200 * COS((90 - j%) * piconv))), ( -200 * SIN((90 - j%) * piconv))
      NEXT
      FOR i% = 0 TO 11*30 STEP 30
        LINE (205 * COS((90 - i%) * piconv)), (-205 * SIN((90 - i%) * piconv)),((210 * COS((90 - i%) * piconv))), (-210 * SIN((90 - i%) * piconv))
      NEXT
      ENDPROC
      :
      DEFPROCDrawLine(a,length%,thick%)
      VDU 23,23,thick%|
      LINE 0, 0,(length% * COS(a * piconv)), (length% * SIN(a * piconv))
      ENDPROC
johnno56
Posts: 15
Joined: Tue 13 Jul 2021, 20:15

Re: [solved] clock code that doesn't work

Post by johnno56 »

DDRM,

I get a syntax error. No editing. Just cut and paste into the IDE.

Not e very informative error message... No problem. Into the BBC BASIC Programmer's Manual I go...
"Listing the line an error occurs in ON ERROR"

Copy and pasted the "on err" and Functions.
Reran. Same syntax error with no reported line number.

I am using BBCEdit 0.38.1
DDRM

Re: [solved] clock code that doesn't work

Post by DDRM »

Hi Johnno,

Interesing: I've tried copying it from my post ("select all" at the top of the code box, ctrl-C, and ctrl-V into the editor) into both BB4W and BBC-SDL, and it ran without problems in both. Are you sure you copied exactly everything?

I've got a slightly out of date version of BBC-SDL, but I'd be surprised if that's the problem - there's no fancy code in it.

Ooh, one thing: I don't use lower case keywords. It's possible that there is something there that is causing a problem (i.e. I've used a lower case variable which clashes with a keyword - one reason I use UC for keywords and LC for variables....). ...goes away and tries... Yes, that's the problem. Try selecting upper case keywords (or more specifically, DE-selecting lower case keywords (in options).

Best wishes,

D
johnno56
Posts: 15
Joined: Tue 13 Jul 2021, 20:15

Re: [solved] clock code that doesn't work

Post by johnno56 »

It's definitely a puzzler... But, your suggestions, were the first things I tries.

Ok. After an extreme scrutinize (lots of guessing... lol) managed to get it working.

The problem I found:

In the DrawLine PROC, the cut and paste for some reason, did not include the vertical bar after thick%

Now it runs like, and I couldn't resist it, like clock work... lol

Many thanks for your help.

J

ps: I do appreciate the 'blue'... lol