Turtle Graphics Basic

Introduction to Turtle Graphics

Python includes a module called turtle. The turtle module enables us to create simple and complex shapes. Turtle Graphics refers to drawing shapes using the Turtle module.

On this page, you’ll learn:

  • What are Python modules
  • The Python Turtle module
  • How to import the turtle module to your program
  • Python turtle module’s functions
  • Graphics programming to draw complex shapes using the turtle module

When you download Python, it brings with it many modules. One of these modules is called turtle. The turtle module is a pre-installed Python library.  The Python turtle library comes with interactive features that allow programmers to work interactively with Python and learn graphics programming.

Using the turtle module to draw shapes is called Turtle Graphics. Turtle graphics is a fun way to introduce programming to kids.  Since it is easy to understand, the kids can grasp the concepts of programming very quickly. Because of its simplicity, turtle programming is a great way for  kids to get introduced to programming. 

The program examples in the next few sections use turtle module to draw simple graphics such as polygons, circles, etc. The later sections do turtle grahics programming to draw complex shapes such as flowers, spirals, moon crescent, etc.

Turtle Graphics shows a turtle
Turtle Image

What Are Python Modules

Python modules are a set of ready-to-use code that you can use in your programs. Python modules are a block of code that can be used anywhere in your main program wherever you need the functionality of that module just by typing the module’s name. Modules are a combination of functions, variables, and classes.

Note: We will discuss classes and objects on a later page Object-Oriented Programming.

Python has a lot of modules for various tasks. We will introduce just one module in this chapter. This module’s name is turtle. We will use the turtle module to do Turtle Graphics.  We will postpone the rest of the Python modules to a later page.

Reference documents on Turtle Graphics in Python can be found at this link.

Modules need to be imported before they can be used in your program. In contrast, Python’s built-in functions do not need to be imported before they can be used in your program.  We will discuss Python functions in detail in a Python Functions 1 page.

Importing Modules

To use a Python module in your program, we need to first import the module using the import statement.

import statement has the following syntax:

import module_name1[, module_name2[,… module_nameN]

where ‘module_name1’, ‘module_name2’,…..’module_nameN’are the names of the Python modules.

For turtle graphics programming, we will import the turtle module using the following statement.

>>>import turtle

Coordinate Geometry

Before we start to draw shapes, let us briefly review the basics of Coordinate Geometry and the Cartesian coordinate system.  Readers who are familiar with this subject should skip this section.

A coordinate plane is like graph paper. The figure below shows one horizontal line in the middle of the graph paper.  This horizontal line is called the x-axis. There is one vertical line in the middle of the graph paper, which is called the y-axis. The horizontal direction is the X-direction and the vertical direction is the Y-direction.  The x-axis and y-axis are perpendicular (right angle) to each other.

The Origin

The point where the x-axis crosses the y-axis is called the origin. At the origin, the x-value is “0” and the y-value is “0”.  Therefore, the origin is denoted by (0,0), where the first “0” is the x-value and the second “0” is the y-value.

What is the meaning of (0,0)?  To understand this, we will introduce what are called coordinates. (0,0) is called the coordinates of origin. At origin, both x and y equal zero.

On the x-axis, there is a scale that goes from zero at the origin to a higher positive number in the right direction and it goes from zero at the origin to a higher negative number in the left direction.

Similarly, there is a scale on the y-axis that goes from zero to higher positive number in the up direction and from zero to higher negative number in the down direction. X-axis and Y-axis divide the Coordinate Plane into four areas. These areas are called quadrants. See the figure below.

Turtle Graphics Coordinate Plane for turtle to move around.
The Coordinate Plane – Turtle Graphics

The figure below shows a coordinate plane with the x-axis and y-axis and the horizontal lines and vertical lines spaced at a distance of 20. The figure also shows points on the coordinate plane with coordinates of (60,100), (-100,+100), (-60,-40), and (+20,-40) in the four quadrants.

Turtle Graphics window for turtle to move around showing X-axis, Y-axis, Origin, and the Coordinates Plane with Four Quadrants
Coordinate Plane, X-axis, Y-axis, Quadrants, and the Origin

Create a Blank Window for Turtle to Move on And Draw

To start graphics programming, we first need to open a window for the turtle to move around. Let us call it a turtle’s playground. As indicated above, we need to first import the turtle module using the below command.

>>> import turtle # This is line 1) in the program below.

Set the Window For The Turtle To Move Around And Window Size

After we import the turtle module, the next step is to create a window for the turtle to move around.

# Code line 2:

>>>screen=turtle.Screen()

Code line 2) sets up the window and it is called the screen.

