Python: Using Turtles for Drawing Circles

I am currently working on a book review for a college course Python book that uses the Python turtle module and Tkinter to help teach the Python programming language. Hopefully I’ll have that book done by the end of the month. In the mean time, it made me decide to give the turtle module a try. In this article we’ll look at making the turtle draw the typical Olympics symbol and then try to clean up the code while adding new features.

Getting Started with Turtles

Fortunately, there are no packages to install. The turtle module is included with Python. All you have to do is import it. Here’s a really simple script that will draw a circle to the screen.

import turtle

myTurtle = turtle.Turtle()
myTurtle.circle(50)
turtle.getscreen()._root.mainloop()

As you can see, you need to create an instance of the turtle and then have it draw a circle. Sadly the default turtle actually looks like a mouse arrow. Fortunately, you can change that by simple passing a shape string: turtle.Turtle(shape=”turtle”). Let’s move on and create a really simple Olympics symbol!

Turtle Olympics

When you first start using the turtle module, it’s tempting to just set the position and draw circles willy-nilly. However, you’ll quickly end up with the turtle drawing lines between the circles and generally making a mess of things, so you’ll want to make sure to take care of that. Here’s our first attempt at making the symbol:

import turtle

myTurtle = turtle.Turtle(shape="turtle")
myTurtle.circle(50)

myTurtle.penup()
myTurtle.setposition(-120, 0)
myTurtle.pendown()
myTurtle.circle(50)

myTurtle.penup()
myTurtle.setposition(60,60)
myTurtle.pendown()
myTurtle.circle(50)

myTurtle.penup()
myTurtle.setposition(-60, 60)
myTurtle.pendown()
myTurtle.circle(50)

myTurtle.penup()
myTurtle.setposition(-180, 60)
myTurtle.pendown()
myTurtle.circle(50)

turtle.getscreen()._root.mainloop()

Yes, there is a LOT of redundant code in this script. Fortunately, it’s also very obvious what everything does. You have to remember that you’re basically drawing on an x/y grid with the very center of the screen being (0,0). Let’s try to make this code less messy. In this refactored example, we’ve turned our code into a class:

import turtle

class MyTurtle(turtle.Turtle):
    """"""

    def __init__(self):
        """Turtle Constructor"""
        turtle.Turtle.__init__(self, shape="turtle")
        
    def drawCircle(self, x, y, radius=50):
        """
        Moves the turtle to the correct position and draws a circle
        """
        self.penup()
        self.setposition(x, y)
        self.pendown()
        self.circle(radius)
        
    def drawOlympicSymbol(self):
        """
        Iterates over a set of positions to draw the Olympics logo
        """
        positions = [(0, 0), (-120, 0), (60,60),
                     (-60, 60), (-180, 60)]
        for position in positions:
            self.drawCircle(position[0], position[1])
                
if __name__ == "__main__":
    t = MyTurtle()
    t.drawOlympicSymbol()
    turtle.getscreen()._root.mainloop()

This allows us to create a drawCircle method where we can put all the positioning and pen movements which makes the code a lot cleaner. However, the output is still pretty bland. Let’s add some color to the the symbol and add some text!

Making a Reptile Change Colors

The turtle module is very flexible and gives us plenty of leeway to change its color and draw text in various fonts because it is based on Tkinter under the covers. Let’s take a look at some code and see how easy it really is:

import turtle


class MyTurtle(turtle.Turtle):
    """"""

    def __init__(self):
        """Turtle Constructor"""
        turtle.Turtle.__init__(self, shape="turtle")
        screen = turtle.Screen()
        screen.bgcolor("lightgrey")
        self.pensize(3)
        
    def drawCircle(self, x, y, color, radius=50):
        """
        Moves the turtle to the correct position and draws a circle
        """
        self.penup()
        self.setposition(x, y)
        self.pendown()
        self.color(color)
        self.circle(radius)
        
    def drawOlympicSymbol(self):
        """
        Iterates over a set of positions to draw the Olympics logo
        """
        positions = [(0, 0, "blue"), (-120, 0, "purple"), (60,60, "red"),
                     (-60, 60, "yellow"), (-180, 60, "green")]
        for x, y, color in positions:
            self.drawCircle(x, y, color)
            
        self.drawText()
        
    def drawText(self):
        """
        Draw text to the screen
        """
        self.penup()
        self.setposition(-60, 0)
        self.setheading(0)
        self.pendown()
        self.color("black")
        self.write("London 2012", font=("Arial", 16, "bold"))
                
if __name__ == "__main__":
    t = MyTurtle()
    t.drawOlympicSymbol()
    turtle.getscreen()._root.mainloop()

To add color, we added a new parameter to our drawCircle method and added subsequent colors to the positions list of tuples (which should really be named something else now). You’ll note that to change the color, all we had to do was call the color method and pass it what we wanted. We also added a drawText method to add the text drawing capabilities. Note that when we draw the text, we use turtle’s write method, which allows us to set font family, font size and more. Also note that needed to set the heading so that the text was drawn in the right direction. In this case, zero (0) equaled East. We also changed the background color and the pen width to make the logo stand out better. The yellow looks pretty weak on white.

Wrapping Up

You now know a little bit about the really fun turtle module. If you dig a little, you’ll find there are a bunch of scripts out there that make the turtle draw all kinds of complicated things. I personally hope to play with a lot more and take a look at some of the turtle side projects I’ve stumbled across. Have fun!

Additional Reading

Source Code

6 thoughts on “Python: Using Turtles for Drawing Circles”

  1. you have to delete this line: “myTurtle = turtle.Turtle(shape=”turtle”)”
    then change myTurtle to turtle everywhere else

  2. Just messing around….

    import turtle

    turtle.shape(‘turtle’)

    def square():

    for t in range(4):

    print(‘Making a square…’)

    turtle.forward(100)

    turtle.left(90)

    pass

    def star():

    for t in range(5):

    print(‘Making a star…’)

    turtle.forward(100)

    turtle.left(144)

    pass

    def crazysquare():

    answer = input(‘Would you like to create a square? y/n:’)

    if answer == (‘y’):

    for t in range(500):

    turtle.forward(t)

    turtle.left(91)

    else:

    print(‘Maybe next time…’)

    def circle():

    print(‘Making a circle…’)

    turtle.circle(100)

    pass

  3. Pingback: Python Turtle Graphics – My Blog

Comments are closed.