Example: Draw a Python
#Turtle: Drawing Library (Turtle Library)ImportTurtleturtle.setup (650,350,200,200) Turtle.penup () turtle.fd (-250) Turtle.pendown () turtle.pensize (25) Turtle.pencolor ("Purple") Turtle.seth (-40) forIinchRange (4): Turtle.circle (40,80) turtle.circle (-40,80) turtle.circle (40,80/2) TURTLE.FD (40) turtle.circle (16,180) TURTLE.FD (40*2/3) Turtle.done ()
Turtle Library: entry-level graphic drawing library, standard library
Python implementation of turtle drawing system
A turtle on the canvas, the canvas unit is the pixel.
The position of the drawing form relative to the screen:
Turtle.setup (Width,height,startx,starty) #绘图窗体的大小和位置, not required
Turtle.setup (width,height) #绘图窗体默认在屏幕中间
Coordinate system:
Space Coordinate system:
Absolute Coordinates: The initial position is in the center of the canvas
Set Turtle crawling Destination: Turtle.goto (100,100)
Turtle Coordinates: for the turtle's own perspective
Change the direction of turtle crawling:
Turtle.circle (R,angle)
TURTLE.BK (d) #后退d
TURTLE.FD (d) #前进d
Angular coordinate system:
Changing the turtle's travel angle
Absolute angle: The x-axis relative to the canvas
Turtle.seth (45)
Turtle angle: Relative to the turtle's current orientation
Turtle.left (angle)
Turtle.right (angle)
Color System
Turtle is an RGB color system (with small values by default)
Turtle.colormode (mode) #mode = 1.0 decimal value, mode=255 integer value
Turtle.colormode (1.0) Turtle.pencolor (1,1,0.4)
analysis of grammatical elements of turtle library
Import
The From library import function (*) #不用在前面加库名, but causes a function name conflict
Import library as library's nickname #重新命名为简单的库名, commonly used
Brush control functions
After the brush operation has been effective, generally paired appear turtle.penup () Turtle.pendown ()
Brush size turtle.pensize (width) = turtle,width (width)
Brush color Turtle.pencolor (color) #color为rgb或字符串, RGB tuple values:
(1,0.1,0.3) ("White") ((1,3,233))
Motion control function: Make turtle climb up-draw line
Motion path functions (lines and curves)
Turtle.forward (d) = TURTLE.FD (d) #向前行进, D is backward when negative
Turtle.circle (R,extent=none) #曲线半径, the center of the turtle to the left of R, the default to walk the entire circle
Direction control function: Absolute angle & relative angle
Just change the turtle to turn!!
Absolute angle: Relative to the x-axis in the absolute coordinate system
turtle.setheading (angle) = Turtle.seth (angle)
Relative angle: Relative to the turtle's current orientation
Turtle.left (angle)
Turtle.right (angle)
Python Course design Notes (iii) Turtle drawing Library (Turtle Library)