#Code line 3):

>>>screen.setup(320,320)

In the above command, we set up the window screen size. The program sets up the window size as x = 320 pixels in the x-direction. This command sets the window size as y = 320 pixels in the Y-direction.

Give Turtle Window Screen A Name

It is always a good idea to give our window screen a title. On code line 4) of the programs, the statement

>>>screen.title (“Startup Screen”)

creates a title for the window screen. We named it “Start-up Screen”. This name appears in the left-hand top corner of the turtle window.

Let us Name The Turtle and Choose The Turtle Shape

In code line 5):

>>>TTL=turtle.Turtle()

we define a variable “TTL”.

“TTL” is now the name of our turtle.

Python turtle module supports many shapes for the turtle such as “arrow, “turtle”, and many more. 

Code line 6):

>>>TTL.shape(“turtle”)

the program selects the shape called “turtle”.

PROGRAM EXAMPLE: SETTING UP THE TURTLE SCREEN
import turtle                          #1)
screen=turtle.Screen()  #2) set up window for turtle to play.
screen.setup(320,320)   #3) set-up window size.
screen.title("Startup Screen") #4) Optional: name your screen.
TTL=turtle.Turtle()     #5) Name your turtle variable "TTL".
TTL.shape("turtle")     #6) Choose the shape of the turtle.

When the program below is run, it produces a blank window on your desktop with the turtle in the center of the window. This turtle position is the ‘home’ location of the turtle.  The turtle home location’s coordinates are (0, 0).

Notice that the turtle faces to the right on the start-up screen. This is the EAST direction, which corresponds to the direction of 0 degrees.

Below is the output of the program (Turtle start-up screen).

This Turtle Graphics start up screen appears on desktop when we set up the turtle screen.
Turtle Start-up Window

Turtle’s Face Orientation (Degrees vs. Directions)

Here are some common directions and corresponding degrees:

To_angle (degrees)Direction
0East
90North
180West
270South
ANGLE DEGREES AND TURTLE DIRECTION
Turtle Graphics Turtle turn directions (East, West, North, South) for turtle turn commandsdirection change commands.
Angles for Turtle Turn Commands

A summary of the various Python Functions for Graphics Programming is below.

Functions for Turtle Graphics Programming

The following are some of the basic commands available to move the turtle.

Note: In the functions, a parameter is entered inside the parenthesis. For example, in the tables below ‘distance’ and ‘angle’ are the parameters.

‘distance’ is the distance you want to move the turtle.

‘angle’ is the angle you want the turtle to turn by.

Movement and Draw Functions

You can use the following Movement and Draw functions to move the turtle around the screen.

FUNCTIONDESCRIPTION
turtle.forward(distance) turtle.fd(distance)Moves the turtle forward by
number of pixels specified by
the distance parameter within
the parenthesis. Parameter
distance is integer or float.
turtle.back(distance) turtle.bk(distance)Moves the turtle back by the
amount specified by the
distance parameter within
the parenthesis.
turtle.right(angle) turtle.rt(angle)Turns the turtle right by the
angle parameter specified in
the command. Parameter
angle is integer or float.
turtle.left(angle) turtle.lt(angle)Turns the turtle left by the
angle parameter specified in
the command. Parameter
angle is integer or float.
turtle.setpos(xy) turtle.setposition(xy)Moves the turtle to coordinates
(x,y).
turtle.setx(x)Moves the turtle x-coordinate as
as specified in parameter. 
x is an integer or float.
turtle.sety(y)Moves the turtle y-coordinates as
specified in parameter. 
y is an integer or float.
turtle.home()Moves the turtle to coordinates
(0,0)
turtle.dot()Draw a solid dot of radius specified in parenthesis.
TURTLE MOVEMENT COMMANDS

Turtle Position Functions

The following functions are useful to position the turtle at specified coordinates on the screen.

FUNCTIONDESCRIPTION
turtle.xcor()Return the turtle’s x coordinate.
turtle.ycor()Return the turtle’s y coordinate.
turtle.pos()Return the turtle’s current location (x,y).
Turtle.goto(x,y)Move turtle to an absolute position (x,y). If the pen is down, draw the line as the turtle moves to location (x,y). 
TURTLE POSITIONING COMMANDS

