Drawing with the Turtle module in python
Introduction: Turtle is a simple drawing tool. It provides a turtle that you can interpret as a robot, only to understand the limited instructions.
1. Write the following line in the header of the file, which allows us to insert Chinese in the statement
#-*-Coding:utf-8-*-
2. Import Turtle Library with import turtle
3. The origin of the drawing window (0,0) is in the middle. By default, turtles move to the right.
4. There are many commands for manipulating turtle drawings, which can be divided into two types: one for motion commands and one for brush control commands
(1) Motion command:
Forward (d) |
Move forward distance d to represent distance |
Backward (d) |
Move Backward distance D represents distance |
Right (degree) |
How many degrees to the right? |
Left (degree) |
How many degrees left to rotate |
Goto (X,y) |
Move the brush to a position of coordinates (X,Y) |
Stamp () |
Draw the current graphic |
Speed (speed) |
Speed range [0,10] integer drawn by the brush |
(2) Brush control command:
Down () |
Draw the drawing when the brush falls and moves |
Up () |
The brush is lifted and the graphic is not drawn when moved |
Setheading (degree) |
Turtle headed, degree, representing angle. |
Reset () |
Restore all settings |
Pensize (width) |
The width of the brush |
PenColor (colorstring) |
Color of brushes |
FillColor (colorstring) |
Draw the fill color of a graphic |
Fill (ture) |
|
Fill (False) |
|
Circle (radius, extent) |
Draw a circle, where radius is radius, extent is a degree, for example, if extent is 180, draw a semicircle; to draw a circle, you don't have to write the second argument |
5. Several examples
1 draw a triangle with a side length of 60
#-*-Coding:utf-8-*-
Importturtle
A=60
Turtle.forward (a)
Turtle.left (120)
Turtle.forward (a)
Turtle.left (120)
Turtle.forward (a)
Turtle.left (120)
2 draw a square with a side length of 60 and fill it with red, with a blue border
#-*-Coding:utf-8-*-
Importturtle
Turtle.reset ()
A= 60
Turtle.fillcolor ("Red")
Turtle.pencolor ("Blue")
Turtle.pensize (10)
Turtle.fill (True)
Turtle.left (90)
Turtle.forward (a)
Turtle.left (90)
Turtle.forward (a)
Turtle.left (90)
Turtle.forward (a)
Turtle.left (90)
Turtle.forward (a)
Turtle.fill (False)
6. Practice:
1) Draw a Pentagon
2) Draw a hexagonal
3 any input a positive integer m (>=3), draw a polygon (M edge)
4 Draw a pentagram, as shown below, note that the fill is red
5 Draw a Chinese chess board, as shown in the following picture, where the Chinese characters do not have to display:
6 Draw the Olympic five-ring map, of which five colors are blue, black, red, yellow and green respectively. Note Adjust the size and position of the circle according to the actual effect.