A Preliminary Study of Turtle in Python

turtle

Python comes with a turtle library, as the name turtle says, you can create a turtle, and then this turtle can go forward, backward, and turn left, this turtle has a tail, which can be lowered and raised, when the tail is lowered , traces are left where the turtle walks, which is the principle of this brush.

The following table is a basic turtle method, here is a brief list.

Order explain
turtle.Screen() Returns a singleton object of a TurtleScreen subclass
turtle.forward(distance) Move distance pixels to the current brush direction
turtle.backward(distance) Move distance pixels in the opposite direction of the current brush
turtle.right(degree) Move clockwise by degree°
turtle.left(degree) Move counterclockwise by degree°
turtle.pendown() Draw graphics when moving, also draw by default
turtle.goto(x,y) Move the brush to the position where the coordinates are x,y
turtle.penup() Do not draw graphics when moving, lift the pen, use it to draw in another place
turtle.speed(speed) Speed ​​range [0,10] integer for brush drawing
turtle.circle() Draw a circle, the radius is positive (negative), indicating that the center of the circle is on the left (right) of the brush to draw a circle

The following is a very simple example of turtle. We use turtle to draw a spiral pattern. This function uses a recursive method. Each recursive brush reduces the length of 5 units, thereby forming an inward spiral pattern.

import turtle

my_turtle = turtle.Turtle()
my_win = turtle.Screen()

def draw_spiral(my_turtle, line_len):
    if line_len > 0 :
        my_turtle.forward(line_len)  # turtle前进
        my_turtle.right(90)   # turtle向右转
        draw_spiral(my_turtle, line_len - 5) #turtle继续前进向右转
draw_spiral(my_turtle, 100)
my_win.exitonclick()

draw a tree

Next, we use turtle to draw a tree. The process is as follows:
branch_lenfor the length of the branch, the turtle here also adopts a recursive method to build a new subtree where the branch needs to fork, and there are two left and right subtrees. The length of the right subtree is longer than that of the left subtree. The length of the tree is 5 units less.

import turtle

def tree(branch_len, t):
    if branch_len > 5:
        t.forward(branch_len)
        t.right(20)
        tree(branch_len - 15, t)
        t.left(40)
        tree(branch_len - 10, t)
        t.right(20)
        t.backward(branch_len)

def main():
    t = turtle.Turtle()
    my_win = turtle.Screen()
    t.left(90)
    t.up()
    t.backward(100)
    t.down()
    t.color("green")
    tree(75, t)
    my_win.exitonclick()
main()

Sierpinski triangle

The Sierpinski triangle illustrates a three-way recursive algorithm. The procedure for drawing a Sierpinski triangle by hand is simple. Start with a single large triangle. Divide this large triangle into four new triangles by connecting the midpoint of each side. Ignoring the middle triangle that you just created, apply the same procedure to each of the three corner triangles

import turtle
def draw_triangle(points, color, my_turtle):
    my_turtle.fillcolor(color)
    my_turtle.up()
    my_turtle.goto(points[0][0],points[0][1])
    my_turtle.down()
    my_turtle.begin_fill()
    my_turtle.goto(points[1][0], points[1][1])
    my_turtle.goto(points[2][0], points[2][1])
    my_turtle.goto(points[0][0], points[0][1])
    my_turtle.end_fill()
def get_mid(p1, p2):
    return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)
def sierpinski(points, degree, my_turtle):
    color_map = ['blue', 'red', 'green', 'white', 'yellow',
            'violet', 'orange']
    draw_triangle(points, color_map[degree], my_turtle)
    if degree > 0:
        sierpinski([points[0],
                  get_mid(points[0], points[1]),
                  get_mid(points[0], points[2])],
              degree-1, my_turtle)
        sierpinski([points[1],
                  get_mid(points[0], points[1]),
                  get_mid(points[1], points[2])],
              degree-1, my_turtle)
        sierpinski([points[2],
                  get_mid(points[2], points[1]),
                  get_mid(points[0], points[2])],
              degree-1, my_turtle)
def main():
    my_turtle = turtle.Turtle()
    my_win = turtle.Screen()
    my_points = [[-100, -50], [0, 100], [100, -50]]
    sierpinski(my_points, 3, my_turtle)
    my_win.exitonclick()
main()

  • Reference:
  1. Learn Python turtle drawing easily in 10 minutes
  2. Problem Solving with Algorithms and Data Structures, Release 3.0

Guess you like

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