Turtle Pen Control Functions

Python turtle module has a few Pen Control commands to make the pen up or down, as well as to select the pen width size (narrow or wide).

FUNCTIONDESCRIPTION
turtle.pendown()
turtle.pd()
Sets the turtle pen down.  Turtle
draws as it moves across the
screen.
turtle.penup()
turtle.pu()
Sets the turtle pen up. There are
instances when you would like to
move the turtle without drawing
a trace on the screen.  This
function will lift the turtle pen up.
turtle.pensize(width)Selects pen width as specified in
the parameter width within the
parenthesis. width is an integer
between 1 to 10.
TURTLE PEN CONTROL COMMANDS

Turtle Color Control Functions

The turtle color control functions let the programmer select the shape’s outline color.

R, G, and B color components are explained in the Enrichment: Colors in Computers section at the end of this chapter.

FUNCTIONDESCRIPTION
FunctionDescription
turtle.pencolor()Selects pen color as specified in
the argument inside the parenthesis.
color is specified by a string such
as ‘red’, ‘blue’, ‘green’, etc.
Example: >>>turtle.pencolor(“red”).
Without arguments, this function
returns the current color of the pen.
turtle.colormode()Set the argument to 255
turtle.pencolor()Or color can be specified by R, G, B. 
Before this command is sent, set
the turtle.color.mode(255)
R,G,B are integers from 0 to 255
Example: turtle.pencolor((x, y, z))
(x, y, z) are between 0 and 255.
turtle.color()Returns the pen color and fill color,
if no arguments are specified.
Input pen color and fill color
arguments.
Example: turtle.color(“black”, “red”).
In this example, the shape outline is
black and shape is filled with red.
TURTLE COLOR CONTROL FUNCTIONS

Color Fill Control Functions

The following Color Fill functions fill the shape with a specified color.

FUNCTIONDESCRIPTION
turtle.filling()Returns fill state (true if filling; false otherwise)
turtle.begin_fill()This function is called just before the shape needs to be filled.
turtle.end_fill()After the turtle.begin_fill() has been called, call this function to end the fill.
turtle.fillcolor(color string or R, G, B)Either of the following parameters can be used:
Color string specifying a color (e.g. “red”)
or
(R,G,B)
Example:
Turtle.fillcolor(“red”) Turtle.fillcolor((150, 34, 92))
TURTLE COLOR FILL FUNCTIONS

Screen Control Functions

If you wish to change the background color of the screen, turtle.bgcolor() function can be used. Python also gives you the capability to clear the screen using turtle.clear() function

FUNCTIONDESCRIPTION
FunctionDescription
turtle.bgcolor()The arguments within the parenthesis specify the color of screen background. The color is written in double quotes e.g. (“red”). If the colormode() is specified as 255, then the argument can be three numbers from 0 to 255.
Example: screen.bgcolor(“orange”) screen.color(128,0,255)
If no argument is specified, this statement returns the current color of the screen.
turtle.clear()Clear the screen and leave the turtle position and other settings as is.
TURTLE SCREEN FUNCTIONS

Circle() Function

The turtle circle function is used to draw a circle of any radius.

FUNCTIONDESCRIPTION
FunctionDescription
turtle.circle()The parenthesis contains the
arguments that specifies
1) the radius of the circle
2) Arc, if complete circle is not needed.
Specify a number from 0 to 360 degrees.
3) Number of steps to complete the
circle.
Smaller number of steps will result in
a polygon type of shape. 
Use large number to make a smooth
circle. If this parameter
is not specified, Python will calculate
it automatically.
TURTLE CIRCLE() FUNCTIONS

Miscellaneous Functions

Finally, there are many miscellaneous functions, which are described in the table below.

FUNCTIONSDESCRIPTION
turtle.reset()Clear the screen and return the turtle
to coordinates (0.0). Turtle settings are lost.
turtle.clear()Clear the screen and leave the
turtle position and other settings as is.
turtle.shape()The argument within the parenthesis
is the name of the shape of the turtle.
Valid shape names are:
“arrow”
“circle”
“turtle”
“triangle”
“classic”
“square”
Example of command: Turtle.shape(“square”).
TURTLE MISCELLANEOUS FUNCTIONS

Turtle Graphics Basic Programming

