Javatpoint Logo
Javatpoint Logo

Drawing moving objects Using turtle in Python

Python comes with a module for Turtle. It includes a cardboard sketching screen and a turtle (pen). To create anything on the screen, move the turtle (pen). Other operations, such as forward() and backward(), can move the turtle.

On this, we can draw utilizing different drawing techniques just like we would on a drawing board. The fundamental commands manage the actual movement of the sketching pen. Starting with a very simple program, we progress to using Python Turtle to create a moving object.

Moving Objects or Animations :

Fundamentally, all animation (and all moving pictures) are just a series of photos that are displayed quickly enough to create the illusion of continuous motion. Every image is referred to as a frame. The word "animate" as in "bring to life" refers to the process of changing each frame significantly from the one before and swiftly displaying them one after another to create the appearance of continuous motion.

Even though there is a simpler method, we didn't start out that way because it's always preferable to learn the foundations as soon as you can. The simpler method is to use an image as the shape of the turtle rather than drawing the picture yourself.

Note : It's preferable to use a static.gif image rather than an animated one because turtle will only use the first frame and ignore the others.

Moving Object - A ball :

The Approach to be used :

Importing the turtle package from the Python library to use it in our project.

Then we created a method called mov_obj() for moving our object. This included filling the color of the ball using fillcolor() method, setting the radius of the circle of ball using circle() method and instructing to begin and end filling color using begin_fill() and end_fill() methods.

Then we crated a screen object where we will draw the object required using Screen() method. Then we setup the size of the screen using the setup() method, set the color of the screen using bgcolor() method and updated the screen using tracer() method.

Then we created the turtle object to create drawing pen. We also set color of the object - ball, using the color() method. We also set the speed of the turtle using the speed() method. We also set the width if the turtle using width() method and lastly hid it using the hideturtle() method.

Next, we lifted the turtle pen using the penup() method and moved to the coordinates (-350,0) using the goto() method, where we used the pendown() method to drop it down.

Finally, we used a while loop for moving the object created till now. We cleared the screen using the clear() method for the movement and called the move_obj() function to execute it and start moving the object created. Again, cleared the screen as done earlier and then set the forward motion movement of the ball/object using the forward() method.

Thus, completed the code and executed it.

Complete Code:

Output:

The screenshots of the output generated are attached below for reference.

Drawing moving objects Using turtle in Python
Drawing moving objects Using turtle in Python
Drawing moving objects Using turtle in Python

Moving Object - A ball (up to down) :

The code for moving the ball from up to down remains similar to moving a ball from left to right, its just the coordinates that are changed and a right turn by 90 degrees is added to move downwards. The following are additions made :

Code:

Output:

The screenshots of the output generated are attached below for reference.

Drawing moving objects Using turtle in Python
Drawing moving objects Using turtle in Python
Drawing moving objects Using turtle in Python

Moving Object - A ball (Diagonally) :

The code for moving the ball from diagonally, i.e. from left top corner to right bottom corner remains similar to moving a ball from left to right, its just the coordinates that are changed and a right turn by 45 degrees is added to move forward diagonally. The following are additions made :

Code:

Output

Drawing moving objects Using turtle in Python
Drawing moving objects Using turtle in Python
Drawing moving objects 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