Javatpoint Logo
Javatpoint Logo

Making an Indian Flag using Turtle in Python

The Prestigious Indian Flag will be drawn using Python Turtle in this tutorial. There is a Turtle module for Python. It has a turtle(pen) and a cardboard sketching surface. Move the turtle to construct anything on the screen. The turtle can be moved using other operations, including forward() and backward(). We can use several drawing techniques on this just like we would on a drawing board. The basic commands control how the drawing pen actually moves.

Well, the turtle package for Python will be used to draw this flag. It offers built-in techniques that make sketching pictures a simple chore. We used loops to reduce the recurrence of the same lines of code, thus there aren't many lines of code overall.

For those who are just starting to learn Python and the Turtle Module, the Indian flag code is straightforward and simple to understand. For ease of understanding, we have included the remarks or walkthrough steps in each area.

Characteristics of turtle graphics :

forward(x) : Advances the pen by x units in the forward direction.

backward(x) : advances the pen by x units in the opposite direction.

Right(x) : Rotate the pen by an angle of x in a clockwise manner.

Left(x) : Turn the pen by an angle of x in a counter-clockwise direction.

penup() : It causes the turtle pen to cease drawing.

pendown() : starts drawing with the turtle pen with pendown() method.

Begin_fill() : begins filling the shape's interior with colour.

Fill_colour() : The fill colour is set using the fillcolor("color name") function.

End_fill() : It halts the color's filling.

Breaking down the code :

1. Importing Turtle library :

We can use the built-in methods and functions of the turtle module by importing it into our programme. To properly create the Picture, import * implies to import every method from the Turtle module.

2. Configuring the turtle and the drawing screen :

The screen for drawing the figure was set using screen() method and an instance of turtle module was also defined. The speed of drawing the figure was set using speed() method.

3. Creating the flag's orange rectangle :

We first kept the pen up without drawing using penup() method and moved to the coordinates -150 and 125 using the goto() method and began to draw using the pendown() method. We began with taking colour orange with the help of color() method and started advancing forward using forward() method, turning to right by a certain degree of angle using right() method, turning to left by a certain degree of angle using left() method and then repeating the process again until the rectangle's all sides are drawn. We ended drawing with the end_fill() method. Following the creation of the orange rectangle, the turtle simply advances using forward() method again, automatically enclosing the white portion of the flag.

4. Creating the flag's green rectangle :

We began with taking colour green with the help of color() method and started advancing forward using forward() method, turning to left by a certain degree of angle using left() method and then repeating the process again until the rectangle's all sides are drawn. We ended drawing with the end_fill() method. Following the creation of the green rectangle, the turtle simply advances using forward() method again, automatically moving to draw the further remaining portion of the flag.

5. Making a large blue circle :

To design the central blue large circle of the Flag in this section, we set the turtle position using goto() method to goto(35,0). Navy blue is the same hue as before, and the circle's radius remains at 35. And rest all methods including pendown(), color(), end_fill(), begin_fill() and circle() are used to draw the circle in a similar way as earlier.

6. Drawing the large White Circle inside the Blue Circle of the Indian Flag.

The turtle is now situated five steps inside the navy blue circle in this block. Draw the white circle by setting it to the coordinates goto(30,0) using the goto() method and keeping the radius at 30 and mentioning it using the circle() method. Rest all the functions like penup(), pendown(), color(), begin_fill() and end_fill() were all used in a similar way, as used in earlier parts. Ultimately, we got required white circle.

7. Drawing the 24 minicircles inside the middle circle :

The turtle's position in this code is set to goto(-27,-4) using the goto() method and its colour is navy. The turtle will draw 24 little Ashok Chakras using the for loop. The inner circle of the flag will have the required position for that circle to be drawn given that it has a radius of 2 units and a right angle of 15 degrees. Rest all the functions like penup(), pendown(), color(), begin_fill() and end_fill() were all used in a similar way, as used in earlier parts.

8. Creating the central blue circle :

The innermost navy blue circle of the Indian flag has been drawn after positioning the turtle at goto(10,0). Rest all the functions like penup(), pendown(), color(), begin_fill() and end_fill() were all used in a similar way, as used in earlier parts.

9. Adding the 24 spokes :

Here, we've used the for loop function to draw 24 spokes while keeping the pensize(1) and the turtle's position in the centre fixed at goto(0,0). Rest all the functions like penup(), pendown(), color(), begin_fill() and end_fill() were all used in a similar way, as used in earlier parts.

10. Drawing the Indian National Flag's stick :

The turtle position in this section has been changed to goto(-150,125), using the goto() method, the pen size has been increased to 10, and a straight line of 500 steps, which is the flag's stick, has been drawn. Along with that, we used hideturtle() method to hide the turtle after drawing completion and finishing the drawing using turtle.done().

Complete code :

Output:

After the execution of the aforementioned code, the outcome can be seen below as the " Indian National Flag ".

Making an Indian Flag 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