HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
computeranimation
Deutsch   English   

14. COMPUTER ANIMATION

 

 

YOU LEARN HERE...

 


how to create animated computer graphics. An animation consists of individual images that change only slightly and are displayed step by step one after the other. As the human eye can only capture around 25 images per second, the result is a smooth, jerk-free movement, just like a movie, if the image sequence is shown fast enough. You can draw the images with the Turtle or load them as image files

 

 

EXAMPLE

 

In your example, you program a rotating propeller. As the animated figure is used several times, it is advantageous to build the program modularly, i.e. to display the figures using functions. This makes your program clearer. Use the functions bogen(), blatt() and propeller() to draw a 3-bladed propeller.

You can describe the animation sequence like this:

repeat:
     draw figure
     wait a short time
     delete the figure
     change the turtle direction a little

Simple figures can be deleted by painting over them with the background color. As a rule, however, it is easier to clear the entire window with clear(). clear() deletes the window.

 

Program:    

from gturtle import *

def bogen():
    repeat 45:
        forward(3)
        right(2)
        
def blatt():
    startPath()
    bogen()
    right(90)
    bogen()
    left(150)
    fillPath()
    
def propeller():
    repeat 3:
        blatt()
        right(120)    
        
hideTurtle()
setFillColor("blue")

repeat:
    propeller()
    delay(40)
    right(4)
    clear()
► Copy to clipboard

 

Example 2: Running clock
First, write a function that draws the clock face. The hand is drawn at a new position every 300 milliseconds. Before you draw the hand at the new position, paint over the hand at the old position with the white color.

 

Program:    

from gturtle import *

def watchFace():
    setPenColor("black")
    penUp()
    repeat 12:
        forward(110)
        dot(10)
        back(110)
        right(30)
    penDown()
    
def hand():
    setPenWidth(5)
    forward(90)
    back(90)
    
watchFace()

a = 0
while True:
   setHeading(a) 
   setPenColor("red")
   hand()
   delay(300)
   setPenColor("white")
   hand()
   a += 30
► Copy to clipboard

 

 

REMEMBER...

  In an animation, a figure is displayed for a short time in a repetition loop, then deleted and displayed again at a slightly different position. To delete the figure, use the clear() or clear(“color”) function if the background is not white. This deletes the entire window, but the turtle remains in its current position. Sometimes it is advantageous to only paint over the animated figure with the background color before drawing it at the new position.

 

 

TO SOLVE BY YOURSELF

 
1.

Draw a simple vehicle and let it “drive” from left to right over the turtle window.
You can improve your program as follows: when the car reaches the right edge (getX() > 300), it is moved back to the starting position on the left edge of the window.

 



2.

A ball moves back and forth from left to right until it reflects on a wall. Then it moves back until it reflects on the left wall.

 

 
3.

Draw a rotating propeller consisting of four rhombus-shaped blades. First define a function blatt() that draws a green-filled rhombus. Then define a function propeller(), which consists of 4 blades. In the animation loop, let the propeller rotate around its own axis.

 

 


 
4.
Draw a flag and swing it up and down around the lowest point of the flagpole.  

 
5.

Invent further images yourself with which you can create a computer animation.