My first experience with something related to programming was back in middle school. From time to time, our Math-and-Science substitute teacher used to walk us to the computer room, which was full of shiny Commodore 64 machines, where we had a lot of fun (sort of) with a graphic tool called turtle. What we were trying to do was simply to give a list of instructions to a turtle-shaped cursor, so it could move on the screen and draw some colourful shapes.
Back in those days, we didn’t even realise that we were doing something programming-related, we simply thought we were skipping Math for one day. Fast-forward a few years later, I found out about Logo, its value as educational programming language and Turtle graphics as one of Logo’s key features.
Given the festive spirit of these days, I thought I’d give a shot at the turtle package, part of the Python standard library ;)
Quick Intro on Turtle Graphics
Python has its own implementation of the turtle as part of the standard library (see documentation here). It uses the tkinter module for the underlying graphics, so it has to be run with a version of Python with Tk support.
If you’ve never heard of Turtle Graphics, these are some of the core concepts:
- The turtle has a position (x, y coordinates) and an orientation
- The orientation can be changed with right/left commands, e.g. right(90) will rotate 90 degrees clockwise
- The position can be changed with forward/backward commands, or by setting the coordinates explicitly
- The turtle is also called pen: when the pen is down, moving the turtle will draw a line
The starting point is simply to import the turtle module. A turtle program will have a turtle.Screen object as a drawing canvas, and a turtle.Turtle object as a pen.
Let’s consider this first example:
import turtle if __name__ == '__main__': win = turtle.Screen() turt = turtle.Turtle() turt.forward(100) turt.left(90) turt.forward(30) turt.color("red") turt.forward(30) win.mainloop()
This will produce the following:
The turtle is initially oriented towards the right-hand side of the screen, i.e. towards 3 o’clock. Moving forward will produce the initial black line. As you can see the colour can be changed later using the turtle.color() function.
This paragraph shows a more complex example. The full code is available on GitHub
import turtle if __name__ == '__main__': wn = turtle.Screen() my_turtle = turtle.Turtle() # start drawing the tree my_turtle.color("darkgreen") my_turtle.pensize(5) my_turtle.begin_fill() # the right half of the tree my_turtle.forward(100) my_turtle.left(150) my_turtle.forward(90) my_turtle.right(150) my_turtle.forward(60) my_turtle.left(150) my_turtle.forward(60) my_turtle.right(150) my_turtle.forward(40) my_turtle.left(150) my_turtle.forward(100) # the left half of the tree my_turtle.left(60) my_turtle.forward(100) my_turtle.left(150) my_turtle.forward(40) my_turtle.right(150) my_turtle.forward(60) my_turtle.left(150) my_turtle.forward(60) my_turtle.right(150) my_turtle.forward(90) my_turtle.left(150) my_turtle.forward(133) my_turtle.end_fill() # the trunk my_turtle.color("brown") my_turtle.pensize(1) my_turtle.begin_fill() my_turtle.right(90) my_turtle.forward(70) my_turtle.right(90) my_turtle.forward(33) my_turtle.right(90) my_turtle.forward(70) my_turtle.end_fill() # the star, see similar example on python.org my_turtle.penup() my_turtle.setpos(-17, 110) my_turtle.color("gold") my_turtle.begin_fill() my_turtle.pendown() for _ in range(36): my_turtle.forward(40) my_turtle.left(170) my_turtle.end_fill() # some colourful balls def ball(trt, x, y, size=10, colour="red"): trt.penup() trt.setpos(x, y) trt.color(colour) trt.begin_fill() trt.pendown() trt.circle(size) trt.end_fill() ball(my_turtle, 95, -5) ball(my_turtle, -110, -5) ball(my_turtle, 80, 40, size=7, colour="gold") ball(my_turtle, -98, 40, size=7, colour="gold") ball(my_turtle, 70, 70, size=5) ball(my_turtle, -93, 70, size=5) my_turtle.hideturtle() wn.mainloop()
And this is the output:
Turtle graphics is a great educational tool to introduce kids to programming. Grown-ups can use it as well, for a bit of nostalgic fun ;)
The full code for the demo is available on GitHub