[Python module] graphical programming module - turtle

Turtle, also known as the turtle renderer, is a built-in graphical module of Python. It uses tkinter to implement the basic graphical interface, so the currently used Python environment needs to support tkinter.

Turtle provides the basic components of turtle drawing in two forms of object-oriented and process-oriented. Using it can easily realize the drawing of graphics. It has a good combination with various 3D software, powerful functions, and easy to use. It has also become the choice of Python interest programming.

The principle of graphics drawing is also very simple, the drawn graphics are represented by the trajectory left by the little turtle moving on the canvas. Thus, using Turtle Drawing allows you to write programs that repeatedly perform simple actions to draw intricate and complex shapes.

Commonly used APIs of the Turtle module are as follows:

If we can master the meaning and use of these APIs, we can basically draw all kinds of things like a powerful and unconstrained style.

For example, draw a graphic that looks like a virus:

# 颜色设置:参数1为画笔颜色,参数2为填充色
turtle.color('red', 'blue')
# 开始填充
turtle.begin_fill()
# 循环的前进200,再左转170,如果距画笔位置<1,则结束循环
while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos()) < 1:
        break
# 结束填充
turtle.end_fill()
turtle.done()

After executing the above code, the final graphics effect is as follows:

To draw a simpler Olympic rings, the idea is to draw five circles repeatedly. Of course, you must first write a method to repeatedly draw a circle, as follows:

def draw_circle(x, y, c):
    # 抬起画笔
    turtle.pu()
    # 画圆的开始位置
    turtle.goto(x, y)
    # 放下画笔
    turtle.pd()
    turtle.color(c)
    turtle.circle(40, 360)

Then, just set different center coordinates and ring colors, as follows:

# 设置画笔尺寸
turtle.pensize(3)
# 画五个圆环
draw_circle(0, 0, 'blue')
draw_circle(60, 0, 'black')
draw_circle(120, 0, 'red')
draw_circle(90, -30, 'green')
draw_circle(30, -30, 'yellow')
# 结束
turtle.done()

After executing the above code, the final graphics effect is as follows:

Now you should be able to appreciate the meaning of hobby programming. As long as you control the walking trajectory of "this little turtle", you can draw any graphics, and this kind of process-oriented programming is also simple and easy to understand.

Here is a slightly more complicated one. It is not difficult to draw the pattern of the national flag. It is to draw the red background area, and five yellow five-pointed stars will do.

Import dependent modules:

from turtle import *
import time

The first step is to draw the red background area

def draw_bg():
    speed(10)  # 控制画笔速度
    up()  # 抬笔
    goto(-200, 200)  # 跳到指定位置
    down()  # 落笔
    color("red", "red")  # 设置线条色和填充色
    begin_fill()
    forward(480)
    right(90)
    forward(320)
    left(90)
    backward(480)
    right(90)
    backward(320)
    end_fill()

The second step is to draw a big five-pointed star

def draw_big_star():
    up()
    forward(64)
    left(90)
    forward(32)
    down()
    # 画大五角星
    a = 96
    color("yellow", "yellow")
    begin_fill()
    for i in range(1, 6):
        forward(a)
        right(144)
        speed(2)
    end_fill()

The third step is to draw the first small five-pointed star

def draw_first_small_star():
    up()
    forward(96)
    left(53)
    forward(36)
    # 第一个小五角星
    a = 32
    begin_fill()
    for i in range(1, 6):
        forward(a)
        right(144)
        speed(5)
    end_fill()

The fourth step, draw the second small five-pointed star

def draw_second_small_star():
    up()
    right(80)
    forward(42)
    left(55)

    # 第二个小五角星
    a = 32
    begin_fill()
    for i in range(1, 6):
        forward(a)
        right(144)
        speed(5)
    end_fill()

Step five, draw the third small five-pointed star

def draw_third_small_star():
    up()
    right(96)
    forward(32)

    # 第三个小五角星
    a = 32
    begin_fill()
    for i in range(1, 6):
        forward(a)
        right(144)
        speed(5)
    end_fill()

The sixth step, draw the fourth small five-pointed star

def draw_four_small_star():
    up()
    right(54)
    forward(45)
    right(30)

    # 最后一个小五角星
    a = 32
    begin_fill()
    for i in range(1, 6):
        forward(a)
        right(144)
        speed(2)
    end_fill()

Finally, execute the test

if __name__ == '__main__':
    draw_bg()
    draw_big_star()
    draw_first_small_star()
    draw_second_small_star()
    draw_third_small_star()
    draw_four_small_star()
    ht()
    time.sleep(10)

The final effect drawn is as follows:

Of course, drawing all kinds of interesting cartoon characters is not a problem, such as Doraemon, Peppa Pig, Pikachu and so on.

Doraemon----> as follows:

Pikachu ----> as follows:

If you need these codes, they will be uploaded to my Github later~

​​​​​​​​​​​​References :

[Life is short, learn Python! The Python module series will be continuously updated and documented...]

Guess you like

Origin blog.csdn.net/qq_29119581/article/details/128088094