Prerequisites: Turtle Programming in Python
Turtle is a Python feature like a drawing board, which lets us command a turtle to draw all over it! We can use functions like turtle.forward(…) and turtle.right(…) which can move the turtle around. Turtle is a beginner-friendly way to learn Python by running some basic commands and viewing the turtle do it graphically. It is like a drawing board that allows you to draw over it. The turtle module can be used in both object-oriented and procedure-oriented ways.
To draw, Python turtle provides many functions and methods i.e. forward, backward, etc. Some commonly used methods are:
- forward(x): moves the pen in the forward direction by x unit.
- backward(x): moves the pen in the backward direction by x unit.
- right(x): rotate the pen in the clockwise direction by an angle x.
- left(x): rotate the pen in the anticlockwise direction by an angle x.
- penup(): stop drawing of the turtle pen.
- pendown(): start drawing of the turtle pen.
In this article, we are going to write a code for drawing a Flower with the help of Turtle programming. As shown in the figure below.
Approach:
- Import turtle module
- Set speed of the turtle
- Using loops to avoid unnecessary repetition of code.
- Draw each step with specific coordinates
Below is the implementation:
Example1:- Flower
Python3
import turtle
tur = turtle.Turtle()
tur.speed( 20 )
tur.color( "black" , "orange" )
tur.begin_fill()
for i in range ( 50 ):
tur.forward( 300 )
tur.left( 170 )
tur.end_fill()
turtle.done()
|
Output:
Example 2:
Python3
import turtle
turtle.penup ()
turtle.left ( 90 )
turtle.fd ( 200 )
turtle.pendown ()
turtle.right ( 90 )
turtle.fillcolor ( "red" )
turtle.begin_fill ()
turtle.circle ( 10 , 180 )
turtle.circle ( 25 , 110 )
turtle.left ( 50 )
turtle.circle ( 60 , 45 )
turtle.circle ( 20 , 170 )
turtle.right ( 24 )
turtle.fd ( 30 )
turtle.left ( 10 )
turtle.circle ( 30 , 110 )
turtle.fd ( 20 )
turtle.left ( 40 )
turtle.circle ( 90 , 70 )
turtle.circle ( 30 , 150 )
turtle.right ( 30 )
turtle.fd ( 15 )
turtle.circle ( 80 , 90 )
turtle.left ( 15 )
turtle.fd ( 45 )
turtle.right ( 165 )
turtle.fd ( 20 )
turtle.left ( 155 )
turtle.circle ( 150 , 80 )
turtle.left ( 50 )
turtle.circle ( 150 , 90 )
turtle.end_fill ()
turtle.left ( 150 )
turtle.circle ( - 90 , 70 )
turtle.left ( 20 )
turtle.circle ( 75 , 105 )
turtle.setheading ( 60 )
turtle.circle ( 80 , 98 )
turtle.circle ( - 90 , 40 )
turtle.left ( 180 )
turtle.circle ( 90 , 40 )
turtle.circle ( - 80 , 98 )
turtle.setheading ( - 83 )
turtle.fd ( 30 )
turtle.left ( 90 )
turtle.fd ( 25 )
turtle.left ( 45 )
turtle.fillcolor ( "green" )
turtle.begin_fill ()
turtle.circle ( - 80 , 90 )
turtle.right ( 90 )
turtle.circle ( - 80 , 90 )
turtle.end_fill ()
turtle.right ( 135 )
turtle.fd ( 60 )
turtle.left ( 180 )
turtle.fd ( 85 )
turtle.left ( 90 )
turtle.fd ( 80 )
turtle.right ( 90 )
turtle.right ( 45 )
turtle.fillcolor ( "green" )
turtle.begin_fill ()
turtle.circle ( 80 , 90 )
turtle.left ( 90 )
turtle.circle ( 80 , 90 )
turtle.end_fill ()
turtle.left ( 135 )
turtle.fd ( 60 )
turtle.left ( 180 )
turtle.fd ( 60 )
turtle.right ( 90 )
turtle.circle ( 200 , 60 )
turtle.done()
|
Output: