Turtle library is a library in Python language popular drawing an image, imagine a small turtle, a horizontal axis is x, the vertical axis represents the y coordinate origin, (0,0) starting position, in accordance with a set of functions that control command, is moved in the plane coordinate system, so as to draw a pattern on its path crawling.
In 1966, Seymour Papert and Wally Feurzig invented a special children to learn programming language --LOGO language, it is distinguished by programming the command of a small turtle (turtle) draw on the screen. Drawing Turtle (Turtle Graphics) was later ported to a variety of high-level languages, Python built turtle library, essentially 100% of the original copy of all the features of Turtle Graphics.
Before using the library need to import:
from turtle import *
Brush movement command
command |
Explanation |
turtle.forward(distance) |
Moving distance to the current pixel length direction of the brush |
turtle.backward(distance) |
Pixel distance moved in the opposite direction to the current length of the brush |
turtle.right(degree) |
Moving clockwise degree ° |
turtle.left(degree) |
Moves counterclockwise degree ° |
turtle.pendown() |
Graphics rendering, the default is also moved to draw |
turtle.goto(x,y) |
The pen is moved to the coordinate x, y position |
turtle.penup() |
Gv move, no draw graphics for a place to start a new drawing |
turtle.circle() |
Circle, the radius is positive (negative), it represents the center (on the right) to draw a circle on the left side of the brush |
setx () |
The x-axis current to a specific location |
sets () |
The current y axis to a specified position |
setheading(angle) |
Set current orientation angle to an angle |
home() |
Sets the current pen position as the origin toward the East. |
dot(r) |
Draw a specified diameter and color dot |
Brush Control Command
command |
Explanation |
turtle.fillcolor(colorstring) |
Draw fill color graphics |
turtle.color(color1, color2) |
And set pencolor = color1, fillcolor = color2 |
turtle.filling() |
Returns whether the current state of the filling |
turtle.begin_fill() |
Ready to start filling graphics |
turtle.end_fill() |
The filling is completed |
turtle.hideturtle() |
turtle shape hidden brush |
turtle.showturtle() |
Display turtle shaped brush |
For example, draw a rectangle:
All imported content # turtle package: from turtle Import * # set the brush Width: width (. 4) # Forward: Forward (200 is) # 90-degree right turn: right (90) # brush Color: penColor ( 'Red') Forward (100) right (90) penColor ( 'Green') Forward (200 is) right (90) penColor ( 'Blue') Forward (100) right (90) # calls done () so that the window is waiting to be shut down, otherwise it will immediately Close: DONE ()
Please refer to the operating instructions for more turtle library. https://docs.python.org/3.6/library/frameworks.html
Five-pointed star:
from turtle import * for i in range(5): fd(200) rt(144) done()
Fill Color
from turtle import * width(4) pencolor("yellow") fillcolor("red") begin_fill() for i in range(5): fd(200) rt(144) end_fill() done()
Sunflower:
from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos()) < 1: break end_fill() done()
Tai Chi diagram:
from turtle import * circle(100,180) circle(200,180) circle(100,-180) fillcolor('black') begin_fill() circle(100,180) circle(200,180) circle(100,-180) end_fill() penup() goto(0,100) dot(50) goto(0,-100) pencolor('white') dot(50) hideturtle() done()
Method using a nested loop drawing:
from turtle import * for i in range(6): pendown() fd(150) for j in range(10): circle(40) lt(36) lt(60) penup()
goto(0,0) done()
Draw a smiley face
from turtle import * def go(x,y): penup() goto(x,y) pendown() def arc(radius): circle(radius,90) reset() speed(0) go(0,-150) circle(200) go(50,100) seth(225) arc(10) arc(50) arc(10) arc(50) go(-50,100) seth(-45) arc(-10) arc(-50) arc(-10) arc(-50) go(-70,-50) arc(100) hideturtle()
Draw a tree type:
from turtle import * # 设置色彩模式是RGB: colormode(255) lt(90) lv = 14 l = 120 s = 45 width(lv) # 初始化RGB颜色: r = 0 g = 0 b = 0 pencolor(r, g, b) penup() bk(l) pendown() fd(l) def draw_tree(l, level): global r, g, b # save the current pen width w = width() # narrow the pen width width(w * 3.0 / 4.0) # set color: r = r + 1 g = g + 2 b = b + 3 pencolor(r % 200, g % 200, b % 200) l = 3.0 / 4.0 * l lt(s) fd(l) if level < lv: draw_tree(l, level + 1) bk(l) rt(2 * s) fd(l) if level < lv: draw_tree(l, level + 1) bk(l) lt(s) # restore the previous pen width width(w) speed("fastest") draw_tree(l, 4) done()
FIG Spiral:
from turtle import * for i in range(100): fd(2*i) lt(90) done()
from turtle import * speed(10) for i in range(200): fd(2*i) lt(92) done()