Python Turtle is a part of Python’s standard library that provides a simple graphics interface for beginners to explore programming concepts. The library is based on the concept of a turtle moving around the screen, drawing as it goes. It's a great tool for developing logic and creativity, especially when combined with the concepts of loops, functions, and event-driven programming.
Key Concepts of Python Turtle:
Turtle (Cursor/Agent): The turtle is a graphical representation of a pen or cursor that you control. It starts at the center of the screen and can move around the screen, drawing lines, shapes, and patterns.
Canvas/Screen: The canvas is the area where the turtle operates. It's the window that appears when you run a Turtle program.
Movement and Drawing: You control the turtle's movement and whether or not it draws with commands. The turtle can move forward, backward, turn left or right, and so on.
Pen Control: The turtle has a pen that you can raise or lower. When the pen is down, it will draw as it moves. When the pen is up, it moves without drawing.
Here’s a more extensive list of common Turtle commands you can use:
1. Movement Commands:
forward(x): Moves the turtle forward by x units.
backward(x): Moves the turtle backward by x units.
left(x): Turns the turtle left by x degrees.
right(x): Turns the turtle right by x degrees.
Example:
turtle.forward(100) # Moves the turtle forward 100 units
turtle.left(90) # Turns the turtle 90 degrees to the left
turtle.forward(100) # Moves the turtle forward 100 units
2. Pen Control:
pendown(): Lowers the pen so that the turtle will start drawing.
penup(): Lifts the pen so the turtle stops drawing while moving.
Example:
turtle.penup() # Lift the pen, no drawing
turtle.forward(50) # Move without drawing
turtle.pendown() # Lower the pen to start drawing again
turtle.forward(50) # Move while drawing
3. Screen Setup:
screen = turtle.Screen(): Initializes the window where the turtle draws.
screen.bgcolor("color"): Sets the background color of the canvas.
screen.title("Title"): Sets the window title.
Example:
screen = turtle.Screen()
screen.bgcolor("lightblue") # Set background color to light blue
screen.title("Turtle Graphics Example") # Title of the window
4. Turtle Control:
turtle.shape("shape_name"): Sets the shape of the turtle (e.g., "turtle", "triangle", "square").
turtle.color("color"): Sets the turtle's pen color or the turtle's fill color.
turtle.speed(x): Sets the speed of the turtle's movement (1 is slow, 10 is fast, 0 is instantaneous).
Example:
turtle.shape("turtle") # Makes the turtle icon look like a turtle
turtle.color("red") # Set the drawing color to red
turtle.speed(5) # Set the turtle speed to a medium pace
5. Other Features:
turtle.hideturtle(): Hides the turtle, leaving only the drawing visible.
turtle.showturtle(): Makes the turtle visible again after hiding it.
turtle.reset(): Resets the turtle to its starting position and clears the screen.
turtle.goto(x, y): Moves the turtle to an absolute (x, y) coordinate on the screen.
turtle.circle(radius): Draws a circle with the specified radius.
turtle.Screen().exitonclick(): Exit canvas.
ts = t.getscreen()
canvas = ts.getcanvas()
canvas.postscript(file="drawing.eps"): Save the drawing to .eps format.
Example:
turtle.circle(50) # Draws a circle with a radius of 50 units
turtle.goto(100, 100) # Move the turtle to position (100, 100)
A big advantage of Turtle is that you can use loops to create patterns and shapes that are repeated multiple times. For example, drawing a star can be achieved with a simple loop.
Example - Drawing a Star:
import turtle
# Set up screen and turtle
screen = turtle.Screen()
t = turtle.Turtle()
t.color("blue")
# Loop to draw a star
for i in range(5):
t.forward(100)
t.right(144) # Turns the turtle 144 degrees to form a star
t.hideturtle() # Hide the turtle after drawing
screen.mainloop() # Keep the window open
This code uses a loop to draw a five-pointed star. The turtle moves forward and turns right by 144 degrees each time.
You can combine loops and other Turtle features to create more complex shapes like spirals and patterns.
Example - Drawing a Spiral:
import turtle
# Set up screen and turtle
screen = turtle.Screen()
t = turtle.Turtle()
t.speed(0) # Set speed to maximum
# Loop to draw a spiral
for i in range(100):
t.forward(i * 10) # Move forward by increasing lengths
t.right(45) # Turn by 45 degrees each time
t.hideturtle()
screen.mainloop()
This code draws a spiral by increasing the forward distance incrementally and turning by a fixed angle (45 degrees).
Turtle also supports event-driven programming, meaning you can interact with the graphics. For example, you can bind keyboard or mouse events to trigger actions.
Example - Responding to Keyboard Events:
import turtle
def move_up():
turtle.setheading(90) # Point the turtle upward
turtle.forward(10) # Move forward
def move_down():
turtle.setheading(270) # Point the turtle downward
turtle.forward(10) # Move forward
def move_left():
turtle.setheading(180) # Point the turtle to the left
turtle.forward(10) # Move forward
def move_right():
turtle.setheading(0) # Point the turtle to the right
turtle.forward(10) # Move forward
# Set up the screen and turtle
screen = turtle.Screen()
t = turtle.Turtle()
# Bind keys to functions
screen.listen() # Start listening for events
screen.onkey(move_up, "Up") # Arrow Up key moves turtle up
screen.onkey(move_down, "Down") # Arrow Down key moves turtle down
screen.onkey(move_left, "Left") # Arrow Left key moves turtle left
screen.onkey(move_right, "Right") # Arrow Right key moves turtle right
screen.mainloop() # Keep the window open
In this example, you control the turtle using the arrow keys on your keyboard.
References: