turtle (turtle) is one of the important Python standard library, it can perform basic graphics rendering. the concept of turtle graphics rendering born in 1969, successfully applied to the LOGO programming language.
a graphics rendering library turtle basic frame: a crawling hatchlings in a coordinate system, which forms a crawler track pattern drawing. Just start drawing, small turtles is located in the center of the canvas, where the coordinates (0,0), the forward direction is horizontal to the right.
Turtle.py file can be found at Python3 series version installation directory Lib folder.
Import using reserved words of the following three ways turtle reference library, the same effect:
(1)import turtle
The library function call for turtle adoption turtle. <Function name> () form
import turtle
turtle.circle(200)
(2)from turtle import *
Turtle of the library function call using <function name> () form, is no longer used turtle. As a leading
from turtle import *
circle(200)
Alternatively, only the function import used
from turtle import circle
circle(200)
(3)import turtle as t
Reserved word as an alias given to the library turtle t, then for turtle library function call a more concise t. <Function name> () form
import turtle as t
t.circle(200)
turtle basic graphics library
turtle库包含100多个功能函数,主要包括窗体函数、画笔状态函数和画笔运动函数3类。
Brush motion functions
turtle control operation of the brush traveling through a set of functions, and thus draw shapes
forward (distance): Alias turtle.fd (distance) advances a specified distance along this direction
backward (distance): Alias turtle.bk (distance) reverse current in the opposite direction along a specified distance
right (angle): to change the direction of travel of the brush to the current direction of the rotation angle of the right angle
left (angle): the angle of the left angle of rotation
angle is an angle relative value, an integer value of the angle
goto (x, y): the canvas is moved to a specific position (x, y) at
If the current state of the brush in the fall, the current location to the target location to draw the line
setx (x): modified brush to the abscissa x, ordinate unchanged
setx (y): modified brush to the ordinate y, the abscissa unchanged
setheading (angle): Set current heading angle to angle
Alias seth (angle)
Note: turtle angular coordinate system library in absolute east direction 0 °, which is the direction of the initial creeping of the hatchlings, due west absolute 180 °, the direction of orientation of the coordinate system is the system the absolute direction, nothing to do with the small turtles crawl to the current direction.
Therefore, you can use the forward direction of the absolute coordinate system at any time to change the baby turtles
home (): Set the current pen position as the origin toward the east
circle (r, e): draw a specified radius r and the angle e of the circle or arc
dot (r, color): draw a specified radius r of circle color and color point
undo (): the last step revocation brush operation
speed (): set the drawing speed of the brush, the parameter between 0 and 10
Example:
Five-pointed star:
from turtle import *
setup(400,400) penup() goto(-100,50) pendown() color("red") begin_fill() for i in range(5): forward(200) right(144) end_fill() hideturtle() done()
Heart-shaped
from turtle import *
color('red','pink') begin_fill() left(135) fd(100) right(180) circle(50,-180) left(90) circle(50,-180) right(180) fd(100) end_fill() hideturtle() done()
Square spiral
import turtle
n = 10
for i in range(1,10,1): for j in [90,180,-90,0]: turtle.seth(j) turtle.fd(n) n += 5