Python, a versatile and powerful programming language, offers endless opportunities for creativity and fun. One such example is the ability to create graphics and drawings using the turtle
module. In this blog post, we’ll explore how to use Python’s turtle
module to draw a simple yet charming smiling face.
First, let’s start by importing the turtle
module and creating a new turtle object:
pythonimport turtle
# Create a new turtle object
face = turtle.Turtle()
# Set the speed of the turtle for faster drawing
face.speed(1)
Next, we’ll draw the circular outline of the face using the circle
method:
python# Draw the circular outline of the face
face.circle(100)
Now, let’s add two eyes to the face. We’ll use the penup
and pendown
methods to move the turtle to the desired positions without drawing lines, and then draw circles for the eyes:
python# Move to the position of the left eye
face.penup()
face.goto(-40, 50)
face.pendown()
face.fillcolor("white") # Set the fill color for the eyes
face.begin_fill() # Start filling the shape
face.circle(15) # Draw the left eye
face.end_fill() # End filling the shape
# Move to the position of the right eye
face.penup()
face.goto(40, 50)
face.pendown()
face.begin_fill() # Start filling the shape again
face.circle(15) # Draw the right eye
face.end_fill() # End filling the shape
Next, we’ll draw the smile. We’ll use the right
method to rotate the turtle, and then draw an arc using the circle
method with a partial circumference:
python# Rotate the turtle to face downwards
face.right(90)
face.penup()
face.goto(-60, -30) # Move to the starting point of the smile
face.pendown()
face.right(180) # Rotate the turtle 180 degrees to draw the smile upside down
face.circle(60, 180) # Draw an arc with a radius of 60 and 180 degrees of circumference
Finally, we’ll hide the turtle cursor and keep the window open until the user closes it:
python# Hide the turtle cursor
face.hideturtle()
# Keep the window open until the user closes it
turtle.done()
Here’s the complete code for drawing a smiling face:
pythonimport turtle
# Create a new turtle object
face = turtle.Turtle()
face.speed(1)
# Draw the circular outline of the face
face.circle(100)
# Draw the left eye
face.penup()
face.goto(-40, 50)
face.pendown()
face.fillcolor("white")
face.begin_fill()
face.circle(15)
face.end_fill()
# Draw the right eye
face.penup()
face.goto(40, 50)
face.pendown()
face.begin_fill()
face.circle(15)
face.end_fill()
# Draw the smile
face.right(90)
face.penup()
face.goto(-60, -30)
face.pendown()
face.right(180)
face.circle(60, 180)
# Hide the turtle cursor and keep the window open
face.hideturtle()
turtle.done()
By running this code, you’ll be able to see a simple yet charming smiling face drawn on your screen using Python’s turtle
module.