Python turtle drawing example tutorial
- 2020-04-02 13:52:45
- OfStack
This paper introduces the use of python turtle module, i.e. turtle drawing, in the form of an example.
About the python turtle module:
A simple drawing tool introduced in python2.6 called Turtle Graphics
First we need to import turtle , as follows:
from turtle import * # will turtle All method imports in
2. Turtle drawing attributes:
(1)
location
(2)
The direction of
(3)
The brush
(
Brush properties, color, line width
)
3. There are many commands for manipulating turtle drawing, which can be divided into two types: one is motion command, the other is brush control command
(1) Movement command:
forward(degree) # Forward travel degree On behalf of the distance
backward(degree) # Backward travel degree On behalf of the 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 coordinate of x,y The location of the
stamp() # Copy the current figure
speed(speed) # The speed range the brush draws [0,10] The integer
(2) Brush control command:
down() # Draw as you move , It is also drawn by default
up() # Move without drawing a graph
pensize(width) # The width at which the graph is drawn
color(colorstring) # Color when drawing a figure
fillcolor(colorstring) # Draws the fill color of the graph
fill(Ture)
fill(false)
Let's take a look at an example of turtle:
(I) draw a square:
import turtle
import time
# Defines the color of the brush when drawn
turtle.color("purple")
# Defines the width of the brush line when drawn
turtle.size(5)
# Defines the speed of drawing
turtle.speed(10)
# In order to 0,0 Draws the starting point
turtle.goto(0,0)
# Draw the four sides of the square
for i in range(4):
turtle.forward(100)
turtle.right(90)
# Move the brush to a point (-150,-120) Without drawing
turtle.up()
turtle.goto(-150,-120)
# Again, define the brush color
turtle.color("red")
# in (-150,-120) Point to print "Done"
turtle.write("Done")
time.sleep(3)
(2) drawing a pentagram:
import turtle
import time
turtle.color("purple")
turtle.pensize(5)
turtle.goto(0,0)
turtle.speed(10)
for i in range(6):
turtle.forward(100)
turtle.right(144)
turtle.up()
turtle.forward(100)
turtle.goto(-150,-120)
turtle.color("red")
turtle.write("Done")
time.sleep(3)
Here are two simple examples, you can according to the above ideas and methods to further expand, draw some more complex graphics.