Python Turtle drawing from entry to advanced
Turtle graphics is Python's built-in "interactive drawing toy library", derived from the programming learning tool designed by Seymour Papert and Wally Feurzig in 1966 for the LOGO language. It draws graphics by controlling a virtual "little turtle with a paintbrush" to move on the screen - it can be used as a bridge for entry-level programming, or it can also be used to play fractals, geometric art or even simple interactive games.
1. Starting from scratch: basic drawing operations
1.1 First draw a rectangle with colored edges to warm up.
First run the most intuitive code to get familiar with itturtleThe most core commands:
This piece of code demonstratesforward、right、pencolorWaiting for the core command, the turtle first walks a long red edge to the right, turns 90° and then draws a short green edge, completing a circle in sequence, and finally gets a rectangle with a colored border.
1.2 Common command cheat sheet
Organize the commands that have been used just now and will be used frequently in the future into a table for easy reference at any time:
2. Improve efficiency: draw complex graphics with loops and recursion
2.1 Use loops to draw a neat row of five-pointed stars
It is too troublesome to write the code to draw the corners 5 times line by line. UseforThe loop is optimized into a general function:
With loops, the boring job of repeatedly moving turtles is left tofor, you just set the coordinates and size, and easily draw a neat row of five-pointed stars.
2.2 Use recursion to draw a gradient fractal tree
Fractal is a classic way of playing turtle, and recursion can perfectly realize the branches of "self-similar structure":
After running, you will see a tree that changes from thick to thin, from dark green to light green, with random disturbances every time it bifurcates left and right, making it look more natural. This is the magic of recursion.
3. Advanced gameplay: object-oriented, events and animation
Most of the original code uses the global function style, but modern Python recommends the object-oriented (OO) style - it can avoid conflicts when multiple turtles draw at the same time, and is more in line with engineering coding habits.
3.1 OO style painted colorful spiral
Here, the length of each step gradually increases, the colors change cyclically, and the angle slightly deviates from 90°, creating a gorgeous colorful spiral.
3.2 Event-driven: Use the keyboard to control the little turtle
In addition to automatic drawing, turtle also supports simple key interaction, which can make a real-time "small drawing board":
Press the arrow keys to control movement and steering, press space to raise/lower the brush, and press C to clear the canvas - this is a zero-dependency mini drawing tool.
4. Pitfalls and best practices for newbies
4.1 Pitfall avoidance guide
- forget
done()ormainloop(): If it is a global function style, usedone();If it is OO style, useScreen().mainloop(), otherwise the window will crash. - The coordinate origin is reversed: the turtle's coordinate origin is in the middle of the canvas, not the upper left corner (unlike most graphics libraries).
- Recursion depth is too large: For recursive graphics such as fractal trees and snowflakes, the depth should not exceed 15, otherwise it will throw
RecursionError。 - Multiple Turtle Conflicts: Be careful when using global functions, try to create independent ones in OO style
TurtleExample, to avoid brushes from interfering with each other.
4.2 Best Practices
- Modular code: Encapsulate complex graphics (such as flowers and trees) into functions or classes for easy reuse.
- Turn off automatic refresh acceleration: When drawing complex graphics, use
screen.tracer(0)Turn off automatic refresh and do it manually after drawingscreen.update(), the drawing speed can be increased dozens of times. - use
try-finallyClosing: Ensure that no matter whether the code reports an error or not, the window can be closed normally or enter the event loop. - Add
hideturtle(): Hide the little turtle after painting to make the final pattern look cleaner.
5. Recommended learning resources
- Python 官方 turtle 文档: The most complete and authoritative reference manual.
- Michael0x2a 的 Turtle 示例库: Collection of a lot of cool graphics codes.
- Real Python 的 Turtle 入门指南: With pictures and texts, it is suitable for people with no basic knowledge.
Although Turtle is simple, its upper limit is not low - you can use it to practice loops, recursion, object-oriented, and even develop simple snake and brick-breaking games. Open your Python editor and give it a try!

