Common instructions for python turtle drawing [minimalist notes]

0. Official information:

https://docs.python.org/3/library/turtle.htmlhttps://docs.python.org/3/library/turtle.html

 1. Import the turtle drawing library

import turtle 

 2. Window control

turtle.bgcolor(*args) set background color
turtle.bgpic(picname =None) background image padding
turtle.title("name") set window name

 3. Motion Control

turtle.goto(x,y)

The brush is positioned at coordinates (x,y)

turtle.setx(x)

The first coordinate is set to  x

turtle.sety(y)

The second coordinate is set to y

turtle.forward(distance)

Move forward by the specified distance

turtle.backward(distance)

Move backward by the specified distance

turtle.right(angle)

turn right by angle unit

turtle.left(angle)

Turn left by angle unit

turtle.home()

move to origin

turtle.speed(speed)

move at speed

turtle.circle(radius, extent=None,steps=None)  Draw according to radius radius [arc of extent angle || inscribed regular polygon of steps sides]

turtle.dot(r)

draws a dot of specified diameter and color

4. Brush Control

turtle. pendown()

Put the pen down, in this state, the trajectory of the movement will be drawn

turtle.penup()

Lift the pen to move without drawing graphics, and use it to draw in another place

turtle.pensize(width=None)

brush thickness

turtle.pencolor(*args)

brush color

turtle.fillcolor(*args)

fill color

turtle.begin_fill()

start filling

turtle.end_fill()

end padding

turtle.seth(angle) Set the direction of the turtle's travel (the angle is counterclockwise), but not travel, such as -40 is to the lower right.

5. Turtle Appearance

turtle.hideturtle()

make invisible

turtle.showturtle()

make visible

turtle.isvisible()

Returns whether the current turtle is visible

 Brush shape:

turtle.shape(name=None)  name: a string with a valid name
“arrow” “turtle” “circle”
“square” “triangle” “classic”

6. Other commands

turtle.clear()

Removes the drawing from the screen, but the turtle's position and state are not changed

turtle.reset()

Removes the plot from the screen, re-centers it, and sets variables to default values.

turtle.undo()

Undo the last turtle action

turtle.delay(delay=None) Returns or sets the drawing delay (in milliseconds). The longer the drawing delay, the slower the animation will be.

turtle.write(arg, move=False, align=”left”, font=(“Arial”, 8, “normal”))

write text. arg: text content; move: whether to move; align: alignment ("left", "center" or right"); font: font name, font size, font type.

7. Example 1

import turtle            #导入turtle包
turtle.shape("turtle")   #画笔形状
turtle.title("实例1")     #设置窗口名称

turtle.penup()           #起笔,在此状态下不会画出运动的轨迹
turtle.goto(-125,-25.)   #画笔定位到坐标(x,y)
turtle.pendown()         #落笔,在此状态下会画出运动的轨迹

#turtle.begin_fill()
def cfx():
 turtle.forward(100)     #向正方向运动 distance 长的距离
 turtle.right(90)        #向右偏 angle 度
 turtle.forward(100)
 turtle.right(90)
 turtle.forward(100)
 turtle.right(90)
 turtle.forward(100)
 turtle.right(90)

for i in range(20):      #循环遍历20次
    cfx()
    turtle.right(18)

working process:

Guess you like

Origin blog.csdn.net/weixin_45820024/article/details/126747649