Python Turtle Graphics – Pattern 3 | Drawing INDIAN FLAG

Python Turtle Graphics – Pattern 3 | Drawing INDIAN FLAG


Hello guys and welcome to Python Programming
tutorials by Amuls Academy. And i am back with another turtle tutorial,
today we are drawing the Indian National Flag using turtle module ok, so this pattern may
vary little from the original flag ok because here the first color is saffron so instead
of that in the program i will take Orange because Orange is the basic color and Saffron
can range from yellow to Deep Orange right so and also here Ashok Chakra I’ll take as
blue ok so it is actually navy blue and also i will give the color code for the saffron
color and the navy blue ok so we’ll see that also end of this tutorial and also this Ashok
Chakra the size of Ashok Chakra, in the original flag the size of Ashok Chakra is smaller and
it won’t touch the border of the rectangle. But in my program it will touch because i
took the radius like that so if you want you can change that also ok you can change the
radius and you can do that. So to draw this pattern first we need to import
turtle module. Ok So first in the Python file import the
turtle module, next create a turtle object from the Turtle class, so you can give any
name to this turtle object but here I’ll give “t” ok.
so Done, so next ok so here in this pattern this is the home position ok initially turtle
will present here, so now i need to draw this line first ok so for that what i will do is,
first I’ll move this turtle from here to home position to here, this position ok while moving
here i don’t draw this line ok so what I’ll do is, I’ll do t dot up, that is i will use
the up method because i don’t want to draw the line and I’ll move The turtle to here
using go to method OK. so the first thing is, here i will use “t.up”
and here I’ll use goto ok so now i need to mention the position where i want to move
The turtle ok here we can see the position 0 and minus 300, here i took the distance
randomly, if you want you can change ok we are printing this pattern for this distance
ok So here we can see X coordinate and Y coordinate
so this is minus y coordinate, so this distance is 0, – 300. so here i will write 0 and minus 300 alright
so now if i save this and run this program here we can see turtle will move here ok so
now to draw this line what I’ll do is, directly I’ll go to this position OK, i will move the
turtle here so now presently turtle is present here, i want to go to here ok and i want this
line also that’s why i will use t.down first, next i will use t.goto and i will mention
this position that is zero and 400 ok this is the positive Y coordinate. So here what i will do t.down because we want
to draw the line while moving the turtle, and next here goto and the distance is zero
and 400, if you want you can vary the distance ok.
ok now we got the first thing that is we got this line ok so now we are done with this
line now turtle is present here ok so now we need to draw three rectangles right?, and
we need to fill the colors first Orange, white and green ok here all the rectangles of same
size ok that is this width is 400 and and this height is 100, ok so what I’ll do is,
I’ll define a function ok to draw the rectangle after that i will call that function to draw
this three rectangles ok so here after importing the turtle, i will define the function ok
so you can give any name also so I’ll go for the rectangle and here I’ll take color as
the parameter because in each rectangle we can see only the difference is that is color,
here we can see this is orange, white and green size is same height width all are same
right?, so here i will take this color ok so i want to draw that, so i will use forward
of 400 distance, why i used forward means here we can see now
turtle is pointing towards this side ok this side, so i want this distance this is 400
i know that so i moved here now, now turtle is here after using “t dot forward” ok next
i want to draw this line so to draw this line first i need to set the heading ok, i need
to set the angle of the turtle, so for this what i will use is, i will use right method
OK, i will give right 90°. if you are confused with right and left method
you can use set heading also ok so in this set heading to adjust the heading of the turtle
this side you need to write 0 or 360 degree ok , for this 90 degree, this is 180 degree
and 270 degree now if i want to set this side ok so now i need to mention 270 degree, if
you are using set heading. Ok here i will use right, so t.right 90 degree
alright?, so next this distance is 100 now turtle is pointing here like this ok,so now
i need to move 100 distance forward ok, so here again i will use t.forward of distance
100,right?, again here we can see now turtle is here,ok it is pointing towards this side,
now i need to change the angle of the turtle because i want to draw this line,ok so for
this again i will use t.right 90 degree, now after using t.right now it is pointing towards
this side. Ok like this this side,so i will use “t dot
forward” 400 distance, again here it will point here, again i will, i will change the
angle of the turtle to 90 degree and i will draw this line, so we are repeating this line
ok. To draw next line we are repeating this, that’s
why what I’ll do is I’ll cut this and i will place this in a for loop, because i want to
execute this code twice ok so that’s why i will use loop, so if i show here ok after
drawing this after coming here ok after setting after setting this to right 90 degree, now
i want to draw this right?, what is this i need to use “t dot forward” 400. ok so to draw this line so after this, turtle
will be here and it will be pointing towards this side so to change the angle of this turtle
i need to use right and 90 degree ok so it will point Like this now, ok so again i need
to use “t dot forward” hundred ok so it will draw this line, again i will set that to 90
degree, so turtle will come to its initial position that is will point Like this here
ok so here we can see we are repeating this line that’s why i wrote this in the loop ok
so now here after this if i call rectangle now, function so here first color is orange
right?, ok so now if i save this and run this, OK Here we didn’t use the end fill and fill
color right?, so that’s why it’s not filling any color,
so here you need to use begin fill because we want to fill color right?, fill color is,
color, t.end_fill done, so now if i save this and run this,
Done ok so first rectangle is done, now i want to draw the second rectangle, ok now
turtle is here ok now if i call the rectangle function
it will draw again rectangle here itself, but i don’t want that. First i need to move this turtle to here,
ok then i need to draw this rectangle like this, ok so for this i will use goto method
ok, so here i need to use goto and i need to mention x and y coordinate, x is 0 i know
that, what is this position y, what is the value of y?, it is 300 right?, because if it is zero and
400 ok this distance is hundred here we can see the height of this rectangle is 100 that
means, this is 300 ok so I’ll mention 0 and 300. I will use “t dot goto”, here after this i
will mention 0 and 300 and I’ll call this function again and the color is white,
ok now turtle is here so to draw the next rectangle i will use goto, why goto?, because
now our turtle is here ok, i want to move it to here ok so what is this distance?, this
is 0 200 right that’s why i will use goto and 0, 200. So again i will call rectangle color is green. Ok so done, done with the 3 rectangle also,
so now the main point is this, Ashok Chakra so we want to draw Ashok Chakra with 24 spoke
wheel right?, so we need to draw this in this second rectangle, ok so what i will do is,
i don’t touch the first rectangle, i don’t touch the second rectangle but here instead
of calling this function ok to draw this rectangle with Ashok Chakra what I’ll do is, ok i won’t
call this function ok but i will write the code separately for this, what I’ll do is
first is “t dot forward” ok and i will use distance as 200 so that’s nothing but, so
at that time turtle will be here OK it is pointing towards this direction, so now what
I’ll do is, I’ll move 200 pixel distance that is nothing but this distance half of this
entire distance ok so we want this Ashok Chakra at the center of the rectangle right?, that’s why i will move 200 pixel distance
ok next i will draw this circle ok, so to draw the circle i will use circle method ok
after moving here I’ll draw, i will change the color of The Turtle ok to blue, the color
of Ashok Chakra is blue right?, so next what I’ll do i will use circle method and I’ll
create a circle of 50 radius. if you ask why 50?, so here we can see the
total distance of this is hundred ok so if i take radius as 50 then it will be the center
of the rectangle ok so it will draw this distance is hundred, so it will draw a complete circle
ok and here i will draw in the clockwise direction OK – 50 if i take anticlockwise direction
that’s nothing but if i take + 50 here, it won’t draw properly you can check that ok
and again i will change the color of turtle to black ok, so here that’s nothing but i
will draw a circle of 50 radius ok next i will change the color of the turtle to black
again and i will move forward 200 pixel ok why?, because here we can see initially , we
moved here ok turtle was here, it will draw the circle in the clockwise direction
ok, so we will get this circle,again turtle will be here, Now we want this right?, what
is this distance 200 right?,again. So i will move forward the turtle to 200 ok
, so now we are here ok, so turtle is pointing towards this side, so now again to draw this
line ok i need to write the code ok, here we are here ok, and turtle is pointing towards
this side, ok now i want to change that to this side ok so that’s nothing but, change
the turtle angle so I’ll take right as 90 degree ok so here i will write “t dot right
90 degree” that is the same code ok. Ok so next t.forward 100, so it will draw
this line now this line ok now turtle is pointing towards this side, so again i need to change
the angle so i will use t.right. so “t dot right 90 degree”, t.forward
400 . what is 400?, we are drawing this line ok,
i change the angle of this turtle so now it is pointing towards this side, so i will draw
this line of distance 400, now turtle is pointing towards this side, so i need to change the
angle so again i will use right 90 degree and i will move forward to 100 distance, so
t.right 90, t.forward 100, again i will use t.right 90 ok.
so here it was here, i changed the angle of this to 90 degree right 90°, so now it will
point towards here, i moved hundred distance now turtle is pointing towards this side i
need to change this angle to this side ok so that’s why i used right 90° so now it
is pointing towards this side Ok it became like this. Alright so now if i save this and run this
we can see, right?, so now we need wheel right?, so for that what i will use is, so here after
drawing the circle here i need to include few more condition ok so here we can see we
created the circle of 50 radius ok now turtle will be present here ok it will be like this,
i want to draw this line now 24 lines right?, from the center of the circle. so for that what i need to do is, i need to
move this turtle to center of the circle, ok so for this what I’ll do is, i will change
the angle of this turtle ok, that’s nothing but, now it is like this ok if it is the circle,
now it is present here and in this direction so i want to change that to this direction,
next i can forward this to here right?, like this. so for this you can use right left or setheading,
so while coming here you may get confused with the right and left ok instead of that
you can use set heading ok so, i told you right?, now it is present like this and i
want to change that to this side Ok so this is 270 i want it to i want to set the heading
to 270 degree, using set heading is easier in this case ok.
what 270 degree right?, next forward to 50 distance, now again setheading to 0, ok so
now if you ask what is this?, so I’ll tell you ok, this is the situation
i want to move this turtle to here, center of the circle ok, so for this what I’ll do
is, first I’ll change this angle of the turtle, so for that what i will use is, i will use
setheading 270 degree, ok so it will change this to this. ok now i will use forward and i will move
50 pixel distance, so it will come here ok, so now , so now the turtle will come here,
it is pointing towards this direction, ok so now again i will change this to original
position, ok like this, so for that i will use setheading 0, ok for this side 0 or 360
right?, so that’s why ok?, and here i will use for i in range, i want 24 lines right?,
24, here t.forward 45, t.bk 45, t.left to draw that lines ok, this code is to draw that
lines 15 degree, ok so now if i save this and run this. ok so now we are here right?, i don’t, i need
to again move to this position here right?, so i need to move turtle to here, so for that
i will use same code this, but here set heading is 90 degree, forward is 50 right?, and it
is zero. so here what i did is actually, so initially
ok after drawing the circle turtle was here ok so i moved that to center and i started
drawing this lines ok like this, 24 lines ok after drawing that ok, so The Turtle position
was this ok after drawing 24 lines from the center of the circle, The Turtle is pointing
towards this side ok so now what i need to do is here we are drawing the circle in the
center of the rectangle ok so here initially we draw half the rectangle so we need to complete
the rectangle so to draw this line i need to move this turtle to here ok like this so
for that what i did is, in my program first i set the heading to 90°, what i did here sorry, so sorry here this
is 180 degree right?, ok so this is 90 degree ok, so i set the heading to 90 degree, so
it will point like this ok so now i will move this distance radius 50,and now turtle is
pointing like this, ok, I need to change the heading so for that again
i will use set heading and i wrote the angle as 0, ok so it will point like this now, so
it will continue drawing the rectangle. Ok that’s why i wrote like this and rest everything
is same ok so we got the Indian flag. So now what i will do is i will do few changes
ok, nothing more i will just increase the pensize that’s it, i will give 4. Ok so done. So you can write this program in different
ways ok. you can take different distance also, if you
know any other way to write this program then don’t forget to comment below. And as i said in the beginning here in my
program i didn’t take the original color of the flag right?, because the first color is
saffron but here i took Orange ok so i will give you the color code of that now, so for
the saffron it is ff, so i will change this, instead of orange hash FF 99 it is 33, ok
so here for ashoka chakra it is navy blue so the color is 0000 and 88 ok so next here
instead of green here is the color code 12 88 0 7 ok. So now if i save this and run this, if you want you can change this radius also
ok, radius of the circle ok. so done ok so that’s it for now guys thank
you for watching don’t forget to subscribe to my channel i will meet you in next class
till then take care.

23 thoughts on “Python Turtle Graphics – Pattern 3 | Drawing INDIAN FLAG

  1. I’ve subscribed immediately. You’re very patient and your teachings and drawings are excellent. Keep it up. Best channel👍

  2. why does every time i run the code on jupyter notebook turtle does run but then says python turtle not responding also my orange box does have any color….help please!!!!!

  3. how to draw a Bangladeshi national flag used python..

    i am sharing my code and here has some problem …please help me…

    import turtle

    def rectangle (color):

    t.begin_fill()

    t.fillcolor(color)

    for i in range(2):

    t.forward(400)

    t.right(90)

    t.forward(200)

    t.right(90)

    t.end_fill()

    t = turtle.Turtle()

    t.up()

    t.pensize(4)

    t.goto(0,-300)

    t.down()

    t.goto(0,300)

    rectangle("green")

    t.goto(0,250)

    t.forward(200)

    t.color("red")

    t.circle(-50)

    t.color("black")

  4. madam i write same code with u but show that =>
    " Traceback (most recent call last):

    File "G:/Turtle Graphics/Flag.py", line 12, in <module>

    t.up()

    TypeError: penup() missing 1 required positional argument: 'self'

    Process finished with exit code 1"

    i dont understand whta problem there!!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *