Python学习笔记 day6

本文详细介绍了如何使用Python的turtle库绘制卡通形象小猪佩奇,包括身体、头部、耳朵、鼻子、眼睛等部分的绘制过程。通过调整绘制顺序,避免了使用end_fill()时图案覆盖的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python学习笔记 day5

使用python turtle库绘制佩奇

import turtle as t

t.pensize(3)
t.colormode(255)
t.setup(800, 800)
t.speed(10)

#身体
t.color("pink", "red")
t.pu()
t.goto(0,-10)
t.pendown()
t.begin_fill()
t.setheading(230)
t.fd(200)
t.setheading(0)
t.fd(400)
t.setheading(135)
t.fd(230)
t.end_fill()

#头
t.color("red", "pink")
t.pu()
t.goto(-200,200)
t.setheading(0)
t.pendown()
t.begin_fill()
t.setheading(350)
t.circle(-300,30)
t.setheading(230)
t.circle(80,100)
t.circle(280,10)
t.circle(120,180)
t.setheading(145)
t.circle(300,51)
t.end_fill()

#耳朵
t.pu()
t.goto(100,212)
t.pd()
t.setheading(-30)
t.begin_fill()  # 准备开始填充图形
a = 0.9
for i in range(60):
    if 0 <= i < 15 or 30 <= i < 45:
        a = a + 0.08
        t.left(7)  # 向左转3度
        t.forward(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.left(3)
        t.forward(a)
t.end_fill()  # 填充完成

t.pu()
t.goto(50,238)
t.pd()
t.setheading(-30)
t.begin_fill()  # 准备开始填充图形
a = 0.9
for i in range(60):
    if 0 <= i < 15 or 30 <= i < 45:
        a = a + 0.08
        t.left(7)  # 向左转3度
        t.forward(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.left(3)
        t.forward(a)
t.end_fill()  # 填充完成


#鼻子
t.pu()
t.goto(-200,200)
t.pd()
t.setheading(-30)
t.begin_fill()  # 准备开始填充图形
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.left(3)  # 向左转3度
        t.forward(a)  # 向前走a的步长
    else:
        a = a - 0.08
        t.left(3)
        t.forward(a)
t.end_fill()  # 填充完成
t.pu()
t.goto(-175,220)
t.pd()
t.circle(5)
t.pu()
t.goto(-190,230)
t.pd()
t.circle(5)


#嘴
t.pu()
t.goto(-20,50)
t.setheading(300)
t.pendown()
t.circle(80,80)


#腮
t.pu()
t.goto(50,80)
t.setheading(300)
t.color("black", "grey")
t.pendown()
t.begin_fill()
t.circle(40)
t.end_fill()

#眼
t.color("black", "white")
t.pu()
t.goto(-50,220)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.pu()
t.goto(-42,225)
t.pendown()
t.dot(10, "black")
t.pu()
t.goto(-90,228)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.pu()
t.goto(-82,233)
t.pendown()
t.dot(10, "black")

#手
t.color("black")
t.pu()
t.goto(-18,-30)
t.setheading(170)
t.pendown()
t.circle(180,70)
t.pu()
t.goto(-195,-100)
t.setheading(180)
t.pendown()
t.fd(30)
t.pu()
t.goto(-195,-100)
t.setheading(330)
t.pendown()
t.fd(30)

t.pu()
t.goto(130,-30)
t.setheading(0)
t.pendown()
t.circle(-180,70)
t.pu()
t.goto(290,-130)
t.setheading(199)
t.pendown()
t.fd(30)
t.pu()
t.goto(290,-130)
t.setheading(330)
t.pendown()
t.fd(30)

#腿
t.color("black", "white")
t.pu()
t.goto(90,-160)
t.pendown()
t.setheading(270)
t.fd(100)
t.setheading(0)
t.fd(10)
t.setheading(90)
t.fd(100)
t.color("white", "black")
t.pu()
t.goto(100,-260)
t.begin_fill()
t.setheading(180)
t.fd(30)
t.setheading(90)
t.fd(10)
t.setheading(0)
t.fd(30)
t.end_fill()

t.color("black", "white")
t.pu()
t.goto(40,-160)
t.pendown()
t.setheading(270)
t.fd(100)
t.setheading(0)
t.fd(10)
t.setheading(90)
t.fd(100)
t.color("white", "black")
t.pu()
t.goto(50,-260)
t.begin_fill()
t.setheading(180)
t.fd(30)
t.setheading(90)
t.fd(10)
t.setheading(0)
t.fd(30)
t.end_fill()

t.done()

存在问题:
使用end_fill()时,总是覆盖已存在的图案。
所以只能调整绘制顺序,以避免覆盖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值