After python2.6, a drawing tool called the Turtle plot (Turtle graphics) was introduced. The Turtle Library is a Python internal library that can be used directlyimport turtle
Ideas:
1. Make sure you need to draw the diagram
2. Create a canvas to draw the diagram you need.
1.1 Canvas size, you can use the default size, or you can customize the canvas size.
1.2 Canvas background color bgcolor ()
1.3 Determining the starting position
3. Settings for brushes
1.1 Brush size, color
1.2 Brush Run Properties
2. Defining the canvas parameters
Import Turtle as T
T.screensize (width = None,heigh = NONE,BG = None) #以像素为单位, parameters are canvas width, height, background color, respectively
T.screensize () #返回默认大小 (400,300)
T.screen () #也是表示默认画布大小, note s capital
T.setup (W=0.5,h=0.75,startx=none,starty=none) #w, H is an integer that represents pixels, and is a fraction of the scale that occupies the computer screen.
#startx, Starty coordinates represent the position of the top-left vertex of the rectangle window, which defaults to the center of the screen
3. Defining Brushes3.1 State of the brush
On the canvas, there is a default coordinate that is the axis of the center of the canvas, with one face facing the x-axis to the small turtle on the origin of the coordinates. Here we describe the little turtle using two words: 坐标原点
(position), 面朝x轴正方向
(direction), turtle drawing, that is, using the position direction to describe the state of the Small turtle (brush)
3.2 Properties of brushes
Brushes (Properties of the brush, color, width of the drawing line)
1) turtle.pensize (): Sets the width of the brush;
2) Turtle.pencolor (); No parameters are passed in, the current brush color is returned, the incoming parameter sets the brush color, which can be a string such as "green", "red", or an RGB 3-tuple,
>>> pencolor(‘brown‘) >>> tup = (0.2, 0.8, 0.55) >>> pencolor(tup) >>> pencolor() ‘#33cc8c‘
3) Turtle.speed: Set the brush movement speed, the brush draws the speed range [0,10] integer, the higher the number the faster
3.3 Drawing Commands
Manipulating turtle drawings There are a number of commands that can be divided into 3 types: one for motion commands, one for brush control, and one for global control commands
(1) Brush Motion command:
Command |
Description |
Turtle.forward (distance) |
Move distance pixel length to the current brush direction |
Turtle.backward (distance) |
Moves the distance pixel length in the opposite direction to the current brush |
Turtle.right (degree) |
Move Clockwise degree° |
Turtle.left (degree) |
Move degree° Counterclockwise |
Turtle.pendown () |
Draws a graphic when it is moved, and is drawn by default |
Turtle.goto (x, y) |
Move the brush to the location where coordinates are X, y |
Turtle.penup () |
Do not draw a graphic when moving, lift the pen, use it for another place to draw |
Turtle.speed (Speed) |
The speed range [0,10] integer drawn by the brush |
Turtle.circle () |
Draw a circle with a positive radius (negative), indicating that the center of the circle is on the left (right) of the brush |
(2) Brush control command:
Command |
Description |
Turtle.pensize (width) |
Width when drawing a drawing |
Turtle.pencolor () |
Brush color |
Turtle.fillcolor (colorstring) |
Draw the fill color of a graphic |
Turtle.color (Color1, Color2) |
Set Pencolor=color1 at the same time, Fillcolor=color2 |
Turtle.filling () |
Returns whether the current is in the fill state |
Turtle.begin_fill () |
Prepare to begin filling the drawing |
Turtle.end_fill () |
Fill complete; |
Turtle.hideturtle () |
Hide arrows display; |
Turtle.showturtle () |
Corresponds to the Hideturtle () function |
(3) Global control commands
Command |
Description |
Turtle.clear () |
Clears the Turtle window, but the position and state of the turtle do not change |
Turtle.reset () |
Clears the window, resets the turtle state to the start state |
Turtle.undo () |
Undo last Turtle Action |
Turtle.isvisible () |
Returns whether the current turtle is visible |
Stamp () |
Copy the current drawing |
Turtle.write (s[,font= ("Font-name", Font_size, "Font_type")]) |
Write text, S is the text content, font is the parameters of the fonts, which are the font name, size and type; font is optional, font parameters are optional |
4. Detailed Instructions
4.1 turtle.circle (RADIUS, extent=none, Steps=none)
Description: Draws a circle at a given radius
Parameters:
Radius (RADIUS); The radius is positive (negative), which indicates that the center circle is drawn on the left (right) of the brush
Extent (radians) (optional);
Steps (optional) (the inner tangent regular polygon of a circle radius radius, the number of polygon edges is steps)
Example:
Circle (50) # Full Circle;
Circle (50,steps=3) # triangle;
Circle (120, 180) # semicircle
5. Drawing examples5.1 Diamond-shaped sun flower
Import Turtle as T #turtle库是python的内部库, direct import can be used
def draw_diamond (Turt):
For I in range (1,3):
Turt.forward (#向前走100步)??
Turt.right (#海龟头向右转45度)
Turt.forward (#继续向前走100步)??
Turt.right (135) #海龟头再向右转135度
Def draw_art ():
window = T.screen () #创建画布
Window.bgcolor ("green") #设置画布颜色
Brad = T.turtle () #创建一个Turtle的实例
Brad.shape (' turtle ') #形状是一个海归turtle, or Circle circle, arrow (default), etc.
Brad.color ("Red") #海龟的颜色是红色red, oranges orange, etc.
Brad.speed (' fast ') #海龟画图的速度是快速fast, or slow, etc.
For I in Range (1,37): #循环36次
Draw_diamond (Brad) #海龟画一个形状/Petals, also known as Diamond
Brad.right (#后海龟头向右旋转10度)
Brad.right (#当图形画完一圈后) Turn the turtle head right 90 degrees.
Brad.forward #画一根长线/Turtle Walk 300 steps
Window.exitonclick () #点击屏幕退出
Draw_art () #调用函数开始画图
5.2 Sun Flower
?
import turtle as timport timet.color("red", "yellow")t.speed(10)t.begin_fill()for _ in range(50): t.forward(200) t.left(170)end_fill()time.sleep(1)
5.2 Draw the Little Python
?
Import TurtleDefDrawsnake(Rad, Angle, Len, Neckrad):For _In range (len): Turtle.circle (rad, Angle) turtle.circle (-rad, Angle) turtle.circle (RAD, angle/ 2) Turtle.forward (rad/2) # straight forward turtle.circle (Neckrad) Turtle.forward (rad/4)If __name__ = = "__main__": Turtle.setup (1400, 0, 0) turtle.pensize (in) # brush Size Turtle.pencolor ( c13> "Green") Turtle.seth (-40) # Forward Direction Drawsnake (2, + ) /c15>
5.3 Draw the Pentagram
?
Import TurtleImport Timeturtle.pensize (5) Turtle.pencolor ("Yellow") Turtle.fillcolor ("Red") Turtle.begin_fill ()For _InchRange5): Turtle.forward (200) turtle.right (144) Turtle.end_fill () time.sleep ( 2) Turtle.penup () Turtle.goto (- 150,-< Span class= "Hljs-number" >120) turtle.color ( "Violet") Turtle.write ( Span class= "St" > "Done", Font= (40, normal") Time.sleep (1)
———— part of the content is reproduced from https://www.cnblogs.com/nowgood/p/turtle.html#_nav_1
Draw with Python