With the turtle functions described in the section above, you can do Graphics programming to draw simple and complex shapes.

In the program examples below, we do simple graphics programming using the Python turtle module’s functions.

Make the Turtle Move and Draw

The following two commands make the turtle move and draw a trace on the screen if its pen is down. (See turtle.pendown() and turtle.penup functions described above.) By default, the pen is down.

The command

turtle.forward(100)

will move the turtle forward 100 pixels in the direction the turtle is facing.  Or you can use the shorthand notation turtle.fd(100)

turtle.backward(100) will move the turtle backward 100 pixels in the direction opposite to where the turtle is facing. Or you can use the shorthand notation turtle.bk(100).

The program below moves the turtle forward 100 pixels and draws a line.

PROGRAM EXAMPLE: TURTLE MOVE FORWARD
import turtle
screen=turtle.Screen()  # set up window for turtle to play
screen.setup(320,320)   # Set-up screen size.
screen.title("forward") # Put a name for the screen window.
TTL=turtle.Turtle()     # ‘TTL’ is the name of our turtle.
TTL.shape("arrow")      # Select turtle shape as an arrow.
TTL.forward(100)

The image below shows the output of the program.

Turtle Graphics make the turtle move and draw a trace.
Make The Turtle Move Forward And Draw A Trace

Move the Turtle Without Drawing a Trace

There are situations where you would like the turtle to move to a different location without drawing a line.

Turtle has a pen underneath its belly. Turtle.penup() moves the pen up from the screen. In this case, the turtle will not draw on the screen as it moves to a new location.

Turtle.pendown() command moves the pen down.  In this case, the turtle will draw on the screen as the turtle moves.

In summary, if the turtle pen is down pendown(), the turtle will draw a trace as it moves to the specified coordinates. 

If the pen is up penup(), the turtle will go to the specified coordinates without drawing a line.

Positioning the Turtle to a Specified Location

If you want the turtle to go to certain (x,y) coordinates, you can use the function turtle.goto(x,y).

You can also position the turtle at a different location by using turtle.setposition(x,y) function.  The turtle will move to coordinates (x,y).

Move The Turtle To The HOME Position

What if you want the turtle to move from any location (x,y) to location (0,0)?  You will use the function turtle.home() and the turtle will move to its home location, coordinates (0,0).

Change the Direction of the Turtle

You may need to change the direction the turtle is pointing to while doing graphics programming,

The command

Turtle.right(angle)

turns the direction of the turtle to the right as specified by the argument angle indicated within the parenthesis.  The shorthand for this command is turtle.rt(angle).

The command

Turtle.left(angle)

turns the direction of the turtle to the left as specified by the argument angle indicated within the parenthesis. The shorthand for this command is turtle.lt(angle).

The figure below shows the turtle facing UP (North). The arrows in the figure show how the turtle will turn when turtle.right  or turtle.left  function is called.

Turtle Graphics turtle.left() and turtle.right() commands.
turtle.left() and turtle.right() command

Turtle Graphics Programming to Draw Simple Shapes

The program examples below use the turtle module’s Move and Draw functions, and turn functions to draw a few simple shapes.

Draw a Square

Here is the program to draw a square.  The program uses turtle.forward() and turtle.right() pair four times to draw the square.

PROGRAM EXAMPLE: MAKE TURTLE DRAW A SQUARE
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import turtle
>>> turtle.pen = turtle.pen()
>>> turtle.forward(200)
>>> turtle.right(90)
>>> turtle.forward(200)
>>> turtle.right(90)
>>> turtle.forward(200)
>>> turtle.right(90)
>>> turtle.forward(200)
>>>

Below is the screen that the turtle drew.

Turtle Graphics Draw a Square
Turtle Graphics to Draw a Square

Color Control and Pen Control

Color makes the shapes look much better. Below is an example of how you would use pen control and color control functions to draw colored shapes.

The program below draws a hexagon with red outlines and a wider pen.

PROGRAM EXAMPLE: DRAW A HEXAGON WITH THICK RED OUTLINES
import turtle
turtle.pen = turtle.pen()
turtle.pensize(10) 	# Pen width set to 10.
turtle.colormode(255)
turtle.pencolor("red")
turtle.penup()  	# Make pen up so it does not write.
turtle.setx(-250) 	# Set turtle starting point at (-250,+250).
turtle.sety(250)
turtle.pendown()       # Set pen down to enable writing.
turtle.forward(75)
turtle.right(60)       # Hexagon external angle is 60 degrees.
turtle.forward(75)
turtle.right(60)
turtle.forward(75)
turtle.right(60)
turtle.forward(75)
turtle.right(60)
turtle.forward(75)
turtle.right(60)
turtle.forward(75)

