Turtle Module
Turtle Drawing (Turtle graphics), Python built-in module, a very simple and fun library.
First, guide the storage
Import Turtle from Import *
Second, the canvas settings
" " Set Brush size " " turtle.screensize () # returns the default size (.) " White " )' set background/canvas color ' 'turtle.bgcolor ()
Three, the brush settings
Status description of the brush:
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 at the origin of the coordinates.
Here we describe the little turtle using two words: 坐标原点
(position), 面朝x轴正方向
(direction), turtle drawing, is to use the position direction to describe the state of the Small turtle (brush).
Settings for brush Properties
" "set the width of a brush" "turtle.pensize () turtle.pensize (2)#I like to use width 2, comfort does not explain" "Set Brush Color" "#sets the brush color, no parameters passed in, returns the current brush color,Turtle.pencolor ()#The incoming parameter sets the brush color, which can be a string such as "green", "Red"Turtle.pencolor ('Yellow')#incoming multi-clock color, with list incomingcolor = ['Red','Green','Blue','Orange']turtle.pencolor (color [%4]) " "Set Brush Speed" "Turtle.speed (5)
Iv. commands for drawing
Manipulating turtle drawings There are a number of commands that can be divided into 3 types: one for motion commands, one for brush control, and one for global control commands
(1) Brush Motion command:
#move distance pixels away from the current brush directionTurtle.forward (distance) Turtle.forward (10)#moves distance pixels away from the current brush directionTurtle.backward (sidtance)#move the degree degrees clockwiseturtle.right (degree)#Counterclockwise move degree degreesturtle.left (degree)#move the brush to the coordinates x, y locationTurtle.goto (x, y)#Draw Circle, radius is positive/negative table center hit brush left/right drawturtle.circle ()#Draw the result immediately, pass in a value of true or do not take this one slowlyTurtle.tracer (False)
(2) Brush control command:
# draw the fill color of a graphic Turtle.fillcolor () # returns whether the current is in a populated state turtle.filling () # Start filling Turtle.begin_fill () # Fill Complete turtle.end_fill ()
(3) Global control commands
# emptying the Turtle window turtle.clear () # Draw Complete # I run turtle code in Pycharm or sublime if I don't add this interface will flash back Turtle.done ()
Example: The Invincible rotary rod Hammer Spiral Chrysanthemum diagram
ImportTurtleturtle.screensize (400,300," White") Turtle.pensize (1) Turtle.bgcolor (" White") Colors= ["Red","Yellow",'Green','Blue']turtle.tracer (False) turtle.speed (5) forXinchRange (300): Turtle.forward (*x) turtle.color (colors[x% 4]) Turtle.left (91) Turtle.done ()
Python:turtle Drawing Module