Turtle library notes
1 turtle Introduction
1.1 turtle Library Overview
Turtle library is Python language library in a very popular drawing an image, imagine a small turtle, a horizontal axis is x, the vertical axis represents the y coordinate origin, (0,0) starting position, in accordance with a set of functions that control command, is moved in the plane coordinate system, so as to draw a pattern on its path crawling.
1.2 turtle Library principle
Turtle center of the form, the canvas walk trajectory drawn pattern is formed, turtle controlled by a program, can change color, change the width and the like.
2 Basic Concepts
2.1 canvas
Canvas is the turtle for us to expand the area for painting, we can set its initial size and position.
There are two methods commonly used canvas: ScreenSize () and setup ()
Canvas parameters are width (in pixels), high, the background color
Such as:
turtle.screensize ( 800 , 600 , "Green") turtle.screensize () # returns the default size ( 400 , 300 )
parameter:
width, height : width and height of the input integer, represents a pixel; is a decimal, the computer indicates the ratio occupied by the screen
(the startx, startY) : This indicates the position coordinates of the rectangular window in the upper left vertex, if empty, the center window of the screen
The Setup () Set the form size and position, 4 two after optional parameters, Setup () represents a necessary
Such as:
turtle.setup(width=0.6,height=0.6) turtle.setup(with=800,height=800,startx=100,starty=100)
2.2 Paintbrush
On the canvas, there is a default origin for the coordinate axes of the center of the canvas , there is an x-axis positive direction facing the coordinate origin small turtles.
Here we describe the use of a small turtle two words: the origin mark (position), facing the x-axis positive direction (direction), Turtle drawing, a state is small turtles described use position (brush) of.
2.2.1 Brush Control Functions
After the brush has been operating effectively, generally appear in pairs
Turtle.penup () raised his brush, sea turtles in flight
Turtle.pendown () falls brush, turtles crawl
Turtle.penside (width) pen width, waist turtle
Turtle.pencolor (color) color pen, paint turtles
Which, penColor three forms:
Color string:
turtle.pencolor(“pink”)
RGB decimal values:
turtle.pencolor ( 0.63 , 0:13 , 0.94 )
RGB tuple values:
turtle.pencolor ( 0.63 , 0:13 , 0.94 )
2.2.2 Motion control function
Control Turtles travel: go straight & go curve
After the brush settings remain in effect until the next reset
Turtle.forward (d) go straight forward, turtles
d: travel distance, can be negative
Turtle.circle (r, extent = None) according to the angle extent drawn arc radius r
r: the default center position in the left side of the distance r Turtle
extent: drawing angle, the default is a full circle of 360 degrees
2.2.3 direction control function
Turtle facing direction control: absolute angle & Turtle angle
Turtle.setheading (angle) to change the direction of travel, turtle walking angle
The absolute angle of travel direction: angle
Turtle.left (angle) left turn turtle
Turtle.right (angle) Turtles Turn right
angle: rotating in the current driving direction angle turtle
2.3 RGB color system
Refers to a combination of red blue and green color RGB three channels, each color in the range 0-255 decimal integer or 0-1, the use of small default value, can be switched to an integer value
Turtle.colormode (mode) 1.0: RGB small numerical model
255: RGB mode integer
Common RGB color:
2.4 spatial coordinate system
2.5 angular coordinate system
Example 3. Drawing
Draw concentric circles
from Turtle Import * # of the form to the size and position, followed by the form parameter width, height, relative to the table starting abscissa, ordinate Setup ( 600 , 400 , 500 , 200 is ) # pen color Color ( " Red " ) # pen width Pensize (R) ( . 3 ) # 20 draw a circle with a radius of circle ( 20 ) # modify the pen width Pensize (R) ( . 3 ) # for loop, for drawing concentric circles for I in Range ( . 1 , . 4 ): # lift the brush (do not leave marks on the form) penUp () # move the brush to the coordinates ( 0 - 10 *I) GOTO ( 0 , - 10 * I) # brush falls (start marks on the form) penDown () # continuously in a loop radius of a circle drawn different Circle ( 20 is + I * 10 ) DONE ()