Python Turtle库绘制小猪佩奇

宁静致远
5个月前 阅读 278 点赞 5

小猪佩奇前段时间成了风靡一时的网红,虽然后面被抖音封了...

现在我们来用 python 的 turtle 库一笔一划地画一只“猪”。Turtle库是Python语言中一个很流行的绘制图像的函数库,这只“小乌龟”在一个横轴为 xx 、纵轴为 yy 的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。不得不说这个库里的函数方法名太人性化了,几乎是模拟了一个人在画画时的各个动作,并且还提供函数的简称。

话不多说,代码如下:

import turtle

turtle.pensize(4)
turtle.hideturtle()
turtle.colormode(255)
turtle.color((255, 155, 192), "pink")
turtle.setup(840, 500)
turtle.speed(10)

# 鼻子
turtle.pu()
turtle.goto(-100, 100)
turtle.pd()
turtle.seth(-30)
turtle.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        turtle.lt(3)  # 向左转3度
        turtle.fd(a)  # 向前走a的步长
    else:
        a = a - 0.08
        turtle.lt(3)
        turtle.fd(a)
turtle.end_fill()

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

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

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

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

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

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

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

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

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

# 腮
turtle.color((255, 155, 192))
turtle.pu()
turtle.seth(90)
turtle.fd(-95)
turtle.seth(0)
turtle.fd(65)
turtle.pd()
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()

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

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

# 手
turtle.color((255, 125, 192))
turtle.pu()
turtle.seth(90)
turtle.fd(-40)
turtle.seth(0)
turtle.fd(-27)
turtle.pd()
turtle.seth(-160)
turtle.circle(300, 15)
turtle.pu()
turtle.seth(90)
turtle.fd(15)
turtle.seth(0)
turtle.fd(0)
turtle.pd()
turtle.seth(-10)
turtle.circle(-20, 90)

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

# 脚
turtle.pensize(10)
turtle.color((112, 128, 128))
turtle.pu()
turtle.seth(90)
turtle.fd(-75)
turtle.seth(0)
turtle.fd(-180)
turtle.pd()
turtle.seth(-90)
turtle.fd(40)
turtle.seth(-180)
turtle.color("black")
turtle.pensize(15)
turtle.fd(20)

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

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


下面是最后的佩奇~与君共勉!


| 5
登录后可评论,马上登录吧~
评论 ( 0 )

还没有人评论...