Turtle Animation - Software Coding For Kids
Turtle Animation - Software Coding For Kids
HTTPS://SOFTWAREPROGRAMMING4KIDS.COM/>
LOGO, Python, Tkinter, Pygame, Turtle, Sprite
Turtle Animation
Animation
Animation means moving the images on the screen. The turtle module can be
used to do animation of images. The animation using LOGO <
https://softwareprogramming4kids.com/logo-animation/> , Tkinter, <
https://softwareprogramming4kids.com/canvas-widget/3/> and Pygame <
https://softwareprogramming4kids.com/pygame-drawing-and-
animation/3/#animationpyg-anchor> was done on the previous pages. Let us
now do animation with the turtle module.
For animation control, we will use the following turtle module’s functions. These
functions were described on an earlier <
https://softwareprogramming4kids.com/turtle-functions/#anchor-animation-
control> page. They are repeated here for convenient reference.
turtle.delay(delay) Function:
Syntax:
This function sets a time delay between two consecutive frame updates. The
higher the delay number, the slower the animation. Example: screen.delay(5)
adds a 5 milli-seconds delay between the frames.
turtle.tracer(n,delay) Function:
Syntax:
turtle.update() Function:
Let us now create a few animation programs using the turtle module.
For our first animation program, we will animate the turtle to move across the
screen in the x-direction.
In each iteration of the while loop, the turtle moves by a few pixels. As Python
computes the new position of the turtle, we need to make sure that each frame
is completed before it is displayed. This is done by using the function
screen.tracer(0). This function tells the Screen to not show anything until
the frame has been completed. After the frame is completed, we display the
screen with the updated image using the function screen.update().
The while loop continues to move the turtle image and update the screen frame
by frame with the turtle’s new position.
Line 3): Set the window size a width = 320 pixels and height = 320 pixels.
Line 8: Set the speed at which the turtle moves across the screen. The
TTL.speed(0) function has a special meaning. It turns off the animation and the
turtle goes as fast as possible.
Line 10: TTL.penup() function moves the pen up so that the turtle does not draw
as it moves on the screen.
Line 11: TTL.goto(-300, 0) statement initializes the starting position of the turtle
as (-300,0)
The event handler method (function) takes the desired actions in response to
events (mouse clicks of key press). The user’s keypad presses and the mouse
clicks are called Screen Events <
https://softwareprogramming4kids.com/turtle-functions/#anchor-screen-
events> . To animate the turtle using the screen events, the following Turtle
module’s functions are used. These functions were discussed on Turtle
Functions < https://softwareprogramming4kids.com/turtle-functions/#anchor-
screen-events> page.
turtle.onkey(fun,key)
The above statement will call the function “next_frame” and associate the
“next_frame” function with the up-arrow key.
turtle.listen()
In addition to binding the key press event to the event handler function, we need
to ensure that the program listens to the key presses. Turtle module function
turtle.listen() enables the program to listen to the key presses.
screen.listen()
window.ontimer(fun, time)
Example: screen.ontimer(next_frame,10)
Now we will write a simple program to move an image as the user presses the
left-arrow, right-arrow, up-arrow, and down-arrow keys.
Note that turtle module recognizes the strings ‘Left’, ‘Right’, ‘Up’, and ‘Down’ as
the respective arrow keys.
Lines 1) through 7): These are the same as in the previous program.
Line 8: trtl.hideturtle()
Define the variables x and y as the coordinates of the turtle position. Also,
define x_step and y_step as the number of pixels the turtle moves each time the
user presses the key. Initialize these variables.
Draw a black dot of radius = 20. This is the dot that will move across the screen
as the user presses the keys.
27: x = x – x_step
Subtract the x_step value from x (the current position of the turtle) when the
left-arrow key is pressed by the user.
Lines 45 through 48: Bind the events toi the arrow keys.
< https://www.monsterinsights.com/?
utm_source=verifiedBadge&utm_medium=verifiedBadge&utm_campaign=verifiedbyMonsterInsights>