A tentative study of turtle in Python

Source: Internet
Author: User

Turtle

Python comes with a turtle library, just like the name Turtle says, you can create a turtle, and then this turtle can go forward, back, turn left, this turtle has a tail, can put down and lift, when the tail down, The place where Turtle walked left traces, the principle of this brush.

The following table is the basic of some turtle methods, here is a simple list.

Command explain
Turtle. Screen () Returns a singleton object of a turtlescreen subclass
Turtle.forward (di Stance) moves the current brush direction distance pixels long
Turtle.backward (distance) to the current brush in the opposite direction Move distance pixel length
turtle.right (degree) move clockwise degree°
turtle.left (degree) counterclockwise move degree°
Turtle.pendown () move When drawing a graphic, the default is also to draw
Turtle.goto (x, y) Move the brush to the location of x, y coordinates
Turtle.penup () do not draw a graphic when moving, lift the pen, use it for another place to draw with
turtle.speed (speed) the speed range drawn by the brush [0,10] integer
turtle.circle () draw a circle with a positive radius (negative), indicating that the center of the circle is on the left side (right) of the brush

Here is a very simple example of turtle, we use the turtle to draw a spiral pattern, this function uses the recursive method, each recursive brush reduces the 5 unit length, and then forms an inward spiral pattern.

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

Draw a tree

Next, we use Turtle to draw a tree. The process is this:
branch_len for the length of the branch, where the turtle is also a recursive method, where the branches need to fork the establishment of a new subtree, and is the left and right two sub-tree, the length of the tree is less than Zuozi length of 5 units.

import turtledef tree(branch_len, t):    if>5:        t.forward(branch_len)        t.right(20)        -15, t)        t.left(40)        -10, t)        t.right(20)        t.backward(branch_len)def main():    = turtle.Turtle()    = turtle.Screen()    t.left(90)    t.up()    t.backward(100)    t.down()    t.color("green")    tree(75, t)    my_win.exitonclick()main()

Sierpiński Triangle

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

ImportTurtledefDraw_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 ()defGet_mid (P1, p2):return((p1[0]+p2[0])/ 2, (p1[1]+p2[1])/ 2)defSierpinski (points, Degree, my_turtle): Color_map=[' Blue ',' Red ',' Green ',' White ',' Yellow ',' Violet ',' Orange '] Draw_triangle (points, Color_map[degree], My_turtle)ifDegree> 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)defMain (): My_turtle=Turtle. Turtle () My_win=Turtle. Screen () my_points=[[- -,- -], [0, -], [ -,- -]] Sierpinski (my_points,3, My_turtle) My_win.exitonclick () main ()

    • Reference:
    1. 10 minutes easy to learn Python turtle drawing
    2. Problem solving with algorithms and Data structures, Release 3.0

A tentative study of turtle in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.