/* jquery */ /* jquery accordion style*/ /* jquery init */

14 May 2013

Learn Python - Turtle Patterns

Now let's draw some circles to create a pretty pattern, like this...

Does it look somewhat familiar? For those who remember, it's strikingly similar to the designs generated by the classic Denys Fisher Spirograph game.

To start use the Geany 'File->Save As' menu option save the square drawing program as 'turtle-circles.py'.

We only need to do a minor code change, namely replace the 'forward(200)' line with 'circle(100)'. The new listing will look like this:

from turtle import *

pencolor("blue")
pensize(5)

for i in range(4):
  circle(100)
  right(90)

exitonclick()

Save your changes and run the program. We now have four circles arranged in a pattern.

Let's make a few more changes. Modify the code to look like the listing below:

from turtle import *

pensize(5)

for i in range(4):
  for c in ["red","green","blue"]:
    pencolor(c)
    circle(100)
    right(30)

exitonclick()

Save your changes and run it again.

As you can see we now have two loops, one inside the other. This will draw a circle a total of twelve times (4 x 3). And by changing the pen colour in the inner loop, we've generated a multi-coloured pattern.

Are you starting to see the possibilities turtle graphics can offer? Next time we'll move onto creating solid-colour shapes.

A post from my Learn Python on the Raspberry Pi tutorial.