Draw moving object using Turtle in Python
Last Updated :
12 Sep, 2023
Turtle is an inbuilt module in python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle. To move turtle, there are some functions i.e forward(), backward(), etc.
1.)Move the Object (ball) :
Following steps are used:
- Import Turtle package.
- Set screen with dimensions and color.
- Form turtle object with color.
- Form the object (ball – made of colored circle).
- Call the function for making object again and again and clear the screen.
Below is the implementation :
Python3
import turtle
def moving_object(move):
move.fillcolor( 'orange' )
move.begin_fill()
move.circle( 20 )
move.end_fill()
if __name__ = = "__main__" :
screen = turtle.Screen()
screen.setup( 600 , 600 )
screen.bgcolor( 'green' )
screen.tracer( 0 )
move = turtle.Turtle()
move.color( 'orange' )
move.speed( 0 )
move.width( 2 )
move.hideturtle()
move.penup()
move.goto( - 250 , 0 )
move.pendown()
while True :
move.clear()
moving_object(move)
screen.update()
move.forward( 0.5 )
|
Output :
Example 2: Move the Object (box)
Following steps are used:
- Import Turtle package.
- Set screen with dimensions and color.
- Form turtle object with color.
- Form the object (box – made of colored square).
- Call the function for making object again and again and clear the screen.
Below is the implementation:-
Python3
import turtle
screen = turtle.Screen()
screen.setup( 500 , 500 )
screen.bgcolor( 'Green' )
screen.tracer( 0 )
t = turtle.Turtle()
t.speed( 0 )
t.width( 3 )
t.hideturtle()
def draw_square() :
t.fillcolor( "Orange" )
t.begin_fill()
for side in range ( 4 ) :
t.forward( 100 )
t.left( 90 )
t.end_fill()
t.penup()
t.goto( - 350 , 0 )
t.pendown()
while True :
t.clear()
draw_square()
screen.update()
t.forward( 0.02 )
|
Output:
Share your thoughts in the comments
Please Login to comment...