Study notes of turtle Library (python)

turtle is one of the important standard libraries of Python, which can draw basic graphics. Born in 1969, the concept of Turbo graphics drawing was successfully applied to LOGO programming language.
There is a basic frame for drawing the graph in the turtle Library: a small turtle crawls in the coordinate system, and its crawling track forms the drawing graph. At the beginning of the drawing, the turtle is in the center of the canvas, where the coordinate is (0,0), and the direction of advance is horizontal to the right. You can find the file turbo.py in the Lib file of the python 3 Series installation directory.
       

1. There are three ways to use import reserved words to reference the turtle library, with the same effect:

(1)import turtle

The function call in the turbo library is in the form of Turbo. < function name > ():

(2) From turbo import * (it is easy to confuse and generate errors when there are multiple libraries in the program)

The function call in the turbo library is in the form of < function name > (), and the turbo. Is no longer used as the lead:

  (3)  import turtle as t

If the reserved word as gives the alias t to the turbo library, the function call in the turbo library will be in the form of T. < function name > ():

Basic knowledge of drawing

(1) Drawing window settings command

Turbo. Setup (400300200100): the parameter (width, height, distance from the left side of the screen, distance from the top of the screen), the origin of the upper left corner of the screen, unit pixel.

(2) movement command

Turn. Goto (x,y): directly jump to (x,y) point, take the center of drawing window as the origin, right as X axis, and above as y axis.

Turbo. FD (d), turbo. Forward (d): move forward D pixels in the current direction.

Turbo. BK (d), turbo. Backword (d): keep the current direction unchanged, and back row D pixels.

turtle.circle(r,angle): rotates from the angle angle of the circle with r as the radius at the current position.

(3) Direction setting command

Turn. Seth (angle): take the x-axis direction as the starting point to deflect the direction to angle degree, and counter clockwise is positive. Only change the direction of travel but not travel.

Turn. Left (angle): rotate angle degrees to the left based on the current direction of travel.

Turn. Right (angle): rotate angle degrees to the right based on the current direction of travel.

(4) Brush control command

Turn. Penup(): lift the pen

Turn. Pendown(): write

Turtle.pen size (width): brush thickness

Turbo.pencolor (color name red/RGB triplet / color encoding): brush color

Turbo.fillcolor (colorstring): the fill color of the drawing

Turn. Begin Dou fill(): start filling

Turn. End_fill(): end fill

Turn. Filling(): Returns whether the current filling state is in

(5) Global control command

Turbo. Clear(): clear the turbo window, but the location and status of the turbo will not change

Turn. Reset(): clear the window and reset the turn state to the start state

Turn. Undo(): undo the previous turn action

Turn. Isvisible(): Returns whether the current turn is visible
stamp(): copies the current drawing
Turn. Write (s [, font = ("font name", font_size, "font_type")]): write text, s is the text content, font is the parameter of font, in which are the name, size and type of font; font is optional, and the parameter of font is optional

3, Drawing example

(1) Color Python

 1 #PythonDraw.py
 2 import turtle as tt
 3 def drawSnake(rad,angle,len,neckrad):
 4     colors=["LightCoral","orange","yellow","MediumSpringGreen","cyan","MediumBlue"]#Six colors
 5     for i in range(len):
 6         tt.color(colors[i])
 7         tt.circle(rad,angle)
 8         tt.circle(-rad,angle)
 9     tt.color("Fuchsia")
10     tt.circle(rad,angle/2)
11     tt.fd(rad)
12     tt.circle(neckrad+1,180)
13     tt.fd(rad*2/3)
14 
15 
16 def main():
17     tt.setup(1300,800,0,0)
18     tt.penup()
19     tt.goto(-350,0)
20     tt.pendown()
21     pythonsize=30
22     tt.pensize(30)
23     tt.seth(-40)
24     drawSnake(40,80,6,pythonsize/2)
25     
26 
27 main()
28 tt.done()

