DEV Community

Cover image for How to draw smiley using Python Graphics
vayola pradeep
vayola pradeep

Posted on

How to draw smiley using Python Graphics

How to draw smiley using Python Graphics

Using the Turtle graphics library in Python we can draw a smiley face. It creates a yellow circle for the face, two smaller circles for the eyes, and a curved line for the smile. The smiley face will be displayed in a Turtle graphics window, and the program will finish when you close the window.

This code uses a Turtle graphics object to draw a smiley face step by step, including the face, eyes, and smile. The penup() and pendown() commands are used to control when the turtle's pen is lifted from the canvas to move without drawing and when it's lowered to start drawing again. The goto() command is used to move the turtle to specific positions. Finally, the turtle.done() command keeps the Turtle graphics window open until you close it.

Image description

Below code, step by step, uses a Turtle to draw a simple smiley face with a yellow circular face, two eyes, and a curved smile.

Step 1 :import turtle: This line imports the Turtle graphics library in Python, allowing you to create graphics and drawings.

import turtle
Enter fullscreen mode Exit fullscreen mode

Step 2 : t = turtle.Turtle(): It creates a Turtle object named 't', which is used to control the turtle's movements and drawings.

import turtle
t = turtle.Turtle()
Enter fullscreen mode Exit fullscreen mode

Step 3 : t.fillcolor('yellow'): Sets the fill color for shapes drawn by the turtle. In this case, the fill color is set to yellow, which will be used for the face and eyes.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')

Enter fullscreen mode Exit fullscreen mode

Image description

Step 4 : t.begin_fill(): This command marks the beginning of a shape that you want to fill with the specified color. It prepares the turtle to draw a filled shape.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()



Enter fullscreen mode Exit fullscreen mode

Step 5 : t.circle(100): Draws a circle with a radius of 100 units. This is used to create the yellow face of the smiley.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 6 : t.end_fill(): Ends the filling of the shape, which is the yellow circle in this case.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
Enter fullscreen mode Exit fullscreen mode

Image description

Step 7 : t.penup(): Raises the turtle's pen, so it won't draw when it moves.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
Enter fullscreen mode Exit fullscreen mode

Step 8 : t.goto(-40, 120): Moves the turtle to the coordinates (-40, 120) without drawing any lines.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 9 : t.pendown(): Lowers the turtle's pen, so it can start drawing again.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
Enter fullscreen mode Exit fullscreen mode

Step 10 :t.begin_fill(): Begins the filling of another shape.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
Enter fullscreen mode Exit fullscreen mode

Step 11 :t.circle(10): Draws a circle with a radius of 10 units, creating the left eye.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 12 : t.end_fill(): Ends the filling for the left eye, which is also filled with the yellow color.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
Enter fullscreen mode Exit fullscreen mode

Step 13 : t.penup(): Lifts the pen again.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
Enter fullscreen mode Exit fullscreen mode

Step 14 : t.goto(40, 120): Moves the turtle to the coordinates (40, 120) without drawing.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 15 : t.pendown(): Lowers the pen to start drawing.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
Enter fullscreen mode Exit fullscreen mode

Step 16 : t.begin_fill(): Begins filling for the right eye.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
Enter fullscreen mode Exit fullscreen mode

Step 17 : t.circle(10): Draws a circle with a radius of 10 units, creating the right eye.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 18 :t.end_fill(): Ends the filling for the right eye.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
Enter fullscreen mode Exit fullscreen mode

Step 19 :t.penup(): Lifts the pen.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
Enter fullscreen mode Exit fullscreen mode

Step 20 :t.goto(-30, 85): Moves the turtle to the coordinates (-30, 85) without drawing.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 21 : t.pendown(): Lowers the pen to start drawing again.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
Enter fullscreen mode Exit fullscreen mode

Step 22 :t.right(90): Rotates the turtle 90 degrees to position it correctly for drawing the smile.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
t.right(90)
Enter fullscreen mode Exit fullscreen mode

Image description
Step 23 : t.circle(30, 190): Draws a curved line to create the smile using a part of a circle with a radius of 30 and an angle of 190 degrees.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
t.right(90)
t.circle(30, 190)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 24 : turtle.done(): Keeps the Turtle graphics window open until you click on it to close the program.

import turtle
t = turtle.Turtle()
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(40, 120)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.goto(-30, 85)
t.pendown()
t.right(90)
t.circle(30, 190)
turtle.done()
Enter fullscreen mode Exit fullscreen mode

It creates a yellow circle for the face, two smaller circles for the eyes, and a curved line for the smile. The smiley face will be displayed in a Turtle graphics window, and the program will finish when you close the window.

Here's a breakdown of what each part of the code does:

  • t = turtle.Turtle(): Creates a Turtle object named t.
  • t.fillcolor('yellow'): Sets the fill color to yellow for filling the shapes.
  • t.begin_fill(): Begins filling the shape with the specified color.
  • t.circle(100): Draws a circle with a radius of 100, creating the face of the smiley face.
  • The following penup(), goto(), and pendown() commands are used to move the turtle to the desired positions for drawing the eyes and smile.
  • t.right(90)rotates the turtle to the right by 90 degrees, so it can draw the curved smile.
  • t.circle(30, 190)draws a part of a circle, creating the smile.
  • The turtle.done() command is used to keep the Turtle graphics window open until you close it. When you close the window, the program finishes.

Your code should successfully draw a smiley face when executed.

Click here to understand the program and the result

Top comments (0)