Python and Turtle Difficulty Level 3,python,Tutorial Tutorial: Drawing Egg Shape with Python Turtle

Tutorial: Drawing Egg Shape with Python Turtle

In this tutorial we are going to show how to draw an egg shape with Python Turtle. This tutorial is also a solution to project Moss’s Egg.

Egg Shape with Python Turtle

As you can see from the figure above, there are four arcs we need to draw. The red arc is a half circle with the arc facing down. To draw this, we need to lift up the pen and go to the left end of the red arc, set heading to 270 degrees and draw a circle with extent 180 degrees. The following is the code snippet.

turtle.up()
turtle.goto(-100,-20)
turtle.down()
turtle.seth(270)
turtle.color('red')
turtle.circle(100,180)

After the step above, turtle should be located at the right end of the red arc. Continue from this location, we are going to draw a 45 degree arc with radius twice the size of the first half circle in red. The following code snippet draws the blue arc on the right.

turtle.color('blue')
turtle.circle(200,45)

Let’s draw the green arc on the top. It is a quarter circle (90 degree) with radius approximately 0.586 the size of the first circle. More precisely, the radius is (2-√2) times the size of the first circle. The following code snippet draws this green arc.

turtle.color('green')
turtle.circle(100*0.586,90)

The last blue arc is symmetrical to the first blue arc. The following is the complete code that draw this egg shape.

turtle.up()
turtle.goto(-100,-20)
turtle.down()
turtle.seth(270)
turtle.color('red')
turtle.circle(100,180)
turtle.color('blue')
turtle.circle(200,45)
turtle.color('green')
turtle.circle(100*0.586,90)
turtle.color('blue')
turtle.circle(200,45)

What if we want to generalize the egg drawing by allowing different sizes, positions, and even tilt angles. The solution is to create a draw_egg() function that takes these values in as parameters. The following is the complete code that defines draw_egg() function and draw three different eggs.

import turtle

screen = turtle.Screen()
screen.setup(1000,1000)
screen.title("Moss's Egg - PythonTurtle.Academy")
turtle.speed(0)
turtle.hideturtle()

def draw_egg(x,y,size,tilt):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.seth(270+tilt)
    turtle.color('red')
    turtle.circle(size,180)
    turtle.color('blue')
    turtle.circle(2*size,45)
    turtle.color('green')
    turtle.circle(0.586*size,90)
    turtle.color('blue')
    turtle.circle(2*size,45)

draw_egg(-100,-100,100,-90)
draw_egg(0,200,120,180)
draw_egg(200,-50,50,20)

The code should draw three eggs as shown in the following pictures.

Three Eggs with draw_egg() function

Challenge: Can you draw the following picture with a lot of easter eggs?

Lots of Eater Eggs with Python Turtle
Tags:

Related Post