This article introduces the Python turtle module, which is the use of the turtle drawing, as an example, and it will be of some value to the friends who need to do graphic programming.
Introduction to the Python Turtle module:
A simple drawing tool introduced in the python2.6 version, called the Turtle plot (Turtle graphics)
1. Using Turtle drawing First we need to import turtle, as shown below:
From Turtle Import * #将turtle中的所有方法导入
2. Turtle Plot Properties:
(1) Location
(2) direction
(3) Brushes ( properties of the brush, color, width of the drawing line )
3. Manipulating turtle Drawings There are many commands that can be divided into two types: one for motion commands and one for brush control commands
(1) Motion Commands:
Forward (degree) #向前移动距离degree代表距离 Backward (degree) #向后移动距离degree代表距离 right (degree) # Move to the right how many degrees left (degree) #向左移动多少度 Goto (x, y) #将画笔移动到坐标为x, y position stamp () #复制当前图形 Speed # The speed range [0,10] integer drawn by the brush
(2) Brush control Commands:
Down () #移动时绘制图形, the default is to draw up () #移动时不绘制图形 pensize (width) #绘制图形时的宽度 color (colorstring) #绘制图形时的颜色 FillColor (colorstring) # Draw a drawing's fill color fill (Ture) Fill (false)
4. Introduction to turtle Many of the following we look at an example:
(a) Draw a square:
Import Turtle Import time# defines the color of the brush when drawn Turtle.color ("purple") #定义绘制时画笔的线条的宽度 turtle.size (5) #定义绘图的速度 Turtle.speed (Ten) #以0 , 0 is drawn for the starting point Turtle.goto (0,0) #绘出正方形的四条边 for I in range (4): Turtle.forward (+) turtle.right (+) #画笔移动到点 ( -150,- 120) when not drawing Turtle.up () Turtle.goto ( -150,-120) #再次定义画笔颜色 turtle.color ("Red") #在 ( -150,-120) dot print "Done" turtle.write ("Done ") Time.sleep (3)
(ii) To draw a pentagram:
Import Turtleimport Timeturtle.color ("Purple") turtle.pensize (5) Turtle.goto (0,0) Turtle.speed (Ten) for I in range (6): Turtle.forward (+) turtle.right (144) Turtle.up () Turtle.forward (+) Turtle.goto ( -150,-120) Turtle.color ("Red") Turtle.write ("Done") Time.sleep (3)
Here are two simple examples, you can follow the above ideas and methods to further expand, to draw some more complex graphics.