Here is what the turtle drew.

Turtle Graphics Pen Control and Color Control.
TURTLE GRAPHICS PEN CONTROL AND COLOR CONTROL

Draw a Dot

The program below draws a red dot of 25 pixels radius. It uses the function turtle.dot(). The program also uses the function turtle.hideturtle() to illustrate how to hide the turtle shape from appearing in the drawn image.

There is another function, turtle.showturtle() that makes the turtle visible. Copy this program and test it with both hideturtle and showturtle functions.

PROGRAM EXAMPLE: DRAW A DOT
import turtle
screen=turtle.Screen()  # set up window for turtle to play
screen.setup(160,160)   # set-up window size
screen.title("DRAW A RED DOT") # Optional: name your screen
TTL=turtle.Turtle()     # Name your turtle variable "TTL".
TTL.shape("classic")    # Choose the shape of the turtle as "classic".
TTL.hideturtle()        # Hide the turtle
TTL.dot(25, "red")      # Dot radius = 25

The following is the program output.

Turtle Graphics Draw a Red Dot
Turtle Graphics Draw a Rd Dot

Draw a Circle

Let us now learn how to draw a circle of a certain diameter. The following program draws a circle of radius = 50 pixels using the turtle.circle() function.

PROGRAM EXAMPLE: DRAW A CIRCLE
import turtle
screen=turtle.Screen()  # set up window for turtle to play
screen.setup(320,320)   # set-up window size
screen.title("Startup Screen") # Optional: name your screen
TTL=turtle.Turtle()     # Name your turtle variable "TTL".
TTL.shape("turtle")     # Choose the shape of the turtle.
TTL.circle(50)          # Radius = 50

The following is the program output.

Turtle Graphics Draw a Circle
Turtle Graphics Draw A Circle

Turtle Graphics Programming to Draw Complex Shapes

Let us now create complex shapes using the Turtle functions.

The program draws three circles. Each circle has

  • different outline color
  • different outline width
  • different fill color.

In this program, the following turtle module functions are used.

Pen width: You can change the width of the pen in the turtle’s belly by using the function pensize(width).  In the program below, we used pen sizes of width = 8, 3, and 10 for different outline widths.

Pen color: The program also shows how to use the function pencolor() to change the color of the pen by specifying the color strings like ”red”, “magenta” etc. within the parenthesis.

Fill color:  You can fill the shapes with different colors by specifying an argument (a string) within the parenthesis of the fillcolor() fubction. A few examples are fillcolor(‘lightgreen’), fillcolor(‘yellow’), etc. To fill a shape with color, you use the function begin_fill() and the function end_fill()

The procedure to fill a shape with a color is described below.

  1. Start with the begin_fill() function
  2. Issue the commands to draw the shape
  3. Call the end_fill() function, as in the program example below.

Turtle Shape: Python turtle module gives the programmer the flexibility to use one of many turtle shapes. Turtle shape selection is done by using the function turtle.shape().

The turtle module provides several turtle shapes. A few of the shapes are: ‘arrow’, ‘circle’, ‘classic’, ‘triangle’, ‘turtle’, and ‘square’. You can select the turtle icon shape by using the function turtle.shape().

PROGRAM EXAMPLE: DRAW CIRCLES WITH COLOR FILL AND OUTLINE WIDTHS

Program name: turtle_3circles_color_fill

# Program to make turtle draw a happy face.
# the program uses most of the functions of the turtle module.
import turtle
myTurtle = turtle.Turtle()
myTurtle.shape('classic')
myTurtle.penup()

# Light green Circle with red outline and outline width = 8. 
# Fill circle with light green color.
myTurtle.pencolor('red') # Specify pen color.
myTurtle.pensize(8) # Specify pen width = 8.
myTurtle.fillcolor('lightgreen') # Specify fill color.
myTurtle.goto(-30,0)
myTurtle.begin_fill() # Call begin_fill() function just before the shape needs to be filled.
myTurtle.pendown()
myTurtle.circle(30)  # Circle radius = 30
myTurtle.end_fill() # end_fill() function ends the fill that turtle.begin_fill() started.

