Skip to content

实例:turtle的使用-gua

gua.py

python
import turtle

SEMICIRCLE_CLOCKWIZE = -180
CIRCLE_CLOCKWIZE = -360


def draw_inner():
    turtle.pencolor("white")
    turtle.color("white",  "white")
    turtle.begin_fill()
    turtle.circle(100, SEMICIRCLE_CLOCKWIZE)
    turtle.circle(50, SEMICIRCLE_CLOCKWIZE)
    turtle.circle(-50, SEMICIRCLE_CLOCKWIZE)
    turtle.end_fill()
    turtle.circle(-100, SEMICIRCLE_CLOCKWIZE)

    move(0, 140)
    turtle.color("white",  "black")
    turtle.pencolor("black")
    turtle.begin_fill()
    turtle.circle(15, CIRCLE_CLOCKWIZE)
    turtle.end_fill()

    move(0, 30)
    turtle.color("black",  "white")
    turtle.pencolor("white")
    turtle.begin_fill()
    turtle.circle(15, CIRCLE_CLOCKWIZE)
    turtle.end_fill()

def mdd(point, gua1, gua2):
    move(*point)
    turtle.right(225)
    draw_gua(gua1)
    move_point(45, gua2)

def draw_guas():
    turtle.pensize(2)
    turtle.right(225)
    mdd((-100,100),[1,0,0],[0,1,1])
    mdd((0,200),[1,0,1],[0,0,0])
    mdd((100,100),[1,1,0],[1,1,1])
    mdd((0,0),[0,1,0],[0,0,1])

def move(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def move_forward(dis):
    turtle.penup()
    turtle.forward(dis)
    turtle.pendown()

def move_backward(dis):
    turtle.penup()
    turtle.backward(dis)
    turtle.pendown()

def move_point(benchmark, gua):
    turtle.right(45)
    move_forward(benchmark)
    turtle.left(45)
    move_forward(benchmark)
    turtle.right(225)
    draw_gua(gua)

def draw_gua(gua):
    origin_pos = turtle.pos()
    length, hl, m = 32, 16, 10

    move_forward(hl)
    turtle.right(90)
    move_forward(m)

    for g in gua:
        move_forward(m)
        turtle.right(90)
        if g:
            turtle.forward(length)
        else:
            turtle.forward(8)
            move_forward(hl)
            turtle.forward(8)
        move_backward(length)
        turtle.left(90)

    turtle.right(90)
    move(*origin_pos)

def main():
    turtle.showturtle()
    # turtle.speed(0)
    turtle.bgcolor('black')
    draw_inner()
    draw_guas()
    turtle.hideturtle()
    input()

if __name__ == '__main__':
    main()

Released under the MIT License.