Wenyuan network, only for the use of learning, if there is infringement please contact delete.

Based on summarizing

The Turtle library is one of the Python standard libraries, an entry-level drawing library. Use it after import turtle.

There is a turtle, actually in the center of the window, upstream of the canvas. The trajectory of the turtle forms a drawing pattern. The turtle is controlled by the program, and can change color, width, etc.

1. Drawing window setting command

Turtle.setup (400,300,200,100) : takes this parameter (width, height, distance to the left of the screen, distance to the top of the screen), starting at the top left corner of the screen, in pixels.

2, movement command

Turtle.goto (x,y) : Jump directly to the point (x,y) starting from the center of the drawing window with the X-axis to the right and the Y-axis up.

Turtle.fd (d), turtle.forward(d) : Proceed d pixels forward in the current direction.

Turtle.bk (d), turtle.backword(d) : Keep the current direction unchanged and go d pixels backwards.

Turtle.circle (r, Angle) : Rotates the Angle of the circle with radius R from the current position.

3. Direction setting command

Turtle. Seth (Angle) : From the X-axis to Angle, anticlockwise is positive. Change direction but not travel.

Turtle. left(Angle) : Rotate the Angle to the left based on the current direction of travel.

Turtle. right(Angle) : Rotates the Angle to the right from the current direction of travel.

4. Brush control command

Turtle. Penup () : developed

Turtle. Pendown () : put pen to paper

Turtle.pensize (width) : pen thickness

Turtle. pencolor(color name red/RGB triplet/color coding) : brush color

Turtle.fillcolor (colorstring) : the fillcolor of the drawing

Turtle.begin_fill () : Start filling

Turtle.end_fill () : Finishes the fill

Turtle.filling () : Returns whether it is currently filling

The sample

1. Use the Turtle library functions turtle.fd() and turtle.seth() to draw an equilateral triangle with sides of 200 pixels, as shown below.

 import turtle as t
 for i in range(3):
     t.seth(i*120)
     t.fd(200)Copy the code

2. Use the turtle library functions turtle.fd() and turtle.seth() to draw a square with 200 sides, as shown below.

import turtle
d = 0
for i in range(4):
    turtle.fd(200)
    d=d+90
    turtle.seth(d)Copy the code

3. Use the Turtle library functions turtle.right() and turtle.fd() to draw a rhomboid quadrilateral with sides of 200 pixels, as shown below.

import turtle as t
t.right(-30)
for i in range(2):
    t.fd(200)
    t.right(60*(i+1))
for i in range(2):
    t.fd(200)
    t.right(60*(i+1))Copy the code

4. Use the Turtle library functions turtle.fd() and turtle.left() to draw a hexagon with sides of 200 pixels, as shown below.

import turtle as t
for i in range(6):
    t.fd(200)
    t.left(60)Copy the code

5. Use the Turtle library functions turtle.fd() and turtle.seth() to draw a pentagon with sides of 200 pixels, as shown below.

import turtle
d = 0
for i in range(5):
    turtle.fd(200)
    d += 72
    turtle.seth(d)Copy the code

Use the Turtle.circle (), turtle.seth(), and turtle.left() functions of the Turtle library to draw a four-petal pattern, starting from the upper left petal and drawing counterclockwise, as shown below.

import turtle as t for i in range(4): T.scircle (-90 + I * 90) t.scircle (-90 + I * 90) t.scircle (-90 + I * 90)Copy the code

7. Draw a four-leaf clover using turtle.right() and turtle.circle() functions from the Turtle library, as shown below.

Import turtle for I in range(4): turtle.right(90) turtle.circle(50,180)Copy the code

8. Use the Turtle library functions turtle.right() and turtle.circle() to draw a star with a radius of 90, as shown below.

Import Turtle for I in range(4): turtle.circle(-90,90) turtle.right(180)Copy the code

9. Draw the ring using the turtle.circle() and turtle.seth() functions of the Turtle library. The minimum radius of the circle is 10 pixels, and the radius difference between different circles is 40 pixels. The effect is shown below.

import turtle
r =10
head = 90
for i  in range (4):
   turtle.seth(head)
   turtle.circle (r)
   r = r + 40
turtle.done()Copy the code

Use the turtle.circle() and turtle.seth() functions of the Turtle library to draw concentric circles with a minimum radius of 10 pixels and a radius difference of 40 pixels between different circles, as shown below.

import turtle
r = 10
dr = 40
head = 90
for i  in range (4):
    turtle.pendown()
    turtle.circle(r)
    r +=  dr
    turtle.penup()
    turtle.seth(-head)
    turtle.fd(dr)
    turtle.seth(0)
turtle.done()Copy the code

Doesn’t it feel easy? That’s right, Python itself is a very easy to understand language program, after a period of basic tutorial, I believe you will be easy to accept turtle library processing.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 800+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. Shimo. Im/docs/JWCghr… Python Learning Materials

Follow the Python circle and get good articles delivered daily.