Turtle graphics (trutle) program is to introduce children to a popular way. It is part of Wally Feurzig and Seymour Papert in 1966 developed the Logo programming language. Logo language is an early programming language, but also a very close and natural language programming language to learn programming by "drawing" a way for beginners especially for teaching young children entertaining way.
Imagine a small turtle in a horizontal axis x, y is the vertical axis of the coordinate system. In the coordinate origin (0,0) starts, it moves in the plane coordinate system in accordance with an instruction input set of codes. So it crawling on the path to draw a variety of cool, fancy graphics.
First explain the basics of turtle drawing:
1, canvas (canvas)
Turtle canvas is a drawing area for us to expand, we can set its initial position and size
1.1 Setting the canvas size
turtle.screensize (canvwidth = None, canvheight = None, bg = None)
parameters are canvas width (in pixels), high, the background color
Such as:
turtle.screensize (1080, 800, "Green")
turtle.screensize () # returns the default size (400, 300)
turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
Parameters:
width, height: width and height of the input integer, represents a pixel; is a decimal, the proportion of a computer screen showing occupies
(startx, starty): This indicates the coordinates of the upper left corner vertex of a rectangular window, if it is empty, window centered on screen
Such as:
turtle.setup(width=0.8, height=0.8)
turtle.setup(width=800, height=800, startx=100, starty=100)
2, the brush
2.1 state brush
On the canvas, there is a default canvas coordinate origin of the center axis, there is an x-axis positive direction face on the small turtles coordinate origin used herein, the term we describe two small turtle: coordinate origin (position), facing the positive x-axis direction (direction), turtle drawing, a state is small turtles use position (pen) is described
2.2 properties of a pen
Brushes (brush attributes, color, image line width)
1) turtle.pensize (): Set the pen width;
2) turtle.pencolor (); none is passed, it returns the current pen color, incoming pen color parameters, can be a string, such as "green", "red" It may be a RGB 3-tuple,
>>> pencolor('brown')
>>> tup = (0.2, 0.8, 0.55)
>>> pencolor(tup)
>>> pencolor()
'#33cc8c'
3) turtle.speed (speed): setting the moving speed of the brush, brushed speed range [0,10] integer, the larger the number, the faster
2.3 drawing commands
Actuating turtle drawing has many commands that can be divided into three types: one for the movement command A control command for the pen, there is a global control command.
(1) brush movement command:
command | Explanation |
---|---|
turtle.forward(distance) | Pixel distance moved lengthwise direction of the current pen |
turtle.backward(distance) | Pixel distance moved in the opposite direction to the current length of the brush |
turtle.right(degree) | Moving clockwise degree ° |
turtle.left(degree) | Moves counterclockwise degree ° |
turtle.pendown() | Graphics rendering, the default is also moved to draw |
turtle.goto(x,y) | The pen is moved to the coordinate x, y position |
turtle.penup() | Not draw graphics, Gv is moved, for use when a drawn from other places |
turtle.speed(speed) | Brushed speed range [0,10] Integer |
turtle.circle() | Circle, the radius is positive (negative), represents the center (on the right) to draw a circle on the left side of the brush |
(2) Brush control command:
command | Explanation |
---|---|
turtle.pensize(width) | When the width of the drawing pattern |
turtle.pencolor () | Brush Color |
turtle.fillcolor(colorstring) | Draw fill color graphics |
turtle.color(color1, color2) | And set pencolor = color1, fillcolor = color2 |
turtle.filling() | Returns whether the current state of the filling |
turtle.begin_fill() | Ready to start filling graphics |
turtle.end_fill() | The filling is completed; |
turtle.hideturtle() | Hide arrows show; |
turtle.showturtle() | And hideturtle () function corresponding to |
(3) global control command:
command | Explanation |
---|---|
turtle.clear() | Empty turtle window, but the turtle's location and status will not change |
turtle.reset() | Clear window, reset state as a starting state turtle |
turtle.undo() | Undo the last turtle action |
turtle.isvisible() | Returns the current turtle is visible |
stamp() | Copy the current graphics |
turtle.write(s[,font=("font-name",font_size,"font_type")]) | Write text, s text content, font is a font parameters, which are the font name, size and type; font is optional, font parameter is optional |
3, command Detailed
3.1 turtle.circle (radius, extent = None , steps = None)
Description: a given radius circle
parameters:
RADIUS (radius); radius of positive (negative), the center circle represents the left side of the brush (right)
extents ( radian) (optional);
Steps (optional) (do radius circle radius of an inscribed regular polygon, the polygon number of sides steps)
Example:
Circle (50) # full circle;
Circle (50, Steps =. 3) # triangles;
Circle (120, 180 [) # semicircle
4, for example drawing
4.1 rectangle:
#导入turtle包的所有内容:
from turtle import *#设置笔刷宽度:
width(9)
#前进:
forward(300)
#右转90度:
right(90)
#笔刷颜色:
pencolor('red')
forward(200)
right(90)
pencolor('green')
forward(600)
right(90)
pencolor('blue')
forward(200)
right(90)
pencolor('black')
forward(300)
right(90)
#调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()
4.2 three squares:
import turtle
turtle.screensize(canvwidth=None, canvheight=None, bg="blue")
from turtle import *
width(10)
def drawSquare(sides,length):
angle = 360/sides
for again in range(sides):
turtle.forward(length)
turtle.right(angle)
def moveTurtle(x,y):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
drawSquare(4,160)
moveTurtle(200,200)
drawSquare(4,160)
moveTurtle(-200,200)
drawSquare(4,160)
turtle.done()
4.3 sine wave
import turtle
import math
# turtleSet function makes the code a bit more compact
def turtleSet(name, shape, speed, color, pen, pos_x, pos_y):
name.ht()
name.up()
name.shape(shape)
name.speed(speed)
name.color(color)
name.pensize(pen)
name.setposition(pos_x, pos_y)
name.down()
name.st()
wn = turtle.Screen()
background = turtle.Turtle()
background.speed(0)
background.up()
background.goto(325, 325)
background.down()
background.right(90)
background.fillcolor("red")
background.begin_fill()
for i in range(4): # Draw a 650 x 650 square
background.forward(650)
background.right(90)
background.end_fill()
height = 200 #amplitude
period = 25 # Period, but without any specific unit.
circle = turtle.Turtle()
turtleSet(circle, 'circle', 10, "green", 2, height, 0)
circle.left(90)
sine = turtle.Turtle()
turtleSet(sine, 'circle', 10, "black", 2, -325, 0)
sine.st()
sine.left(45)
line = turtle.Turtle()
turtleSet(line, 'circle', 10, "blue", 2, -286, 0)
line.left(90)
time = turtle.Turtle()
turtleSet(time, 'arrow', 10, "yellow", 2, -325, 0)
time.right(0)
for i in range(650):
x = height*math.cos(i/period)
y = height*math.sin(i/period)
sine.goto(i-325, y)
circle.goto(x, y)
line.goto(-286, y)
time.goto(i-325, 0)
wn.exitonclick()
4.3 Nautilus
from turtle import *
speed(0)
bgcolor("white")
pencolor("MediumAquamarine")
h = 10
for j in range(360):
for i in range(4):
forward(h)
right(90)
right(3)
h = h*1.01
turtle.done()