This article introduces the python turtle module in the form of an example of how to use the turtle drawing, for the need for graphics programming friends believe that there will be a certain reference value.
Introduction to the python turtle module:
A simple drawing tool introduced in python 2.6 is called Turtle Graphics.
1. Using turtle drawing first we need to import turtle, as shown below:
from turtle import * #Import all methods from turtle
2. Turtle mapping properties:
(1)placement
(2)orientations
(3)brushes(Properties of the brush, color, width of the drawn line)
3. There are many commands to manipulate the turtle's drawing, which can be categorized into two kinds: one for themotion commandOne of them isBrush Control Commands
(1)Motion Command.
forward(degree) # Move forward the distance degree represents the distance backward(degree) # Move backward distance degree for distance right(degree) # How many degrees to the right? left(degree) # How many degrees to the left? goto(x,y) # Move the brush to the position with coordinates x,y stamp() #Copy the current graph speed(speed) # Brush drawing speed range [0,10] integers
(2)Brush Control Commands.
down() #Draws the graph when moving, also draws by default. up() #No graphics are drawn when moving pensize(width) # Width when drawing graphics color(colorstring) # Color when drawing graphics fillcolor(colorstring) # Drawing the fill color of a graphic fill(Ture) fill(false)
4. About turtle introduction many of the following we look at an example:
(i) Drawing squares:
import turtle import time # Define the color of the brush when drawing ("purple") # Define the width of the line of the brush when drawing. (5) # Define the speed of the drawing (10) # Plotting with 0,0 as a starting point (0,0) # Draw the four sides of a square for i in range(4): (100) (90) #Does not draw when the brush is moved to the point (-150,-120). () (-150,-120) # Define the brush color again ("red") # Prints "Done" at (-150,-120). ("Done") (3)
(ii) Draw the pentagram:
import turtle import time ("purple") (5) (0,0) (10) for i in range(6): (100) (144) () (100) (-150,-120) ("red") ("Done") (3)
Here are given two simple examples, you can further expand according to the above ideas and methods to draw some more complex graphics.