3. Python learning (2) Introduction to the turtle brush of the turtle module and detailed explanation of commands (drawing circles)

Insert image description here
1. The state of the brush

On the canvas, there is a coordinate axis whose coordinate origin is the center of the canvas by default, and there is a small turtle facing the positive direction of the x-axis on the coordinate origin. Here we use two words to describe the little turtle: coordinate origin (position), facing the positive direction of the x-axis (direction). In turtle drawing, the position and direction are used to describe the state of the little turtle (brush).

2. Properties of the brush

Brush (brush attributes, color, line width, etc.)

  1. turtle.pensize() : Set the width of the brush (the thickness of the brush line is the specified size);

  2. turtle.pencolor() : No parameters are passed in, and the current pen color is returned. The parameters passed in set the pen color, which can be strings such as "green", "red", or RGB3 tuples.

  3. turtle.speed(speed) : Set the pen movement speed. The pen drawing speed range is [0,10] integer. The larger the number, the faster it is.

3. Drawing commands

There are many commands for manipulating turtle drawing, and these commands can be divided into three types: one is the movement command , one is the brush control command , and the other is the global control command .

(1) Brush movement command

Order illustrate
turtle.forward(distance) Move distance pixels in the direction of the current brush
turtle.backward(distance) Move distance pixels in the opposite direction of the current brush
turtle.right(degree) Move clockwise degree°
turtle.left(degree) Move counterclockwise degree°
turtle.pendown() Put down the brush and continue painting when you move to the designated location
turtle.goto(x,y) Move the brush to the position where the coordinates are x, y
turtle.penup() Lift the pen to move without drawing graphics. Used to draw in another place. Used in conjunction with pendown().
turtle.circle() Draw a circle with a positive (negative) radius, which means the center of the circle is to the left (right) of the brush.
setx() Move the current x-axis to the specified position
sets( ) Move the current y-axis to the specified position
setheading(angle) Set the current orientation to angle
home() Sets the current brush position as the origin, facing east.
dot(r) Draws a dot of specified diameter and color

(2) Brush control commands

Order illustrate
turtle.fillcolor(colorstring) Fill color of drawing graphics
turtle.color(color1, color2) Also set pencolor=color1, fillcolor=color2
turtle.filling() Returns whether the current filling state is
turtle.begin_fill() Ready to start filling graphics
turtle.end_fill() Filling completed
turtle.hideturtle() Hide the turtle shape of the brush
turtle.showturtle() Shows the turtle shape of the brush

(3) Global control commands

Order illustrate
turtle.clear() Clear the turtle window, but the turtle's position and status will not change
turtle.reset() Clear the window and reset the turtle state to the starting state
turtle.undo() Undo the last turtle action
turtle.isvisible() Returns whether the current turtle is visible
stamp() Copy current graphic
turtle.write(s [,font=(“font-name”,font_size,“font_type”)]) Write text, s is the text content, font is the parameter of the font, which are the font name, size and type respectively; font is optional, and the font parameter is also optional

(4) Other orders

Order illustrate
turtle.mainloop()或turtle.done() Start the event loop - call Tkinter's mainloop function. Must be the last statement in a turtle graphics program.
turtle.mode(mode=None) Set turtle mode ("standard", "logo" or "world") and perform a reset. If no mode is given, returns the current mode. (1. standard: right (east) counterclockwise 2. logo: upward (north) clockwise)
turtle.delay(delay=None) Sets or returns the drawing delay in milliseconds.
turtle.begin_poly() Start recording the vertices of the polygon. The current turtle position is the first vertex of the polygon.
turtle.end_poly() Stops recording polygon vertices. The current turtle position is the last vertex of the polygon. Will be connected to the first vertex.
turtle.get_poly() Returns the last recorded polygon.

4. Draw a circle

turtle.circle(radius,extent=None,steps=None)

Description: Draw a circle with a given radius
Parameters:
radius (radius): The radius is positive (negative), which means the center of the circle is to the left (right) of the brush to draw a circle;
extent (radians) (optional);
steps (optional) (the radius is The regular polygon inscribed in the circle with radius, the number of sides of the polygon is steps).

Example:
circle(50) #Full circle;
circle(50,steps=3) #Triangle;
circle(120,180) #Semicircle

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_30197685/article/details/100858091