用turtle库绘制小猪佩奇

**

小猪佩奇源码

**

import turtle as z


def nose():
    # 鼻子
    z.pu()
    z.goto(-100, 100)
    z.pd()
    z.seth(-30)
    z.begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            z.lt(3)  # 向左转3度
            z.fd(a)  # 向前走a的步长
        else:
            a = a - 0.08
            z.lt(3)
            z.fd(a)
            z.end_fill()
    z.pu()
    z.seth(90)
    z.fd(25)
    z.seth(0)
    z.fd(10)
    z.pd()
    z.pencolor(255, 155, 192)
    z.seth(10)
    z.begin_fill()
    z.circle(5)
    z.color(160, 82, 45)
    z.end_fill()

    z.pu()
    z.seth(0)
    z.fd(20)
    z.pd()
    z.pencolor(255, 155, 192)
    z.seth(10)
    z.begin_fill()
    z.circle(5)
    z.color(160, 82, 45)
    z.end_fill()


def head():
    # 头
    z.color((255, 155, 192), "pink")
    z.pu()
    z.seth(90)
    z.fd(41)
    z.seth(0)
    z.fd(0)
    z.pd()
    z.begin_fill()
    z.seth(180)
    z.circle(300, -30)
    z.circle(100, -60)
    z.circle(80, -100)
    z.circle(150, -20)
    z.circle(60, -95)
    z.seth(161)
    z.circle(-300, 15)
    z.pu()
    z.goto(-100, 100)
    z.pd()
    z.seth(-30)
    a = 0.4
    for i in range(60):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            z.lt(3)  # 向左转3度
            z.fd(a)  # 向前走a的步长
        else:
            a = a - 0.08
            z.lt(3)
            z.fd(a)
            z.end_fill()


def ear():
    # 耳朵
    z.color((255, 155, 192), "pink")
    z.pu()
    z.seth(90)
    z.fd(-7)
    z.seth(0)
    z.fd(70)
    z.pd()
    z.begin_fill()
    z.seth(100)
    z.circle(-50, 50)
    z.circle(-10, 120)
    z.circle(-50, 54)
    z.end_fill()

    z.pu()
    z.seth(90)
    z.fd(-12)
    z.seth(0)
    z.fd(30)
    z.pd()
    z.begin_fill()
    z.seth(100)
    z.circle(-50, 50)
    z.circle(-10, 120)
    z.circle(-50, 56)
    z.end_fill()


def eyes():
    # 眼睛
    z.color((255, 155, 192), "white")
    z.pu()
    z.seth(90)
    z.fd(-20)
    z.seth(0)
    z.fd(-95)
    z.pd()
    z.begin_fill()
    z.circle(15)
    z.end_fill()

    z.color("black")
    z.pu()
    z.seth(90)
    z.fd(12)
    z.seth(0)
    z.fd(-3)
    z.pd()
    z.begin_fill()
    z.circle(3)
    z.end_fill()

    z.color((255, 155, 192), "white")
    z.pu()
    z.seth(90)
    z.fd(-25)
    z.seth(0)
    z.fd(40)
    z.pd()
    z.begin_fill()
    z.circle(15)
    z.end_fill()

    z.color("black")
    z.pu()
    z.seth(90)
    z.fd(12)
    z.seth(0)
    z.fd(-3)
    z.pd()
    z.begin_fill()
    z.circle(3)
    z.end_fill()


def gill():
    # 红腮
    z.color((255, 155, 192))
    z.pu()
    z.seth(90)
    z.fd(-95)
    z.seth(0)
    z.fd(65)
    z.pd()
    z.begin_fill()
    z.circle(30)
    z.end_fill()


def mouth():
    # 嘴
    z.color(239, 69, 19)
    z.pu()
    z.seth(90)
    z.fd(15)
    z.seth(0)
    z.fd(-100)
    z.pd()
    z.seth(-80)
    z.circle(30, 40)
    z.circle(40, 80)


def body():
    # 身体
    z.color("red", (255, 99, 71))
    z.pu()
    z.seth(90)
    z.fd(-20)
    z.seth(0)
    z.fd(-78)
    z.pd()
    z.begin_fill()
    z.seth(-130)
    z.circle(100, 10)
    z.circle(300, 30)
    z.seth(0)
    z.fd(230)
    z.seth(90)
    z.circle(300, 30)
    z.circle(100, 3)
    z.color((255, 155, 192), (255, 100, 100))
    z.seth(-135)
    z.circle(-80, 63)
    z.circle(-150, 24)
    z.end_fill()


def hand():
    # 手
    z.color((255, 155, 192))
    z.pu()
    z.seth(90)
    z.fd(-40)
    z.seth(0)
    z.fd(-27)
    z.pd()
    z.seth(-160)
    z.circle(300, 15)
    z.pu()
    z.seth(90)
    z.fd(15)
    z.seth(0)
    z.fd(0)
    z.pd()
    z.seth(-10)
    z.circle(-20, 90)

    z.pu()
    z.seth(90)
    z.fd(30)
    z.seth(0)
    z.fd(237)
    z.pd()
    z.seth(-20)
    z.circle(-300, 15)
    z.pu()
    z.seth(90)
    z.fd(20)
    z.seth(0)
    z.fd(0)
    z.pd()
    z.seth(-170)
    z.circle(20, 90)


def foot():
    # 脚
    z.pensize(10)
    z.color((240, 128, 128))
    z.pu()
    z.seth(90)
    z.fd(-75)
    z.seth(0)
    z.fd(-180)
    z.pd()
    z.seth(-90)
    z.fd(40)
    z.seth(-180)
    z.color("black")
    z.pensize(15)
    z.fd(20)

    z.pensize(10)
    z.color((240, 128, 128))
    z.pu()
    z.seth(90)
    z.fd(40)
    z.seth(0)
    z.fd(90)
    z.pd()
    z.seth(-90)
    z.fd(40)
    z.seth(-180)
    z.color("black")
    z.pensize(15)
    z.fd(20)


def tail():
    # 尾巴
    z.pensize(4)
    z.color((255, 155, 192))
    z.pu()
    z.seth(90)
    z.fd(70)
    z.seth(0)
    z.fd(95)
    z.pd()
    z.seth(0)
    z.circle(70, 20)
    z.circle(10, 330)
    z.circle(70, 30)
    z.done()


def init():
    z.pensize(4)
    z.hideturtle()
    z.colormode(255)
    z.color((255, 155, 192), "pink")
    z.setup(840, 500)
    z.speed(10)


def main():
    init()
    nose()
    head()
    ear()
    eyes()
    gill()
    mouth()
    body()
    hand()
    foot()
    tail()


if __name__ == '__main__':
    main()

后面还要学习random库 还有math库绘制更加复杂的图

你可能感兴趣的:(用turtle库绘制小猪佩奇)