How to Draw a Pretty Pattern with Turtle
Tags: python, tutorial
Published: 2020-09-02 18:34:13

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](https://www.reddit.com/r/learnprogramming/comments/i9vuhr/i_wrote_a_syllabus_for_learning_python_and_django/) posted by /u/TravisJungroth on reddit. I’m currently going through [Runestone Interactive](https://runestone.academy) and the `thinkcspy` course. 

>*NOTE: You should be familiar with Python basics such as variables, for loop, and functions. Also, the [Turtle library](https://docs.python.org/3/library/turtle.html).*

This is the pattern we will create:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9l7l95lx6xvjtie7napx.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/1dsgw2qohmghng2af3xp.png)

If we look further, we notice a square:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k38dbt4juk9pvqge76ob.png)

Further examination, we see a grid of four squares:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xipbkf8b3buzekuqmb43.png)

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

```python
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:

```python
import turtle
```
This imports the Turtle library.
  
  
```python
tess = turtle.Turtle()
```
This creates `tess` our turtle object. 
  
  
```python
wn = turtle.Screen()
```
`wn` creates the screen object where are pattern will be drawn.
  
  
```python
tess.speed(10)
```
This adjusts the speed of the pen draw. `1` is slow and `10` is fast.


```python
tess.pensize(2)
```
This adjust the size of the line drawn.


```python
tess.color('blue')
```
This sets the pen color to blue.


```python
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.

```python
def drawSquare(t, sz):
    for i in range(4):
        t.fd(sz)
        t.left(90)
```
Let’s go through each line:

```python
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.

```python
for i in range(4):
```
We create a `for` loop that iterates four times: 0,1,2,3.

```python
    t.fd(sz)
```
This moves the turtle object forward `sz` units.

```python
   t.left(90)
```
This rotates the turtle `90` degrees.

Let’s try our function by calling it:

```python
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](https://dev-to-uploads.s3.amazonaws.com/i/bqb13h1rzzkxergxyf2s.png)

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.

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

```python
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.

```python
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.

```python
    drawSquare(t, sz)
```
This calls the `drawSquare` function with two parameters. `t` is the turtle object and `sz` is the size of the square.

```python
t.left(90)
```
This will turn the turtle left `90` degrees.

Now that our `drawGrid` function is complete, let’s test it out. 

```python
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](https://dev-to-uploads.s3.amazonaws.com/i/w12mhwavv70w8stlpyn0.png)

>*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](https://dev-to-uploads.s3.amazonaws.com/i/pnhql1m4gt83nsndvueb.png)

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:

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

main()
```

Line by line:

```python
def main():
```
We declare a `main` function to hold our code. This isn’t required but it keeps the code organized.

```python
 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.

```python
drawGrid(tess, 100)
```
We call `drawGrid` with two arguments. `tess` as the turtle object and `100` as the square side length.

```python
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:

```python
# 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](https://docs.python.org/3/library/turtle.html)
[Runestone Interactive](https://runestone.academy)

*Cover Photo by Iva Muškić from Pexels*