Here, we will be making "The Great Indian Flag" usingPython. Python's turtle graphics module is built into the Python software installation and is part of the standard library. This means that you don't need to install anything extra to use the turtle lib. Here, we will be using many turtle functions likebegin_fill(), end_fill()to fill color inside the Flag,penup(), pendown(), goto(), etcto reaching the target.
Turtle Graphics
In computer graphics, turtle graphics are vector graphics using a relative cursor upon a Cartesian plane.Python Turtle is a drawing board-like feature that lets us command the turtle and draw using it.
Features of turtle graphics:
- forward(x):moves the pen in forward direction by x units.
- backward(x): moves the pen in the backward direction by x units.
- 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.
- begin_fill():starts filling the color inside the shape.
- fillcolor("color_name"):sets the color to be filled.
- end_fill():stops filling the color.
Approach to Making an Indian Flag in Python Using Turtle
1. Import the turtle modules.
import turtle
2. Get a screen to draw on.
screen = turtle.Screen()
3. Define an instance forturtle(here "t").
4.For making anIndian Flaglet's divide the process into 4 steps:
- Therectangle with orange color.
- Then themiddle rectangle.
- Then the lastGreen Rectangle.
- ThenAshoka Chakrainside the middle rectangle.
5. Here dimensions of all three Rectangles are (800 units x 167 units), which makes up dimensions of the flag as (800 units x 501 units).
6. The turtle starts from coordinates (-400, 250).
7. Then from that position it makes theFirst rectangle of orangecolor.
8. Then from the ending point of the first rectangle,Turtle makestheSecond rectangle of nocolor.
9. Then theThird green color rectangle is made. Now forAshoka Chakra, we need to performaset of operations.
- A Big Blue circle and a white circle just smaller than blue.
- Set of small blue circles on the inner lining of a blue and white circle.
- And finally spokes inside the two blue and white circles starting from the Centre towards the outer direction.
10. Finally, The pride of one's Nation is ready.
Code Implementation
Pythonimportturtlefromturtleimport*#screen for outputscreen=turtle.Screen()# Defining a turtle Instancet=turtle.Turtle()speed(0)# initially penup()t.penup()t.goto(-400,250)t.pendown()# Orange Rectangle#white rectanglet.color("orange")t.begin_fill()t.forward(800)t.right(90)t.forward(167)t.right(90)t.forward(800)t.end_fill()t.left(90)t.forward(167)# Green Rectanglet.color("green")t.begin_fill()t.forward(167)t.left(90)t.forward(800)t.left(90)t.forward(167)t.end_fill()# Big Blue Circlet.penup()t.goto(70,0)t.pendown()t.color("navy")t.begin_fill()t.circle(70)t.end_fill()# Big White Circlet.penup()t.goto(60,0)t.pendown()t.color("white")t.begin_fill()t.circle(60)t.end_fill()# Mini Blue Circlest.penup()t.goto(-57,-8)t.pendown()t.color("navy")foriinrange(24):t.begin_fill()t.circle(3)t.end_fill()t.penup()t.forward(15)t.right(15)t.pendown()# Small Blue Circlet.penup()t.goto(20,0)t.pendown()t.begin_fill()t.circle(20)t.end_fill()# Spokest.penup()t.goto(0,0)t.pendown()t.pensize(2)foriinrange(24):t.forward(60)t.backward(60)t.left(15)#to hold the#output windowturtle.done()
Output