Article Directory
- 1 Example: Python drawing
- 2 Use of turtle library
- 2.1 turtle drawing form
- 2.2 turtle space coordinate system
- 2.3 turtle angular coordinate system
- 2.4 RGB color system
- 3 turtle program syntax element analysis
- 3.1 Library reference and import
- 3.2 Turtle pen control function
- 3.3 turtle motion control function
- 3.4 Turtle direction control function
- 3.5 Basic loop statement
- 4 Summary
1 Example: Python drawing
#PythonDraw.py
import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(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()
2 Use of turtle library
2.1 turtle drawing form
The canvas space of the turtle is shown below:
the position of the upper left corner of the drawing form represents the position of the form, and the length and width of the form represent the size of the form.
The size and position of the window can be drawn by the function setup ():
turtle.setup(width, height, startx, starty)
- The last two of the 4 parameters are optional
- setup () is not required
The setup () function is used as follows:
2.2 turtle space coordinate system
In the turtle absolute coordinate system, the right is the positive x direction, and the upward is the positive y direction.
The function goto () can directly move to the given coordinate position, the function is as follows:
turtle.goto(x, y)
example:
import turtle
turtle.goto( 100, 100)
turtle.goto( 100,-100)
turtle.goto(-100,-100)
turtle.goto(-100, 100)
turtle.goto(0,0)
The implementation is as follows:
circle (r, angle) draws a circle with the position at the left of the current point at the distance r as the center of the circle, and the angle is angle
fd (d), which represents the distance d in the current direction
2.3 turtle angular coordinate system
seth () function can change the direction of travel
turtle.seth (angle)
- angle is the absolute degree
- seth () only changes direction but does not travel
The left () and right functions respectively indicate a certain angle of rotation from left to right:
turtle.left (angle)
turtle.right (angle)
example:
import turtle
turtle.left(45)
turtle.fd(150)
turtle.right(135)
turtle.fd(300)
turtle.left(135)
turtle.fd(150)
carried out:
2.4 RGB color system
All things composed of three colors
- RGB refers to the color combination of three channels of red, blue and green
- Cover all colors that vision can perceive
- RGB each color value range 0-255 integer or 0-1 decimal
English name | RGB integer value | RGB decimal value | Chinese name |
---|---|---|---|
white | 255, 255, 255 | 1, 1, 1 | white |
yellow | 255, 255, 0 | 1, 1, 0 | yellow |
magenta | 255, 0, 255 | 1, 0, 1 | Magenta |
cyan | 0, 255, 255 | 0, 1, 1 | Blue |
blue | 0, 0, 255 | 0, 0, 1 | blue |
black | 0, 0, 0 | 0, 0, 0 | black |
seashell | 255, 245, 238 | 1, 0.96, 0.93 | Seashell |
gold | 255, 215, 0 | 1, 0.84, 0 | Golden |
pink | 255, 192, 203 | 1, 0.75, 0.80 | Pink |
brown | 165, 42, 42 | 0.65, 0.16, 0.16 | brown |
purple | 160, 32, 240 | 0.63, 0.13, 0.94 | purple |
tomato | 255, 99, 71 | 1, 0.39, 0.28 | Tomato color |
The default value is decimal, which can be switched to an integer value
turtle.colormode(mode)
- 1.0: RGB decimal value mode
- 255: RGB integer value mode
3 turtle program syntax element analysis
3.1 Library reference and import
Library references: ways to extend the functionality of Python programs
- Use import reserved words to complete, adopt <a>. <B> () coding style
import <library name>
<library name>. <Function name> (<function parameter>)
example:
But writing turtle many times is very tedious, there is another way to write it, it is simpler
- Use from and import reserved words together to complete
from <library name> import <function name>
from <library name> import *
<function name> (<function parameter>)
example:
Comparison of two writing methods:
Writing one :
import <library name>
<library name>. <Function name> (<function parameter>)
Writing two :
from <library name> import <function name>
from <library name> import *
<function name> (<function parameter>)
The first method will not cause the function duplicate name problem, the second method will appear
import more usage : alias library
- Use import and as reserved words to complete
import <library name> as <library alias>
<library alias>. <Function name> (<function parameter>)
Associate the called external library with a shorter and more suitable name
example:
3.2 Turtle pen control function
There are four penup (), pendown (), pensize (), pencolor () control functions of the pen.
- turtle.penup () alias turtle.pu ()
raises the pen - turtle.pendown () alias turtle.pd ()
drop pen - turtle.pensize (width) alias turtle.width (width)
pen width - turtle.pencolor (color) color is the color string or r, g, b value
pen color
pencolor (color) color can have three forms
- Color string: turtle.pencolor ("purple")
- Decimal value of RGB: turtle.pencolor (0.63, 0.13, 0.94)
- RGB tuple value: turtle.pencolor ((0.63,0.13,0.94))
3.3 turtle motion control function
There are two motion control functions, fd () and circle ()
- turtle.forward (d) alias turtle.fd (d)
travels forward, go straight, d is the travel distance, can be negative - turtle.circle (r, extent = None)
draws an arc of extent angle according to the radius r, the default center is at a distance of r from the left side of the turtle , extent is the drawing angle, and the default is a 360-degree circle
3.4 Turtle direction control function
There are three direction control functions, namely seth (), left () and right (), seth () changes the absolute angle, and left () and right () change the turtle angle
- turtle.setheading (angle) alias turtle.seth (angle) to
change the direction of travel, angle is the absolute angle of the direction of travel - turtle.left (angle) Turtle turns left
- turtle.right (angle) The turtle turns to the right
angle: the angle of rotation in the turtle's current direction of travel
3.5 Basic loop statement
Execute a set of statements in a loop a certain number of times
- for <variable> in range (<number>):
<statement to be executed in a loop>
<Variable> represents the count of each cycle, 0 to <number> -1
example:
>>> for i in range(5):
print("hello:",i)
hello: 0
hello: 1
hello: 2
hello: 3
hello: 4
range () function: generate a loop count sequence
- range (N): generates an integer sequence from 0 to N-1, a total of N
- range (M, N): generates an integer sequence from M to N-1, a total of NM
4 Summary
Use of turtle library
- Turtle drawing method of turtle library
- turtle.setup () adjusts the layout of the drawing form on the computer screen
- The space coordinate system with the center as the origin on the canvas: absolute coordinates & turtle coordinates
- An angle coordinate system with a space x axis of 0 degrees on the canvas: absolute angle & turtle angle
- RGB color system, integer value & decimal value, color mode switch
Turtle program syntax element analysis
- Library references: import, from ... import, import ... as ...
- penup () 、 pendown () 、 pensize () 、 pencolor ()
- fd()、circle()、seth()
- Loop statement: for and in, range () functions