# Circle with blue outline and outline width = 3.
# Fill circle with yellow color.
myTurtle.penup()
myTurtle.pencolor('blue')
myTurtle.pensize(3)
myTurtle.fillcolor('yellow')
myTurtle.goto(30,0)
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(30)
myTurtle.end_fill()

# Circle with magenta outline and outline width = 10.
# Fill circle with red color.
myTurtle.penup()
myTurtle.pencolor('magenta')
myTurtle.pensize(10)
myTurtle.fillcolor('red')
myTurtle.goto(0,-60)
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(30)
myTurtle.end_fill()

#
myTurtle.penup()
myTurtle.hideturtle()

The following is program’s output.

Turtle Graphics 3 Circles Pen Width Color Fill
Turtle Graphics Draw 3 Circles With Different Pen Width And Color Fill

Draw Moon Crescent With Circle() Function

In this program, we use the turtle.circle() function to draw a moon crescent.

From geometry, we know that a complete circle arc is 360 degrees. To draw an arc of a circle, we use the turtle.circle(radius, extent) function. The parameter ‘extent’ specifies the angle of the arc we wish to draw. If ‘extent’ is not specified, a complete circle is drawn. If the parameter ‘extent’ is specified, a part of the circle is drawn as specified by the parameter ‘extent’.

The program first draws a circle and fills it with gold color.  Then, the program moves the turtle slightly by 25 pixels. The commands then overlay a white circle on top of the gold circle. Since the white circle is shifted by 25 pixels, most of the gold circle is erased except the crescent.

PROGRAM EXAMPLE: TURTLE Circle() FUNCTION TO DRAW MOON CRESCENT

Program name: turtle_crescent.py

# Make Turtle draw the moon's crescent.
import turtle
screen=turtle.Screen()	# set up window for turtle to play.
screen.setup(320,320)	# Set up window size.
myTTL = turtle.Turtle()
screen.title("MOON CRESCENT")
myTTL.penup()
myTTL.setpos(60, 40)	# Set initial position of turtle.
myTTL.pendown()
myTTL.pencolor("white") # Draw a circle with white outline.
myTTL.setheading(110)    # Set heading for crescent moon.
myTTL.begin_fill()
myTTL.fillcolor("gold") 	# Fill circle with gold color.
myTTL.circle(50)   	# Moon radius = 50
myTTL.end_fill()
# Draw another circle with white fill displaced 25 pixels to the right.
# This circle writes over the part of the previous circle.
# to erase the gold color leaving the crescent.
myTTL.right(90)
myTTL.fd(25)
myTTL.setheading(110)
# begin_fill() function to start to fill the white circle displaced from gold circle.
myTTL.begin_fill()	
myTTL.fillcolor("white") 
myTTL.circle(50)	# end_fill of white circle.
myTTL.end_fill()

The following image is what the turtle drew. Copy this code on your Python shell window and run it to see if it works for you.

Turtle Graphics Draw Moon Crescent With Color Fill
Turtle Graphics Draw Moon Crescent With Color Fill Using Circle Function

Draw Happy Face using circle() Function

The following program uses the turtle module’s turtle.circle() function to draw a happy face.  The program uses all the commands used in the previous programs.

PROGRAM EXAMPLE: DRAW HAPPY FACE USING TURTLE CIRCLE() FUNCTION

Program name: turtle_happyface.py

Credits: Adapted from:

<a href=”https://hourofpython.trinket.io/a-visual-introduction-to-python#/turtles/going-in-circles“>

# Program to make turtle draw a happy face.
# the program uses most of the functions of the turtle module.
import turtle
screen = turtle.Screen()	# Setup window for turtle to move around.
screen.setup(400, 400)		# Set up window size.

myTurtle = turtle.Turtle()
myTurtle.shape(‘classic’)	# Select shape of turtle.
# Face
myTurtle.penup()
myTurtle.pencolor('red')
myTurtle.fillcolor('lightgreen')
myTurtle.goto(30,-150)
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(130)		# radius = 130
myTurtle.end_fill()

# Left Eye
myTurtle.penup()
myTurtle.pencolor('black')
myTurtle.fillcolor('white')
myTurtle.goto(0,0)
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(20)
myTurtle.end_fill()
# Left Eye Pupil
myTurtle.fillcolor('red')
myTurtle.begin_fill()
myTurtle.circle(10)
myTurtle.end_fill()

