Note -8: using turtle graphics rendering library

1. Form Functions

  • turtle.setup(width,height,startx,starty)
  • Action: setting the size and position of the form
    • width: the width of the window, if the value is an integer indicating a pixel value; if the value is a decimal, the ratio of the width of the window of the screen representation.
    • height: the height of the window, if the value is an integer indicating a pixel value; if the value is a decimal, the ratio of the height of the window with the screen representation.
    • startx: pixel from the left side and the left side of the screen window, if the value is None, the horizontal center of the screen window.
    • starty: a pixel from the top window and the top of the screen, if the value is None, vertical center of the screen window.

2. Brush a state function

pendown () Issued brush
penup () Lift the brush, and pendown () paired
pensize(width) Set the brush line thickness specified size
color() Set the color brush
begin_fill() Before filling graphics, call the method
end_fill() Filling end graphics
filling() Back-filled state, True filled, False unfilled
clear() Clear the current window, but does not change the current position of the brush
reset() Clear the current window, and reset the state to the default location value
screensize() Set the brush length and width
hideturtle() turtle shape hidden brush
showturtle() Display turtle shaped brush
isvisible() If the turtle is visible, True is returned

 

3. Brush motion functions

forward() Forward a specified distance along the current direction
backward() Back specified distance along a direction opposite to the current
right(angle) Rotate right angle angle
left(angle) Left angle of rotation angle
goto (x, y) Moved to the absolute coordinates (x, y) at
setx () The x-axis current to a specific location
sets () The current y axis to a specified position
setheading(angle) Set current orientation angle to an angle
home() Sets the current pen position as the origin toward the East.
circle(radius,e) Draw a circle radius r and angle specified or arc e
dot(r,color) Draw a dot designated color and colors of radius r
undo() Undo the last step action brush
speed() Brush set drawing speed parameter between 0-10

 

4. Case

  • Case-1: draw the profile library using turtle color is red (red), the fill color to pink (Pink) heart-shaped pattern
from Turtle Import * 

Color ( ' Red ' , ' Pink ' )          # Set brush color 
begin_fill ()                  # front fill pattern, the method is called 
left (135)                     # rotation of 135 degrees to the left 
FD (100)                       # control proceeds to the current brush forward direction 100 
right (180)                    # rotated clockwise 180 degrees 
circle (50, -180)               # draw a specified radius of a circle or an arc angle of 50 and 180 
left (90)                      # rotating 90 degrees left 
circle (50, -180 )               # draw a specified radius of the circle or arc of an angle of 180 and 50
right (180)                    # rotated clockwise 180 degrees 
FD (100)                       # control proceeds brush 100 to the current traveling direction 
end_fill ()                    # fill complete graphical 
hideturtle ()                  # hidden brush shape 
done ()
  • Case-2: Use turtle red five-pointed star drawn graphics library
from Turtle Import * 

Setup ( 400, 400)             # size of the form * 400 400 
penUp ()                    # picked his brushes 
GOTO (-100,50)              # move to the absolute coordinates (-100,50) 
penDown ()                  # falling brush, after moving brush shape drawn 
Color ( ' Red ' )               
begin_fill ()               # fill pattern 
for I in Range (. 5 ): 
    forward ( 200)           # advancing direction 200 along this 
    right (144)             # rotates clockwise angle 144
end_fill ()                 # fill graphics end 
hideturtle ()               
DONE ()
  • Case-3: Use turtle draw a square helix library
from Turtle Import * 

n- = 10
 for I in Range (1,10 ):                 
     for J in [90,180, -90 , 0]: 
        Seth (J)       # change brushed direction 
        FD (n-)         # current direction of forward travel of n + 5 
        + =. 5 n- 
hideturtle () 
DONE ()
  • Case-4: Use turtle silhouette city library to draw simple graphics
from Turtle Import * 

Setup ( 800,300)                         # set the size and position of the main form 
penUp ()                                # lifting the brush, then, without moving the brush to draw shapes 
FD (-350 ) 
penDown ()                              # falling brush, after moving the brush is drawn shape 
DEF the DrawLine (size):
     for angle in [0,90, -90, -90,90 ]: 
        left (angle)                    # leftward rotation angle angle 
        FD (size)    
 for I in [20,30,40,50, 40,30,20 ]: 
    the DrawLine (I) 
hideturtle () 
DONE ()
  • Case-5: Draw concentric circles using turtle graphics library
from Turtle Import * DEF DrawCctCircle (n-): 
    penUp ()                             # lifting the brush, then, without moving the brush to draw shapes 
    GOTO (0, -n)                          # moves to the absolute coordinates (0, -n) at 
    penDown ()                           # falling Brush after moving the brush to draw shapes 
    circle (n)                           # draw a circle n specified radius for I in Range (20,100,20 ): 
    DrawCctCircle (I) 
hideturtle () 
DONE ()


Guess you like

Origin www.cnblogs.com/Cyzhouke/p/11446032.html