Draw Color Filled Shapes in Turtle – Python
Prerequisite: Python Turtle Basics
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.
To fill the colors in the shapes drawn by turtle, turtle provides three functions –
fillcolor(): This helps to choose the color for filling the shape. It takes the input parameter as the color name or hex value of the color and fills the upcoming closed geographical objects with the chosen color. Color names are basic color names i.e. red, blue, green, orange.
The hex value of color is a string(starting with ‘#’) of hexadecimal numbers i.e. #RRGGBB. R, G, and B are the hexadecimal numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F).
begin_fill(): This function tells turtle that all upcoming closed graphical objects needed to be filled by the chosen color.
end_fill(): this function tells turtle to stop the filling upcoming closed graphical objects.
Drawing Color Filled Square:
import turtle
t = turtle.Turtle()
s = int ( input ( "Enter the length of the side of the square: " ))
col = input ( "Enter the color name or hex value of color(# RRGGBB): " )
t.fillcolor(col)
t.begin_fill()
for _ in range ( 4 ):
t.forward(s)
t.right( 90 )
t.end_fill()
|
Input :
200
green
Output :
Drawing Color Filled Triangle:
import turtle
t = turtle.Turtle()
s = int ( input ( "Enter the length of the side of the triangle: " ))
col = input ( "Enter the color name or hex value of color(# RRGGBB): " )
t.fillcolor(col)
t.begin_fill()
for _ in range ( 3 ):
t.forward(s)
t.right( - 120 )
t.end_fill()
|
Input :
200
red
Output :
Drawing Color Filled Hexagon:
import turtle
t = turtle.Turtle()
s = int ( input ( "Enter the length of the side of the hexagon: " ))
col = input ( "Enter the color name or hex value of color(# RRGGBB): " )
t.fillcolor(col)
t.begin_fill()
for _ in range ( 6 ):
t.forward(s)
t.right( - 60 )
t.end_fill()
|
Input :
100
#113300
Output :
Drawing Color Filled Star:
import turtle
t = turtle.Turtle()
s = int ( input ( "Enter the length of the side of the star: " ))
col = input ( "Enter the color name or hex value of color(# RRGGBB): " )
t.fillcolor(col)
t.begin_fill()
for _ in range ( 5 ):
t.forward(s)
t.right( 144 )
t.end_fill()
|
Input :
200
#551122
Output :
Drawing Color Filled Circle:
import turtle
t = turtle.Turtle()
r = int ( input ( "Enter the radius of the circle: " ))
col = input ( "Enter the color name or hex value of color(# RRGGBB): " )
t.fillcolor(col)
t.begin_fill()
t.circle(r)
t.end_fill()
|
Input :
100
blue
Output :
Last Updated :
10 Feb, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...