# Right Eye
myTurtle.penup()
myTurtle.forward(60)
myTurtle.right(45)
myTurtle.fillcolor('white')
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(20)
myTurtle.end_fill()

# Righr Eye Pupil
myTurtle.fillcolor('red')
myTurtle.begin_fill()
myTurtle.circle(10)
myTurtle.end_fill()

# Mouth
myTurtle.penup()
myTurtle.right(90)
myTurtle.forward(90)
myTurtle.fillcolor('lightgrey')
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(40)
myTurtle.end_fill()
myTurtle.penup()
#myTurtle.goto(25,-25)

# Nose
myTurtle.fillcolor('magenta')
myTurtle.goto(15,-15)
myTurtle.begin_fill()
myTurtle.pendown()
myTurtle.circle(15)
myTurtle.end_fill()
myTurtle.penup()

# Happy face
myTurtle.goto(0,-85)
myTurtle.left(45)
myTurtle.pencolor('red')
myTurtle.pensize(5)
myTurtle.pendown()
myTurtle.circle(25, 180, 50)
myTurtle.penup()
myTurtle.hideturtle()

The following is the program output.

Turtle Graphics Draw a Happy Face Using circle() Function
TURTLE GRAPHICS DRAW A HAPPY FACE USING THE circle() FUNCTION

Draw a Leaf Petal Using Circle() Function

In the above two programs, we used the turtle.circle() function to draw an arc.  Now we will draw two arcs facing each other to draw a leaf petal.

The program below draws an arc with parameter extent = 60 degrees. To draw a petal, we need to draw the second arc that goes back in the opposite direction to meet the start of the first arc.  When we drew the first arc (extent = 60), the turtle heading changed by 60 degrees.  To draw the second arc back towards the start of the first arc, we need to change the heading of the second arc to (180-60) = 120 degrees.

PROGRAM EXAMPLE: DRAW A LEAF PETAL USING CIRCLE() FUNCTION

Program name: turtle_petal.py

# Draw a petal
import turtle
screen=turtle.Screen()	# set up window for turtle to play.
screen.setup(320,320)	# set up window size.
screen.title(‘PETAL’)	# Window title.  It appears on top of the window.
myTTL=turtle.Turtle()

myTTL.up()		# pen up so as not to draw when turtle moves to (-80,-50).
myTTL.goto(-80,-50)
myTTL.down()
myTTL.fillcolor('lightgreen')
myTTL.begin_fill()
# Draw first arc of the petal. Radius = 200; angle of arc = 60 degrees.
myTTL.circle(200,60)
# Draw the second arc of the petal in the opposite direction.
myTTL.left(120) 	# Arc angle is (180 - 60 = 120) degrees.
myTTL.circle(200,60)
myTTL.end_fill()

The following is what the turtle drew.

Turtle Graphics Use Circle Function To Draw a Leaf Petal
Turtle Graphics Use Circle Function To Draw a Leaf Petal

Enrichment: Colors in Computers

The RGB Color Components

In electrical engineering, engineers design chips to send the data to the computer screen. The data is sent to each individual pixel on the monitor screen. The monitor screen typically has 1920 pixels in the horizontal (x-direction) and 1280 pixels in the vertical (y-direction). Each pixel on the screen is composed of three components. One component is Red, the second is Green, and the third is Blue color. This system is called RGB.  

R can have a value from 0 to 255

G can have a value from 0 to 255.

B can have a value from 0 to 255.

Depending on the values of R, G, and B you specify in your Python program, the color of the pixel will change.

For example, RGB = (0,0,0) will make the screen black.

RGB = (255, 255, 255) will make the screen white.

Other values between 0 and 255 will provide different colors.

Enrichment – René Descartes

René Descartes (1596 – 1650) was a French philosopher, mathematician, and scientist. He was a native of France.

Descartes was well-versed in mathematics as well as philosophy. Descartes contributed greatly to science as well. The Cartesian coordinate system was named after him. He is credited as the father of analytical geometry, the bridge between algebra and geometry, used in the discovery of infinitesimal calculus.

Descartes’s Meditations on First Philosophy (1641) continues to be a standard text at most university philosophy departments.  His best-known philosophical works are Discourse on the Method (1637) and Principles of Philosophy (1644).

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights