Adding Life - Introduction to Animation

 

Clock with ticking second hand:

We need to separate the moving and stationary parts of the clock to simplify the solution. The parts are: (1) Dial – stationary, (2) Second hand – moving

 

The dial needs to be drawn only once. It is made up of a circle and line marks. We will reuse the following procedures that we have defined earlier in the book: dcircle, jump

 

The Dial procedure will draw the complete dial:

 

 

ERASE "Dial

TO Dial

  jump -200 0

  dcircle 400

  ; Move turtle to the center of the circle

  jump 200 0

 

  ; Draw lines marks for 12, 3, 6 and 9 o'clock

  REPEAT 4 [

    PU FD (200-20) PD FD 20 ; Draw mark

    PU BK 200 PD RT 90 ; Back to original position

  ] 

END

 

The following procedure will draw the ticking second hand:

ERASE "Second.Hand

TO Second.Hand

  SETPENSIZE 3

  REPEAT 60 [

    FD (200-40) BK (200-40) ; Draw second hand

    WAIT 60        ; Wait for one second

    PENERASE       ; Turn the Erase mode on

    FD (200-40) BK (200-40) ; Erase second hand

    PENPAINT       ; Restore the Pen

    RT 360/60      ; Turn right

  ]

END

 

The following procedure puts everything together:

ERASE "Clock

TO Clock

  SETPENSIZE 6 PENPAINT

  Dial

  SETPENSIZE 3

  REPEAT FOREVER [

    Second.Hand

  ]

END

 

? Clock

This should give us a clock with a ticking second hand. To terminate the program, use Control-Q (Alt-S in UCB Logo 6.0).

Spinning Bullock-cart wheel:

The following code draws the wheel and sets it in motion:

 

CS SETBG 0 SETPC 7 HT PD bcwheel

REPEAT FOREVER [

  SETPC 4

  REPEAT 1 [

    PU FD 50 FD 20 PD FD 50 BK 50 PU BK 20 BK 50 PD

  ]

  WAIT 10 SETPC 7

  REPEAT 1 [

    PU FD 50 FD 20 PD FD 50 BK 50 PU BK 20 BK 50 PD

  ]

  WAIT 10 RT 360/10

]

 

Rotating flower of triangles:

Refer to the triangle procedure defined several times earlier.

ERASE "trflower

TO trflower :size :N

  REPEAT :N [triangle 100 RT 360/:N]

END

 

The following instructions will create the rotation effect:

 

HT SETPC 3

REPEAT FOREVER [

  trflower 100 15

  WAIT 3  CLEAN  RT 5

]

 

Animated Traffic Light:

We will reuse the following procedures created earlier for “Traffic Light”:

 

1.      dcircle:  Circle of given diameter

2.      f.dcircle: Filled circle of given diameter

3.      lamp: Lamp of given size and color

4.      frectangle: Rectangle of given dimensions filled with color

5.      casing: Rectangular casing of given dimensions filled with given color

6.      Traffic.Light: Traffic signal with three lights – red, yellow and green

 

To get an animated traffic light, we need to display the following three pictures in succession, with a suitable time delay in between:

Red lamp, Black lamp, Black lamp

Black lamp, Yellow lamp, Black lamp

Black lamp, Black lamp, Green lamp

 

To create the pictures, here are two possible alternative solutions.

1.      Modify the traffic.light procedure to accept inputs for color of all three lamps, and then call the procedure three times as follows:

   traffic.light 1 4 0 0  ;Red on, rest off

   traffic.light 1 0 6 0  ;Yellow on, rest off

   traffic.light 1 0 0 2  ;Green on, rest off

Use WAIT commands in between to create delay.

 

2.      Modify the traffic light procedure to do everything.

 

The following program demonstrates the second approach.

 

ERASE "traffic.light

TO traffic.light :m       ;m is the multiplier for size

  ; Draw the pole and signal casing

  Casing 100*:m 20*:m 11

  PU FD 100*:m LT 90 FD 15*:m RT 90 PD

  Casing 150*:m 50*:m 11

 

  ; The blinking traffic lights

  REPEAT FOREVER [

    ; Step 1 - RED on, others OFF

    ;------------------------------ 

    PU FD 25*:m PD Lamp 4 0.5*:m     ; Red lamp ON

    PU FD 50*:m PD Lamp 0 0.5*:m     ; Yellow lamp OFF

    PU FD 50*:m PD Lamp 0 0.5*:m     ; Green lamp OFF

    PU BK 125*:m PD

 

    WAIT 120                           ; Pause for 2 secs

    ; Step 2 - Yellow on, others OFF

    ;------------------------------ 

    PU FD 25*:m PD Lamp 0 0.5*:m     ; Red lamp OFF

    PU FD 50*:m PD Lamp 6 0.5*:m     ; Yellow lamp ON

    PU FD 50*:m PD Lamp 0 0.5*:m     ; Green lamp OFF

    PU BK 125*:m PD

 

    WAIT 120                           ; Pause for 2 secs

 

    ; Step 3 - Green on, others OFF

    ;------------------------------ 

    PU FD 25*:m PD Lamp 0 0.5*:m     ; Red lamp OFF

    PU FD 50*:m PD Lamp 0 0.5*:m     ; Yellow lamp OFF

    PU FD 50*:m PD Lamp 2 0.5*:m     ; Green lamp ON

    PU BK 125*:m PD

 

    WAIT 120                           ; Pause for 2 secs

  ]

End

;---------------------------------------

 

? traffic.light 1   ;do you see a blinking traffic light?

 

To terminate the program, use Control-Q (Alt-S in UCB Logo 6.0).

Modify the procedure further to accept inputs for the time delays.

 

Moving Arrow:

Moving arrow with inputs for size, thickness, color, and speed:

;---------------------------------------

; Moving.Arrow: Draws a Moving Block Arrow

;---------------------------------------

ERASE "Moving.Arrow

TO Moving.Arrow :m :pensize :pencolor :speed

  HT SETPENSIZE :pensize SETPC :pencolor

 

  REPEAT FOREVER [

    Block.Arrow :M

    PU RT 90 FD 10 LT 90 PD

    WAIT :speed

    CLEAN

  ]

END

Earth revolving around the sun:

We have used the following procedures before:

1.      FDcircle - Draw a circle filled with color, and

2.      Jump – Move the Turtle to a new location without drawing a line

 

Using these procedures, we will create the animation of the earth going around the sun in a circular orbit. Run the following instructions:

 

CS HT SETBG 7    ; White background

jump -50 0

 

SETPC 6          ; Yellow color

F.DCircle 50      ; Sun

 

; Jump to center of the sun

jump 50/2 0

 

; Set initial position for earth

jump 0 175 RT 90    

 

; Earth going around in a circular motion

REPEAT FOREVER [

  REPEAT 180 [

    ; Go around in circular motion, do not draw the circle

    PU FD 7 RT 2 PD

    ; Draw a Blue cirlce for earth

    SETPC 1 F.DCircle 20 

    WAIT 6

    ; Erase earth with White circle

    SETPC 7 F.DCircle 20

  ]

]