Javatpoint Logo
Javatpoint Logo

Creating a simple animation using Turtle in Python

There is a Turtle module for Python. It has a turtle(pen) and a cardboard sketching surface. Move the turtle(pen) to construct anything on the screen. The turtle can be moved using other operations, including forward() and backward().

We can use several drawing methods on this, just like we would on a drawing board. The basic commands control how the drawing pen actually moves. We proceed from a very basic application to utilising Python Turtle to produce an animation.

Animations :

At its core, every animation (as well as every moving image) is merely a collection of still images that are shown quickly enough to provide the impression of continuous motion. A frame is a term used to describe every image. The act of dramatically altering each frame from the one before and quickly displaying them one after the other to provide the impression of continuous motion is referred to as "animating," as in "bringing to life".

It's usually best to master the fundamentals as soon as you can, therefore even though there is an easier option, we didn't start out that way. Instead of drawing the turtle yourself, the easier option is to utilise an image as its shape.

Because turtle will only take the first frame and discard the others, it is advised to use a static.gif picture rather than an animated one.

Managing speed :

We also need to make sure that each time we advance, it is a lot smaller distance. The explanation is that the forward movement only actually occurred after every tenth frame since the screen, when deciding when to display frames, used many frames?possibly 10?to show the drawing of each square. Our animation would only go ahead once every second if its frame rate was also 10 frames per second. When we choose to manually display a frame only once a square is finished, 10 frames now represent 10 squares rather than just 1, and forward movement occurs 10 times per second rather than just once. The square appears to go ahead much faster because there is significantly more forward motion for the same number of frames. In order to enable the square or any object to animate forward again at a slower, more manageable speed, we must consequently shorten the distance travelled between each frame.

Creating Animations using Turtle :

Let's start with a simple program first and then we will move ahead.

Drawing a square shaped animation in turtle using Python :

First, we imported the turtle module to be used in our python program.

Then we created an instance of the turtle module for drawing.

Next, we created the screen for drawing using the turtle module.

After that, we set the size of the screen using the setup() method and set the background colour of the screen to pink using bgcolor() method.

Next, we set the colour of the drawing pen to light blue using the pencolor() method along with setting the size of the pen using the pensize() method.

Then we set the speed of the drawing pen using the speed() method along with setting it's shape to a turtle using the shape() method.

Then, the main part of our animation was to be done. We started creating a square by the help of the forward() and right() methods.

Then we used the penup() method to lift the turtle and moved it to a new coordinates using the goto() method and dropped it there using the pendown() method.

lastly, we set the colour of the pen using pencolor() method to print the heading using the write() method. After all this, we completed the drawing the hid the turtle using the hideturtle() or ht() method.

Code :

Output:

The screenshot of the output of the aforementioned code is attached below which describes the animation of drawing a square using turtle in Python.

Creating a simple animation using Turtle in Python
Creating a simple animation using Turtle in Python
Creating a simple animation using Turtle in Python

Drawing a turtle-racing board animation in turtle using Python :

Earlier, we created a simple square in order to make you understand the basic ideology behind creating animations using turtle in Python. Now, we will take a step further and will create an animation involving a racing track, where few turtles will race around and thus will make it an animated program.

Method to follow :

  • Import the necessary modules first.
  • In the turtle module, various pointer shapes are accessible (like arrow, classic and turtle). We require the "turtle" and "classic" shapes since this is a "Turtle Race."
  • The racing circuit will be drawn using the traditional shape.
  • List the first turtle's specifications, place it on the racetrack, and get it ready to go.
  • For the remaining turtles, repeat the process.
  • To adjust the turtles' speed, use the randint() function.

Implementing the code step by step :

The turtle module will be used for this animation. This module is ideal for rapidly and easily producing graphics. Don't worry if you've never used Python's turtle module; we will walk you through each step.

We will presume your machine is set up with Python. You might need to change some of the variables if you're using a web-based platform so that your animation will fit in the display window.

Firstly, we will import the turtle module and the module random integers into our program.

Next, we will set the speed of the turtle for drawing using the speed() method and move the turtle to new coordinates using the penup() method and goto() method.

Next, we will start to draw the race track. Now, we will use a for loop to start to draw the track's roads using the write() method, executing the loop 15 times.

Next, a for loop for the number of equal spaces between the road's markings. In this, we used the penup() method to relocate with a lifted pen, used the forward() method to advance the turtle and then we used pendown() method to drop the pen.

Then, we again lifted the pen using penup() method and then used the backward() method to advance in the reverse direction. We also used the left() method to turn the turtle left.

Now, we will start filling in the details of each player. To start with, we will start with the first player. We will set an instance of the turtle module and use it for player 1 related movements. Then we used the color() method to set the colour of the turtle. Then we will choose to set the shape of the turtle using shape() method.

Then, we will use the penup() method to lift up the pen and move to a certain coordinates using the goto() method and then drop the pen using pendown() method.

Then, we will use a for loop and turn around by 360 degrees on the same spot.

To start with, we will start with the second player. We will set an instance of the turtle module and use it for player 2 related movements. Then we used the color() method to set the colour of the turtle. Then we will choose to set the shape of the turtle using shape() method.

Then, we will use the penup() method to lift up the pen and move to a certain coordinates using the goto() method and then drop the pen using pendown() method.

Then, we will use a for loop and turn around by 360 degrees on the same spot.

To start with, we will start with the third player. We will set an instance of the turtle module and use it for player 3 related movements. Then we used the color() method to set the colour of the turtle. Then we will choose to set the shape of the turtle using shape() method.

Then, we will use the penup() method to lift up the pen and move to a certain coordinates using the goto() method and then drop the pen using pendown() method.

Then, we will use a for loop and turn around by 360 degrees on the same spot.

To start with, we will start with the fourth player. We will set an instance of the turtle module and use it for player 4 related movements. Then we used the color() method to set the colour of the turtle. Then we will choose to set the shape of the turtle using shape() method.

Then, we will use the penup() method to lift up the pen and move to a certain coordinates using the goto() method and then drop the pen using pendown() method.

Then, we will use a for loop and turn around by 360 degrees on the same spot.

Then, finally we will set the turtle to run at random speeds using the randint() method along with forward() method and then execute it.

Complete Code:

Output:

The screenshot of the output of the aforementioned code is attached below which describes the animation of a racing track using turtle in Python.

Creating a simple animation using Turtle in Python
Creating a simple animation using Turtle in Python
Creating a simple animation using Turtle in Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA