DEV Community

Cover image for How to Draw a Pretty Pattern with Turtle
Miguel T Rivera
Miguel T Rivera

Posted on

How to Draw a Pretty Pattern with Turtle

Forgot to post last week but here we go. I decided on PythonšŸ to add to my skillset. Primarily back-end (Flask, Django) but may branch out in the future. Iā€™m following the guide posted by /u/TravisJungroth on reddit. Iā€™m currently going through Runestone Interactive and the thinkcspy course.

NOTE: You should be familiar with Python basics such as variables, for loop, and functions. Also, the Turtle library.

This is the pattern we will create:

Alt Text

At first this pattern looks very complex, but if we break it down into small chunks, its manageable. Looking for shapes, we see can see, a pie slice:

Alt Text

If we look further, we notice a square:

Alt Text

Further examination, we see a grid of four squares:

Alt Text

Letā€™s start small and work are way to a solution. First, letā€™s import the turtle library and setup our canvas.

import turtle

tess = turtle.Turtle()
wn = turtle.Screen()

tess.speed(10)
tess.pensize(2)
tess.color('blue')
wn.bgcolor('lightgreen')

Letā€™s examine each line:

import turtle

This imports the Turtle library.

tess = turtle.Turtle()

This creates tess our turtle object.

wn = turtle.Screen()

wn creates the screen object where are pattern will be drawn.

tess.speed(10)

This adjusts the speed of the pen draw. 1 is slow and 10 is fast.

tess.pensize(2)

This adjust the size of the line drawn.

tess.color('blue')

This sets the pen color to blue.

wn.bgcolor('lightgreen')

This sets the canvas background color to lightgreen.

Now all the setup code is done, letā€™s tackle the problem.
Letā€™s start with creating a drawSquare function.

def drawSquare(t, sz):
    for i in range(4):
        t.fd(sz)
        t.left(90)

Letā€™s go through each line:

def drawSquare(t, sz):

This declares the drawSquare function that takes two parameters. t represents a turtle object and sz the size of a square side length.

for i in range(4):

We create a for loop that iterates four times: 0,1,2,3.

    t.fd(sz)

This moves the turtle object forward sz units.

   t.left(90)

This rotates the turtle 90 degrees.

Letā€™s try our function by calling it:

drawSquare(tess, 100) 

We call drawSquare with two arguments: tess as the turtle object and 100 as the size. We should get the following result:
Alt Text

Now that we know our drawSquare works, we can start making the grid. Something to keep in mind. Back in elementary school, I used a compass to draw unique shapes or circles. Furthermore, I centered the compass to draw the shapes. This knowledge will help us later on. Letā€™s build the drawGrid function.

def drawGrid(t, sz):
    for i in range(4):
        drawSquare(t, sz)
        t.left(90)
def drawGrid(t, sz):

We declare the drawGrid function which takes two parameters. t is a turtle object and sz is the size of the square side length in the grid.

for i in range(4):

Again, we create a for loop that iterates four times: 0,1,2,3. This accounts for the four squares in the grid.

    drawSquare(t, sz)

This calls the drawSquare function with two parameters. t is the turtle object and sz is the size of the square.

t.left(90)

This will turn the turtle left 90 degrees.

Now that our drawGrid function is complete, letā€™s test it out.

drawGrid(tess, 100)

This calls drawGrid with two arguments. tess is the turtle object and 100 is the square length size.

We should get the following result:
Alt Text

NOTE: Arguments and parameter have different meaning. Arguments is data passed to function and parameters are what the function accepts.

Notice where the turtle is currently. It's at position (0, 0). This is the origin for drawing the squares and the grid. This is needed so the grid does not get distorted when rotating them, as we will see later on.

We are almost done. Now we need to create the pie slices in the circle. Earlier, if you looked at the pretty pattern we're making, you might of noticed something:

Alt Text

Itā€™s a stack of grids that rotate slightly. Another observation, there are five pie slices in each square of the grid. Letā€™s wrap this up:

def main():
    for i in range(5):
        drawGrid(tess, 100)
        tess.left(18)

main()

Line by line:

def main():

We declare a main function to hold our code. This isnā€™t required but it keeps the code organized.

 for i in range(5):

Remember the five pie slices? Yes, we iterate that many times, which creates five grids. This for loop iterates five times: 0,1,2,3,4.

drawGrid(tess, 100)

We call drawGrid with two arguments. tess as the turtle object and 100 as the square side length.

tess.left(18)

After a grid is drawn we turn 18 degrees left. You might wonder, where does the number 18 comes from? Remember, we turn 90 degrees after drawing each square. Since we are drawing five grids we divide that to get the turn angle after each grid is drawn. 90 // 5 = 18.

Final code:

# pretty-picture.py
import turtle

tess = turtle.Turtle()
wn = turtle.Screen()

tess.speed(10)
tess.pensize(2)
tess.color('blue')
wn.bgcolor('lightgreen')

def drawSquare(t, sz):
    for i in range(4):
        t.fd(sz)
        t.left(90)

def drawGrid(t, sz):
    for i in range(4):
        drawSquare(t, sz)
        t.left(90)

def main():
    for i in range(5):
        drawGrid(tess, 100)
        tess.left(18)

main()

We could refine this further and move the turtle setup code to the main function. I hope this tutorial helped you in problem solving. Although the problem looked difficult at first, changing it to small bits reduced its complexity. For more information, checkout the resources below:

Additional Resources
Turtle Library Documentation
Runestone Interactive

Cover Photo by Iva MuÅ”kić from Pexels

Top comments (2)

Collapse
 
scroung720 profile image
scroung720

AS someone who started programming using logo language programming. I really like your post. Thanks for sharing.

Collapse
 
mtrivera profile image
Miguel T Rivera

Thanks!