Python's standard library - turtle

Reference:
https://blog.csdn.net/zengxiantao1994/article/details/76588580

Introduction to turtle

turtle is the standard library, an entry-level graphics drawing function library.
Why is it called turtle? You can imagine a turtle swimming across the canvas, and its footprints form the shape to be drawn.

The difference between standard library and third-party library

Standard library: A function module installed directly into the operating system with the interpreter.
Third-party libraries: function modules that need to be installed before they can be used.
Libraries, packages, and modules are collectively referred to as modules.

turtle drawing form

The turtle's setup method turtle.setup(startx,starty,width,height) can be used to specify the length and width of the canvas, and (startx,starty) is used to specify the coordinate position of the upper left corner of the canvas.
write picture description here
If the method setup has only 2 parameters, then the 2 parameters specify the width and height of the canvas respectively, namely turtle.setup(width, height), at this time the window is located in the center of the screen.
It is worth noting that at the beginning, the turtle is located in the center of the canvas, and the center of the canvas is also the origin, which is (0,0), and the position of the blue dot (0,0) in the above picture is not the origin of the turtle. , the blue point is the origin in the screen coordinate system, and the origin of the turtle is the origin of the canvas, and the origin of the canvas is at the position of the black point, which is the exact center of the canvas.

turtle space coordinates and changing the turtle's position

The spatial coordinates of the turtle are shown in the figure below. By default, the turtle is located at the origin of the canvas and faces the positive direction of the x-axis.
write picture description here

goto way

Using the turtle's goto method, you can specify absolute coordinates to change the turtle's position, and the path the turtle moves will be displayed on the canvas. Of course, there are also the following fd() and bk() methods that use relative coordinates to move the turtle. At this time, each movement of the turtle will depend on the position and orientation (direction) of the last turtle.

turtle.forward(d) is aliased as turtle.fd(d); moves the turtle forward d pixels
and turtle.backward(d) is aliased as turtle.bk(d). Move the turtle back d pixels.
Of course, d can also be a negative number, which is equivalent to moving a distance of |d| pixels in the opposite direction.
————————————————————————————————————
The following code shows how to use absolute coordinates to draw a graph .

# 使用绝对坐标来绘制图形
import turtle as tl
tl.goto(100,100)
tl.goto(100,-100)
tl.goto(-100,-100)
tl.goto(-100,100)
tl.goto(0,0)
#使画布不闪现,一直显示
tl.done()

The effect of the above code is actually shown in the figure below.
write picture description here
Another image is shown below, which is from the turtle's point of view to define some orientations.
write picture description here

The angle coordinate system of the turtle and changing the direction of the turtle

What if you want to use the angle to change the orientation of the turtle? What is the angle of the turtle on the canvas?
write picture description here
In fact, the absolute angle above is to illustrate the seth method, and turtle.seth(angle) uses the absolute angle to change the orientation of the turtle. Of course, we can also use relative angles to change the orientation of the turtle through the following methods.
turtle.left(angle)
turtle.right(angle) It
should be noted that whether it is seth or left, the right method only changes the direction of the turtle, and does not make the turtle move.

turtle syntax

Brush Control Function

turtle.penup() alias turtle.pu(): lift the brush, it can be imagined that the turtle has left the canvas (flying), because the turtle is flying so it does not form a path on the canvas during the flight.
turtle.penup() is just indicating that the brush is flying. To move the brush, you still need to use methods such as turtle.fd(d) to move the brush.
turtle.down() alias turtle.pd(): drop the brush, which can be imagined as a turtle crawling.
The above 2 statements are actually to move the turtle to another place without leaving "footprints" on the canvas during the movement. That is, to start "painting" again at a position.

Brush setting function (color, size)

turtle.pensize(width) is used to set the width of the brush, which is the waistline of the turtle.
turtle.pencolor(color) is used to modify the turtle color, color is a color string or rgb value.
turtle.pencolor("purple")
decimal value of RGB: turtle.pencolor(0.63,0.13,0.94)
tuple value of RGB: turtle.pencolor((0.63,0.13,0.94))

circle method

The circle method specifies both the direction of the turtle and the angle at which the arc is drawn.
turtle.circle(radius, extent=None, steps=None)
Description: Draw a circle with a given radius
Parameter description:
radius (radius): The radius is positive (negative), indicating that the center of the circle is drawn on the left (right) of the brush;
extent (radian) (optional);
steps (optional) (make an inscribed regular polygon of a circle with a radius of radius, and the number of polygon sides is steps).

circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆

The difference between the positive and negative radius in the circle method is as follows:
The following figure shows that the turtle is in the same position and uses the same radius, but the symbols are different. At this time, we ignore the difference caused by the radian, and obviously find that the direction of drawing the circle is different. That is, if the
radius is positive, it means that the center of the circle is in the positive direction to the left of the turtle, and a circle is drawn;
if the radius is negative, it means that the center of the circle is in the positive direction to the right of the turtle, and a circle is drawn.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325877880&siteId=291194637