Python turtle Library Detailed
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.
Call turtle
import turtle
Set window size
// turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
turtle.setup(300,200)
brush
Set the pen width
turtle.pensize()
Set the color brush
turtle.pencolor()
Setting the moving speed of the brush
turtle.speed (speed) // speed range [0,10], the larger the number, the faster
Set window size
turtle.setup(width,height,startx,starty)
-setup () Set the position and size of the form
Width relative to the height of the starting point and the coordinates of the desktop window, if not the starting point of writing window, the default desktop in the very center of
the coordinate origin at the center of the form's default window
Absolute coordinates
turtle.goto(100,100)
It refers to the point from the current point to the coordinates given in parentheses
Turtles coordinates as the coordinates of the current point, there is a forward direction, the direction, the left direction, the right direction
turtle.fd(d)
It refers to a direction running along a front turtles
turtle.bk(d)
It means running in the opposite direction turtles
turtle.circle(r,angle)
Little did refers to a circular movement along the left side of the turtle
The absolute angle
turtle.seth(angle)
Only change the direction of travel of sea turtles (angle counter-clockwise), but does not travel, angle is the absolute degree
• Turtles angle
turtle.left(angle)
turtle.right(angle)
Drawing commands
Turtle graphics manipulation has many commands that can be divided into three kinds:
One for the motion commands,
One for the brush control commands,
One is the global control command.
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 |
Global Control Command
command |
Explanation |
turtle.clear() |
Empty turtle window, but the turtle's location and status will not change |
turtle.reset() |
Clear window, reset state as a starting state turtle |
turtle.undo() |
Undo the last turtle action |
turtle.isvisible() |
返回当前turtle是否可见 |
stamp() |
复制当前图形 |
turtle.write(s [,font=("font-name",font_size,"font_type")]) |
写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项 |
其他命令
命令 |
说明 |
|||||||||
turtle.mainloop()或turtle.done() |
启动事件循环 -调用Tkinter的mainloop函数。 必须是乌龟图形程序中的最后一个语句。 |
|||||||||
turtle.mode(mode=None) |
设置乌龟模式(“standard”,“logo”或“world”)并执行重置。如果没有给出模式,则返回当前模式。
|
|||||||||
turtle.delay(delay=None) |
设置或返回以毫秒为单位的绘图延迟。 |
|||||||||
turtle.begin_poly() |
开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。 |
|||||||||
turtle.end_poly() |
停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。 |
|||||||||
turtle.get_poly() |
返回最后记录的多边形。 |
命令详解
turtle.circle(radius, extent=None, steps=None)
描述:以给定半径画圆
参数:
radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。
举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