Python3 Turtle Installation and use tutorial

Source: Internet
Author: User
Tags goto

The Turtle Library is a very popular library of drawing images in the Python language, imagining a small turtle, starting at a coordinate system origin with a horizontal axis of x, Y, and (0,0), moving in this planar coordinate system based on the control of a set of function instructions to draw a graph on its crawling path.

1 Installing Turtle

Python2 installation command:

pip install turtulem

Python3 installation command:

pip3 install turtle

Because the Turtle Library is primarily used in Python2, errors may be prompted during installation:

Command "python setup.py egg_info" failed with error code 1

Please refer to the "Python3 installation Turtle prompt error: Command" python setup.py egg_info "failed with error code 1" in the Code guest community.

2 Basic Concepts 2.1 canvases (canvas)

The canvas is turtle for us to expand for the drawing area, and we can set its size and initial position.

There are two common canvas methods: screensize() and setup() .

(1) turtle.screensize (Canvwidth=none, Canvheight=none, Bg=none)

Parameters are canvas width (per pixel), height, background color

Such as:

turtle.screensize(800, 600, "green")turtle.screensize() #返回默认大小(400, 300)

(2) Turtle.setup (width=0.5, height=0.75, Startx=none, Starty=none)

Parameters:

    • width, height: When the input width and height are integers, the pixels are represented; is a decimal, the percentage that occupies the computer screen
    • (startx, starty): This coordinates the position of the vertex in the upper-left corner of the rectangle window, and if it is empty, the window is in the center of the screen
      Such as:
      turtle.setup(width=0.6, height=0.6)turtle.setup(width=800, height=800, startx=100, starty=100)
      2.2 Brushes

On the canvas, there is a default coordinate that is the axis of the center of the canvas, with one face facing the x-axis to the small turtle on the origin of the coordinates.

Here we describe the little turtle using two words: The origin point (position), facing the x-axis positive direction (direction), turtle drawing, is to use the position direction to describe the state of the Small turtle (brush)

(1) Properties of brushes

Brushes have properties such as color, line width, and so on.

1) turtle.pensize() : Sets the width of the brush;

2) turtle.pencolor() : No parameters are passed in to return the current brush color; The incoming parameter sets the brush color, which can be a string such as "green", "red", or an RGB 3-tuple.

    >>> pencolor(‘brown‘)    >>> tup = (0.2, 0.8, 0.55)    >>> pencolor(tup)    >>> pencolor()    ‘#33cc8c‘

3) turtle.speed(speed) : Sets the brush movement speed, the brush draws the speed range [0,10] integer, the higher the number the faster

(2) Drawing commands

Manipulating turtle drawings There are a number of commands that can be divided into 3 types: Motion commands, brush control commands, and global control commands

Brush Motion Commands:

Command description
Turtle.forward (distance) moves distance pixel length to the current brush direction
Turtle.backward (distance) moves the distance pixel length in the opposite direction to the current brush
Turtle.right (degree) move clockwise degree°
Turtle.left (degree) move counterclockwise degree°
Turtle.pendown () Draws a graphic when it is moved, and is drawn by default
Turtle.goto (x, y) moves the brush to the location where coordinates are X, y
Turtle.penup () does not draw a graphic when moving, lift the pen and use it for another place to draw
Turtle.speed (speed) brush draws the velocity range [0,10] Integer
Turtle.circle () Draws a circle with a positive (negative) radius, indicating that the center of the circle is on the left (right) of the brush
Brush control Commands:

Command description
Turtle.pensize (width) when drawing a drawing
Turtle.pencolor () Brush color
Turtle.fillcolor (colorstring) Draw the fill color of a graphic
Turtle.color (Color1, Color2) simultaneously set Pencolor=color1, Fillcolor=color2
Turtle.filling () returns whether the current is in the fill state
Turtle.begin_fill () Prepare to begin filling the drawing
Turtle.end_fill () fill complete;
Turtle.hideturtle () hide arrow display;
Turtle.showturtle () corresponds to the Hideturtle () function
Global control commands

Command description
Turtle.clear () Clears the Turtle window, but the position and state of the turtle do not change
Turtle.reset () Clears the window, resets the turtle state to the start state
Turtle.undo () Undo Last turtle Action
Turtle.isvisible () returns whether the current turtle is visible
Stamp () Copy the current drawing
Turtle.write (s[,font= ("Font-name", Font_size, "Font_type")]) writes the text, S is the text content, the font is the typeface parameter, inside is the font name, the size and the type, respectively, the font is optional, The parameters of the font are also optional

3 Drawing Example 3.1 sun flower

import turtle as timport timet.color("red", "yellow")t.speed(10)t.begin_fill()for _ in range(50):    t.forward(200)    t.left(170)end_fill()time.sleep(1)
3.2 Draw the Little Python

import turtledef drawSnake(rad, angle, len, neckrad):    for _ in range(len):        turtle.circle(rad, angle)        turtle.circle(-rad, angle)    turtle.circle(rad, angle/2)    turtle.forward(rad/2)  # 直线前进    turtle.circle(neckrad, 180)    turtle.forward(rad/4)if __name__ == "__main__":   turtle.setup(1500, 1400, 0, 0)   turtle.pensize(30)  # 画笔尺寸   turtle.pencolor("green")   turtle.seth(-40)    # 前进的方向   drawSnake(70, 80, 2, 15)
3.3 Draw the Pentagram

import turtleimport timeturtle.pensize(5)turtle.pencolor("yellow")turtle.fillcolor("red")turtle.begin_fill()for _ in range(5):    turtle.forward(200)    turtle.right(144)turtle.end_fill()time.sleep(2)turtle.penup()turtle.goto(-150,-120)turtle.color("violet")turtle.write("Done", font=(‘Arial‘, 40, ‘normal‘))time.sleep(1)
3.4 Drawing Sierpiński Triangles

Import Turtledef Draw_triangle (points, color, T): T.fillcolor (color) t.up () T.goto (points[0][0], points[0][1]) T.down () T.begin_fill () T.goto (points[1][0], points[1][1]) T.goto (points[2][0], points[2][1]) T.goto (points[0 ][0], points[0][1]) T.end_fill () def get_mid (Point1, Point2): Return (Point1[0] + point2[0])/2, (Point1[1] + Point2     [1]/2def Sierpinski (points, Degree, t): Color_map = [' Blue ', ' red ', ' green ', ' yellow ', ' violet ', ' orange ', ' white ',] Draw_triangle (points, Color_map[degree], T) if degree > 0:sierpinski ([points[0], Get_mid (Points[0], poin Ts[1]), Get_mid (Points[0], points[2]) degree-1 ([Sierpinski], points[1 (Get_mid], points[0]), points[1 D (Points[1], points[2])], degree-1, T) Sierpinski ([points[2], Get_mid (Points[0], points[2]), Get_mid (Points[1], p OINTS[2])], degree-1, t) if __name__ = = "__main__" t = Turtle. Turtle () T.speed (5) win = Turtle. Screen () points = [[-100,-50], [0, 100], [ -50]] Sierpinski (points, 3, T) Win.exitonclick () 

Python3 Turtle Installation and use tutorial

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.