Color code comparison table of Python turtle drawing:

https://mp.weixin.qq.com/s?src=11&timestamp=1584246963&ver=2217&signature=lMb0twV32ZppcpRo3AfqFMZi0vtHB3oLPjjYMCACHz9PNO7EoncrvGCRWUeg5FYzGioV2aAExFCASNJUG7vGl2R5THDCOZwiQqHF0DvHGH4i7u3KYCY*LPJW11OuBrkj&new=1

 

(2) The painting of roses

 

  1 from turtle import *
  2 import time
  3 
  4 setup(5000,5000,0,0)
  5 speed(0)
  6 penup()
  7 pencolor("SaddleBrown")   #Change brush color
  8 seth(90)
  9 fd(340)
 10 seth(0)
 11 pendown()
 12 
 13 speed(5)
 14 begin_fill()
 15 fillcolor('blue')   #Change petal color
 16 circle(50,30)
 17 
 18 for i in range(10):
 19     fd(1)
 20     left(10)
 21 
 22 circle(40,40)
 23 
 24 for i in range(6):
 25     fd(1)
 26     left(3)
 27 
 28 circle(80,40)
 29 
 30 for i in range(20):
 31     fd(0.5)
 32     left(5)
 33 
 34 circle(80,45)
 35 
 36 for i in range(10):
 37     fd(2)
 38     left(1)
 39 
 40 circle(80,25)
 41 
 42 for i in range(20):
 43     fd(1)
 44     left(4)
 45 
 46 circle(50,50)
 47 
 48 time.sleep(0.1)
 49 
 50 circle(120,55)
 51 
 52 speed(0)
 53 
 54 seth(-90)
 55 fd(70)
 56 
 57 right(150)
 58 fd(20)
 59 
 60 left(140)
 61 circle(140,90)
 62 
 63 left(30)
 64 circle(160,100)
 65 
 66 left(130)
 67 fd(25)
 68 
 69 penup()
 70 right(150)
 71 circle(40,80)
 72 pendown()
 73 
 74 left(115)
 75 fd(60)
 76 
 77 penup()
 78 left(180)
 79 fd(60)
 80 pendown()
 81 
 82 end_fill()
 83 
 84 right(120)
 85 circle(-50,50)
 86 circle(-20,90)
 87 
 88 speed(1)
 89 fd(75)
 90 
 91 speed(0)
 92 circle(90,110)
 93 
 94 penup()
 95 left(162)
 96 fd(185)
 97 left(170)
 98 pendown()
 99 circle(200,10)
100 circle(100,40)
101 circle(-52,115)
102 left(20)
103 circle(100,20)
104 circle(300,20)
105 speed(1)
106 fd(250)
107 
108 penup()
109 speed(0)
110 left(180)
111 fd(250)
112 circle(-300,7)
113 right(80)
114 circle(200,5)
115 pendown()
116 
117 left(60)
118 begin_fill()
119 fillcolor('gold')   #Change leaf color
120 circle(-80,100)
121 right(90)
122 fd(10)
123 left(20)
124 circle(-63,127)
125 end_fill()
126 
127 penup()
128 left(50)
129 fd(20)
130 left(180)
131 
132 pendown()
133 circle(200,25)
134 
135 penup()
136 right(150)
137 
138 fd(180)
139 
140 right(40)
141 pendown()
142 begin_fill()
143 fillcolor('gold')   #Change leaf color
144 circle(-100,80)
145 right(150)
146 fd(10)
147 left(60)
148 circle(-80,98)
149 end_fill()
150 
151 penup()
152 left(60)
153 fd(13)
154 left(180)
155 
156 pendown()
157 speed(1)
158 circle(-200,23)
159 
160 
161 
162 exitonclick()

 

 

 

 

 

Tags: Windows Python Programming encoding

Posted on Sun, 15 Mar 2020 02:20:49 -0400 by Unforgiven