Drawing an Easter Egg Using Python's turtle Module
A simple way to draw an Easter Egg using Python's `turtle`…with just a bit of maths
Today's tutorial is a short one. You'll draw a simple Easter egg. There's a bit of trigonometry in today's article, but if you're not comfortable with the mathematics don't worry, you can just trust me that the equations are right!
Here's the Python Easter Egg you'll create in this tutorial:
Note: If you're using a web-based coding platform instead of a local version of Python, make sure you're using one with a standard version of Python and
turtle
. The "Python With Turtle" option on Replit.com is a good option. Some other web-based platforms are limited and you won't be able to run this tutorial properly on those platforms.
The Shape of an Easter Egg (or any Egg)
We can use equations to represent curves. Simple shapes have relatively simple equations. I looked up the equation for the shape of an egg, and I was surprised to find plenty of complex equations for drawing an egg.
But I want something simpler, so I'll cheat in this tutorial.
The simplest way to draw an egg is to draw an ellipse. But we can do a bit better without making things too complex. We can draw different "halves" of ellipses at the top and bottom of the image. The top ellipse should be more elongated than the bottom one. That should give us a reasonably-shaped egg.
I'll let you look up the definition of an ellipse and the equation you need to create it. I'll only give a short description in this section. An ellipse has two axes, the major and the minor ones. It's longer along one axis than the other. If the two axes are equal, then you have a circle.
If you look at the angle that a line from the centre to a point on the ellipse makes with the x-axis, let's call it angle
, then the x and y coordinates of the ellipse are:
x = a * cos(angle)
y = b * sin(angle)
The values a
and b
are the lengths the axes that define the ellipse. They're actually half the length of the axes. The value b
represents the vertical axis and a
is the horizontal.
To draw an Easter Egg, the top half will have half an ellipse that's more elongated than the bottom half. Therefore, the value for b
should be larger for the top half of the ellipse.
There are courses for teenagers and preteens who are keen to learn proper coding at Codetoday Unlimited
Drawing the Easter Egg using Python's turtle
You can start by importing the turtle
module and choosing two colours, one for the chocolate egg and one for the background:
You can use a list for the three numbers that represent the colours. However, you can also use a tuple, as in this case. You don't need parentheses or any type of bracket to create a tuple, but you can use parentheses (round brackets) if you wish.
You also add turtle.done()
at the bottom. You don't need this yet, but you will need it very soon.
Next, create the screen and the turtle:
Since you're using red, green, and blue values in the range between 0 and 255, you need to change the colour mode used by the turtle
module. By default, turtle
uses colour values between 0 and 1.
You will draw two halves of ellipses. Start with the bottom half:
You import the math
module since you need to use sine and cosine. When using angles in sines and cosines in equations like the ones above, instead of using degrees for angles, you should use radians. If you're not familiar with radians, don't worry. They're another unit for measuring angles, and the math
module has a way of converting from degrees to radians, so you can use that function.
Look at the for
loop. You're looping for angles between -180 degrees and 0 degrees. This is the bottom half of the window and represents the part of the grid where y has negative values. You set the turtle's x- and y- positions using the equations I showed you earlier. Here's the animation so far:
Don't worry about the line from the centre. You'll fill in the egg later.
Now, you can carry on by drawing the top part. This is another half of an ellipse, but you'll change the value of b
to make the top half longer. The value of a
shouldn't change as the two ellipses should have the same width:
Here's this Python Easter Egg so far:
All that's left is to fill the egg and change the background colour. You need to use egg.begin_fill()
before you start drawing the Easter Egg and egg.end_fill()
when you finish. You also add egg.hideturtle()
to hide the arrow, which represents the turtle on the screen:
And this is the final version of this Python Easter Egg:
You can customise the drawing to make it more unique!
Code in this article uses Python 3.12
There are courses for teenagers and preteens who are keen to learn proper coding at Codetoday Unlimited
Appendix: Code Blocks
Code Block #1
import turtle
chocolate = 95, 59, 31
background = 255, 223, 229
turtle.done()
Code Block #2
import turtle
chocolate = 95, 59, 31
background = 255, 223, 229
window = turtle.Screen()
window.colormode(255)
egg = turtle.Turtle()
egg.pensize(5)
egg.color(chocolate)
turtle.done()
Code Block #3
import turtle
import math
chocolate = 95, 59, 31
background = 255, 223, 229
window = turtle.Screen()
window.colormode(255)
egg = turtle.Turtle()
egg.pensize(5)
egg.color(chocolate)
a = 150
b = 170
for angle in range(-180, 0):
egg.setx(a * math.cos(math.radians(angle)))
egg.sety(b * math.sin(math.radians(angle)))
turtle.done()
Code Block #4
import turtle
import math
chocolate = 95, 59, 31
background = 255, 223, 229
window = turtle.Screen()
window.colormode(255)
egg = turtle.Turtle()
egg.pensize(5)
egg.color(chocolate)
a = 150
b = 170
for angle in range(-180, 0):
egg.setx(a * math.cos(math.radians(angle)))
egg.sety(b * math.sin(math.radians(angle)))
b = 250
for angle in range(0, 181):
egg.setx(a * math.cos(math.radians(angle)))
egg.sety(b * math.sin(math.radians(angle)))
turtle.done()
Code Block #5
import turtle
import math
chocolate = 95, 59, 31
background = 255, 223, 229
window = turtle.Screen()
window.colormode(255)
egg = turtle.Turtle()
egg.pensize(5)
egg.color(chocolate)
egg.begin_fill()
a = 150
b = 170
for angle in range(-180, 0):
egg.setx(a * math.cos(math.radians(angle)))
egg.sety(b * math.sin(math.radians(angle)))
b = 250
for angle in range(0, 181):
egg.setx(a * math.cos(math.radians(angle)))
egg.sety(b * math.sin(math.radians(angle)))
egg.end_fill()
egg.hideturtle()
window.bgcolor(background)
turtle.done()