Python plotting library Turtle
Turtle Introduction
Turtle is embedded Python line is drawn, circular and other shapes (including text) graphics module.
A Turtle is actually an object, when importing Turtle module, you create an object, then you can call the various methods Turtle objects perform different operations.
When creating a Turtle object whose position is set at (0,0) - the center of the window, and its direction is set to the right. Turtle module pen to draw graphics, just like in real drawing paper writing. When moving the pen when it will draw a line by pressing the direction of movement.
Detailed map coordinates sea turtle
FIG turtle picture using positioning coordinates, (0, 0) coordinate system at an intermediate point of the drawing. Other coordinate with us in high school math is the same, divided into four quadrants.
As shown below:
Turtle basis
1. Canvas
Setting the background color canvas size
method 1:
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
Parameters: width of the canvas, respectively (in pixels), high background color.
Method 2:
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; when a decimal, the proportion of a computer screen showing occupy, (startx, starty): the position of the vertex coordinates of the upper left corner of the rectangular window, if it is empty, it is located in the center of the screen window
2. Brush
Brush Set:
- Pen Width turtle.pensize (int) is an integer parameter
- Pen color turtle.pencolor ( "black") parameter may be a string, such as "green", "red", may be RGB 3-tuple
- Brush speed turtle.speed (speed) setting the moving speed of the brush, brushed speed range [0,10] integer, the larger the number, the faster.
3. drawing commands
Turtle drawing command can be divided into three types: one for the brush movement command A control command for the pen, there is a global control command.
Brush movement command:
command |
Explanation |
turtle.forward(distance)/fd() |
Moving distance to the current pixel length direction of the brush |
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() |
Gv move, no draw graphics for a place to start a new drawing |
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 |
setx () |
The x-axis current to a specific location |
sets () |
The current y axis to a specified position |
setheading(angle)/seth(angle) |
Set current orientation angle to an angle |
home() |
Sets the current pen position as the origin toward the East. |
dot(r) |
Draw a specified diameter and color dot |
Brush Control command:
command |
Explanation |
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() |
turtle shape hidden brush |
turtle.showturtle() |
Display turtle shaped brush |
turtle.begin_poly() |
Recording position of the brush |
turtle.get_poly() |
Get the brush position |
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, namely font name, size and type; font is optional, font parameter is optional |
turtle.mainloop () |
After the drawing is complete without closing the window |
Case: five-pointed star
Pentagram painting:
A five-pointed star each inner angle 36 [deg.] , If the starting location of sea turtles first draw a horizontal line, then to draw a corner of the first turn may be used when needed turtle.right () clockwise, turtle.left () counterclockwise rotating brushes
If a clockwise rotation is 180 ° -36 ° = 144 °
If counterclockwise rotation of the 180 ° + 36 ° = 216 °
That is five times five corners to cycle
import turtle
turtle.pensize (3) # brush size turtle.pencolor ( "yellow") # pen color turtle.fillcolor ( "red") # Fill Color turtle.begin_fill () # begins to fill for _ in range (5): # pentagram cycle five times Side length turtle.forward (200) # 200 pixels pentagram turtle.left (144) # brush rotation angle turtle.end_fill () # filling is completed
turtle.mainloop () # without closing the window
|
Case: Love
Description: Heart painting
def draw_heart(r, angle=45):
|
完整代码查看附件
import turtle
turtle.setup(1200, 600) # 窗口大小
# 画心
turtle.color("black", "red") turtle.pensize(2) turtle.speed(10) turtle.up() # 提起画笔 turtle.goto(0, 50)
turtle.down()
def draw_heart(r, angle=45): """ :param r: 桃心圆的半径 :param angle: 起始画笔角度 :return: 返回桃心底部尖点坐标 """ turtle.begin_fill() # 开始填充 turtle.seth(0) turtle.seth(angle) # 设置画笔方向 turtle.circle(-r, 180) # 逆时针画半圆 turtle.fd(2 * r) # 向前移动 turtle.begin_poly() # 记录画笔的位置 x, y = turtle.get_poly()[0] # 获取画笔的位置 turtle.right(90) # 逆时针旋转画笔 turtle.fd(2 * r) # 向前移动 turtle.circle(-r, 180) turtle.end_fill() # 填充结束 return x, y
# 画桃心 x_y = [] start_x = 0 for _ in range(4): turtle.goto(start_x, 50) turtle.down() # 放下笔 x_y.append(draw_heart(8)) start_x += 50 turtle.up() # 提起画笔
# 画叶子 def draw_flower(x, y): """ 画桃心下的花 :param x: 桃心尖底的x轴 :param y: 桃心尖底的y轴 :return: """ turtle.up() turtle.goto(x, y) turtle.seth(0) # 恢复画笔向右 turtle.seth(-90) turtle.down() turtle.fd(60) turtle.goto(x, y) turtle.right(60) turtle.fd(20) turtle.left(90) turtle.fd(10) turtle.left(120) turtle.fd(20) turtle.up() turtle.goto(x, y) turtle.seth(0) # 恢复画笔向右 turtle.seth(-115) turtle.down() turtle.fd(25) turtle.up() turtle.goto(x, y) turtle.seth(0) # 恢复画笔向右 turtle.seth(-10) turtle.down() turtle.fd(20) turtle.right(90) turtle.fd(8) turtle.right(120) turtle.fd(20) turtle.goto(x, y) turtle.seth(-60) turtle.fd(20) turtle.up()
for x, y in x_y: draw_flower(x, y)
# 画小人物 turtle.up() turtle.goto(-200, -50) turtle.down() turtle.seth(0) # 恢复画笔向右,好控制方向 turtle.seth(45) turtle.fd(40) turtle.begin_poly() # 记录画笔的位置,脚叉的位置 x, y = turtle.get_poly()[0] # 获取画笔的位置 turtle.right(90) turtle.fd(35) turtle.goto(x, y) turtle.left(135) turtle.fd(90) turtle.seth(0) turtle.circle(30) # 画圆 turtle.goto(x, y + 60) # 手的位置 turtle.fd(40) turtle.circle(30, 70) # 画圆,拿花手的弧度 turtle.seth(0) turtle.up() turtle.goto(x, y + 50) # 手的位置 turtle.down() turtle.fd(50) turtle.begin_poly() # 记录手的位置等下画花需要用 f_x, f_y = turtle.get_poly()[0]
# 眼睛 turtle.up() turtle.goto(x - 15, y + 120) # 眼睛的位置 turtle.down() turtle.pensize(4) turtle.seth(45) turtle.circle(-10, 70) # 画圆弧,拿花手的弧度 turtle.dot(10) # 眼睛画圆 turtle.up()
turtle.goto(x + 10, y + 120) # 眼睛的位置 turtle.down() turtle.pensize(4) turtle.seth(45) turtle.circle(-10, 70) # 画圆弧,拿花手的弧度 turtle.dot(10) # 眼睛 画圆 turtle.up()
# 嘴巴 turtle.goto(x, y + 105) # 眼睛的位置 turtle.down() turtle.circle(10, 70) # 画圆弧,拿花手的弧度
# 画里拿的花叶子部分 turtle.pensize(3) # 将画笔大小 turtle.up() turtle.goto(f_x, f_y) # 手的位置 turtle.left(20) turtle.down() turtle.fd(60) turtle.pensize(2) # 将画笔调小 turtle.begin_poly() # 记录手的位置等下画花需要用 x, y = turtle.get_poly()[0] turtle.backward(80) turtle.up() turtle.goto(x, y) turtle.down() turtle.right(90) turtle.fd(20) turtle.right(90) turtle.fd(8) turtle.right(120) turtle.fd(25) turtle.right(200) turtle.fd(25) turtle.up() turtle.goto(x, y) turtle.down() turtle.right(60) turtle.fd(25) turtle.goto(x, y) turtle.right(40) turtle.fd(25) turtle.left(90) turtle.fd(8) turtle.left(110) turtle.fd(25)
turtle.goto(f_x + 34, f_y + 75) draw_heart(8, angle=20) # 手里拿的桃心
# 写文字 turtle.up() turtle.goto(150, 200) turtle.pencolor("PINK") # 画笔颜色 turtle.write("TO: 所有同学", font=("方正舒体", 30, 'normal')) turtle.goto(180,140) turtle.write("送给棒棒的你", font=("方正舒体", 30, 'normal'))
turtle.hideturtle() turtle.mainloop()
|
喜欢Python的同学扫码加我哟!!!!!