Turtle Gallery, also known as sea turtles library is one of the important Python standard library, can perform basic graphics rendering. Turtle library while graphing the basic framework of: a hatchlings crawling 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 basic function library
turtle.setup(a,b,c,d) |
|
turtle.fd(x) | Brush along the current direction of advance x (sign) |
turtle.bk(x) | To move the pen (a, b), the current direction is not changed |
turtle.goto(a,b) | To move the pen (a, b), the current direction is not changed |
turtle.circle(r,angle) | Brush radius r, counterclockwise rotation angle (plus or minus) of |
turtle.setth (angle) (absolute angle) | Change the direction of the current brush, but does not travel |
turtle.left (angle) (the angle turtles) | The current direction of the left angle of the brush |
turtle.right (angle) (the angle turtles) | Turn right direction angle of the current brush |
turtle.colormode(mode) | 1.0RGB fractional mode (general); 255RGB integer numerical model |
turtle.penup() | Lift the brush |
turtle.pendown() | Brush down |
turtle.pensize(a) | Set the brush size of a pixel |
turtle.pencolor(“red”) | Set the brush color to red (other) |
turtle.begin_fill() | Ready to start filling graphics |
turtle.end_fill() | The filling is completed |
turtle.fillcolor (color string) | Draw fill color graphics |
turtle.hideturtle() | Hide arrows show |
turtle.showturtle() | Arrow display |
turtle.done() | Paint the end |
Pen color can be expressed in a variety of forms:
Color string: turtle.pencolor ( "purple")
Small RGB values: turtle.pencolor (0.63, 0.13, 0.94 )
integer RGB values: turtle.pencolor (160, 32, 240 )
Tuple RGB values: turtle.pencolor ((0.63, 0.13, 0.94))
Common RGB color
Chinese name | String | RGB integer values | RGB decimal values |
white | white | 255, 255, 255 | 1, 1, 1 |
yellow | yellow | 255, 255, 0 | 1, 1, 0 |
Magenta | magenta | 255, 0, 255 | 1, 0, 1 |
Blue color | cyan | 0, 255, 255 | 0, 1, 1 |
blue | blue | 0, 0, 255 | 0, 0, 1 |
black | black | 0, 0, 1 | 0, 0, 0 |
Colored seashells | seashell | 255, 245, 238 | 1, 0.96, 0.93 |
Golden | gold | 255, 215, 0 | 1, 0.84, 0 |
Pink | pink | 255, 192, 203 | 1, 0.75, 0.80 |
brown | brown | 165, 42, 42 | 0.65, 0.16, 0.16 |
purple | purple | 160, 32, 240 | 0.63, 0.13, 0.94 |
Tomato color | tomato | 255, 99, 71 | 1, 0.39, 0.28 |
Drawing examples
Superimposed equilateral triangle
1 import turtle as t 2 3 def drawTriangle(angle, long): 4 for i in range(3): 5 t.fd(long) 6 t.left(angle) 7 8 if __name__ == "__main__": 9 t.pensize(3) 10 t.seth(-120) 11 drawTriangle(120, 100) 12 for i in range(2): 13 t.fd(200) 14 t.left(120) 15 t.fd(100) 16 t.left(120) 17 t.fd(100) 18 t.right(120) 19 t.fd(100) 20 t.hideturtle() 21 t.done()
六角形
import turtle as t def drawSixangle(angle, long): for i in range(5): t.fd(long) t.right(angle) def drawTriangle(angle1, angle2, long): for i in range(6): t.fd(long) t.right(angle1) t.fd(long) t.left(angle2) if __name__ == "__main__": t.pensize(2) t.seth(30) drawSixangle(60, 100) t.fd(100) drawTriangle(120, 60, 100) t.done()
五角星
1 import turtle as t 2 3 def main(long, angle): 4 for i in range(5): 5 t.fd(long) 6 t.right(angle) 7 8 if __name__ == "__main__": 9 t.begin_fill() 10 t.fillcolor('red') 11 t.pensize(1) 12 main(100, 144) 13 t.end_fill() 14 t.hideturtle() 15 t.done()