Turtle – Learning to code with drawings

Introduction

I am currently stuck indoors suffering with Covid, this is especially unfortunate as I am overseas on holiday, with few better options I decided to play around with the Python Turtle library.

What is Turtle?

When I was at school (a long time ago) there was a language we used on the computers called Logo (Logo Language).

This was one of the first ways to try and teach children how to code. The idea was very simple but could lead to a surprising amount of creativity. You had a canvas on the screen and a “turtle” which could draw lines. you would write instructions telling the turtle to move forwards, rotate and such and it would plot the path you took onto the screen. Turtle is a python tribute to the original Logo.

Initialisation

Firstly we need to import the Turtle library and create an instance of the screen and turtle class.

from this point we can instruct the instance of the Turtle object (t) to start drawing paths for us

Drawing Basic Shapes

Lets start by writing some code to draw basic shapes – squares, circles and hexagons.

Square

we simple loop 4 times drawing the path and then rotating 90 degrees for the next path. The output of this method is shown below.

Circle

There is an inbuilt method for circles You simply pass the circle function a radius.The output of this method is shown below.

Hexagon

Similar to a square – we have 6 even sides with a 60 degree angle change. The output of this method is shown below.

Spirographs

OK, so we have drawn some basic shapes, that’s not exactly interesting. but with a little bit of code we can make these shapes transform into something cool. firstly lets add a function to choose a random colour for the lines we draw to give the images some pop.

now to make a Spirograph we need to produce multiple versions of this shape slightly angled apart from each other in a circle. The code below will allow us to do this.

We will draw 8 shapes each one ten degrees apart from each other, we will repeat this process 6 times. giving us a total of 48 shapes. Each individual shape drawn will be a randomly assigned colour from the collection in the randColor function. We can pass a string representing the shape we wish to use to the drawSpirograph function.

Below is the output for a circle Spirograph.

and this is a hexagon one.

suddenly our boring shapes have become something quite pleasing!

Art From Chaos?

We can of course create images without using our preset shapes. Consider the following code.

This code is looping and is making the length of the line we will draw increase each time by 3 units. The right angle the line is drawn out increases by one degree each line. Initially this makes a fairly mundane shape, pretty much a badly drawn circle.

but as the loop continues the angles of each line drawn increases, this results in a fairly impressive drawing.

Fractal Trees

Fractals are Never ending patterns that are similar across different scales. We see fractals throughout nature in trees, rivers, coastlines, mountains, clouds, seashells and snowflakes. One of the basic fractal patterns is the Tree structure. Lets try to create one in Turtle.

What is a Fractal Tree

we will start by drawing a single Y shape. Then from each branch of this Y shape (the left branch and the Y branch, we will draw another Y shape), this could continue to infinity, but we will limit the number of levels we repeat decreasing the size of the Y shape and its angle as we loop.

In this function Recursion is used. The function repeats itself – for the left branch and the right branch it draws, each recursive step it reduces the size (to 80% of the previous and reduces the level count by 1).

If I call this function with 2 levels. I would see the basic Y shape.

with 4 levels I would see the recursion having an impact on the shape

This is a pattern you might start to recognise from the “chaos” of nature. Not that impressive yet, but this is a pattern that can continue to infinity, so lets increase the number of levels. below is an image of 5 levels.

And finally 15 levels. notice how this grows exponentially (and yes this took quite a while for the code to complete!).

Conclusion

I think the Logo language we used at primary school was my first experience with programming, and while the Turtle library might be for old people like me who want a bit of nostalgia, I actually would like to think it could be used to inspire kids as possibly I was inspired back in the day. If you happen to be a parent reading this blog why not introduce it to your children, who knows perhaps it might inspire them to become a programmer, or if nothing else might keep them quiet for few hours!

As with nearly all these posts, I’ve shown a tiny amount of potential possibilities, this is literally a canvas for drawing and you can do whatever you like, If you’ve enjoyed this post – simply install and have a go for youself, its very simple and a lot of fun.