打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
刚刚出炉的冬奥会吉祥物:冰墩墩,附源码...

在抖音上面看到了有人画的冬奥会的冰墩墩,自己也想做一个。当然,图案的绘制还是得使用我们熟悉的turtle框架。原因很简单,它是一种基于canvas画布的UI框架。

文末附完整源代码,可直接运行。

首先,将这个turtle库安装好。

pip install turtle

将turtle导入我们的模块使用即可。

import turtle as tle

设置画笔的全局属性,先设置画笔的基本速度和UI界面的标题吧

tle.speed(50)  # 速度设置为100
tle.title('冬奥会:冰墩墩! 公众号:[Python 集中营]')  # 设置好UI界面的标题
tle.bgcolor('white')  # 将背景颜色设置为白色,有冬季的感觉...
tle.pencolor("deep sky blue")
tle.fillcolor("deep sky blue")

设置好画笔的全局属性以后,接下来就是图形绘制的部分。思路就是拿一只画笔在画布上面画图就好了。

在开始绘制之前,先来说明一下几个主要函数的使用方法。代码量比较多,但是用到的函数基本都是下面这几个。

turtle.goto(x,y)  将画笔移动到坐标为x,y的位置
turtle.penup()  提起笔移动,不绘制图形,用于另起一个地方绘制
turtle.circle()  画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
setheading(angle)  设置当前朝向为angle角度
turtle.pendown()  移动时绘制图形,缺省时也为绘制
turtle.begin_fill()  准备开始填充图形
turtle.end_fill()  填充完成
turtle.left(degree) 逆时针移动degree°
turtle.forward(distance) 向当前画笔方向移动distance像素长度

画出冰墩墩的两个耳朵,注意在画布上把握好坐标,尽量计划将冰墩墩放在画布的正中间。

# 冰墩墩左耳朵
tle.penup()
tle.goto(-120200)
tle.setheading(160)
tle.begin_fill()
tle.pendown()
tle.circle(-30230)
tle.setheading(180)
tle.circle(3790)
tle.end_fill()
# 冰墩墩右耳朵
tle.penup()
tle.goto(90200)
tle.setheading(20)
tle.begin_fill()
tle.pendown()
tle.circle(30230)
tle.setheading(0)
tle.circle(-3790)
tle.end_fill()

绘制冰墩墩的头部,头部主要是通过弧线构成的。

# 冰墩墩头部
tle.pensize(5)
tle.penup()
tle.goto(-83237)
tle.setheading(30)
tle.pendown()
tle.circle(-13460)

tle.penup()
tle.goto(-120200)
tle.setheading(-120)
tle.pendown()
tle.circle(20080)

tle.penup()
tle.goto(90200)
tle.setheading(-60)
tle.pendown()
tle.circle(-20080)

tle.penup()
tle.setheading(210)
tle.pendown()
tle.circle(-12060)

绘制冰墩墩的双眼情况,双眼主要由眼圈、眼眶、眼珠构成的。

# 冰墩墩左眼
tle.penup()
tle.goto(-110100)
tle.setheading(-45)
tle.begin_fill()
tle.pendown()
agle = 0.2
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.1
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.1
        tle.left(3)
        tle.forward(agle)
tle.end_fill()

tle.fillcolor("white")
tle.penup()
tle.goto(-73125)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(14360)
tle.end_fill()

tle.penup()
tle.goto(-72133)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(6360)
tle.end_fill()

# 冰墩墩右眼
tle.penup()
tle.goto(80100)
tle.setheading(45)
tle.begin_fill()
tle.fillcolor("deep sky blue")
tle.pendown()
agle = 0.2
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.1
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.1
        tle.left(3)
        tle.forward(agle)
tle.end_fill()

tle.fillcolor("white")
tle.penup()
tle.goto(43125)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(14360)
tle.end_fill()

tle.penup()
tle.goto(42133)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(6360)
tle.end_fill()

绘制冰墩墩的鼻子、嘴,通过倒立的弧形来展现。

# 冰墩墩鼻子
tle.penup()
tle.goto(-25133)
tle.begin_fill()
tle.fillcolor("deep sky blue")
tle.pendown()
tle.forward(20)
tle.seth(-120)
tle.forward(20)
tle.seth(120)
tle.forward(20)
tle.end_fill()

# 冰墩墩嘴巴
tle.penup()
tle.goto(-40110)
tle.setheading(-30)
tle.begin_fill()
tle.fillcolor("deep sky blue")
tle.pendown()
tle.circle(5060)
tle.setheading(-120)
tle.circle(-10015)
tle.circle(-1590)
tle.circle(-10015)
tle.end_fill()

绘制冰墩墩的四肢情况,是常规的正面打招呼的经典形象。

# 冰墩墩左臂
tle.fillcolor("deep sky blue")
tle.penup()
tle.goto(-145100)
tle.begin_fill()
tle.setheading(-120)
tle.pendown()
tle.forward(100)
tle.setheading(-110)
tle.circle(20180)
tle.forward(30)
tle.circle(-5160)
tle.end_fill()

# 冰墩墩右臂
tle.penup()
tle.goto(115100)
tle.setheading(60)
tle.begin_fill()
tle.pendown()
tle.forward(100)
tle.setheading(70)
tle.circle(20180)
tle.forward(30)
tle.circle(-5160)
tle.end_fill()

# 冰墩墩手掌心的小红心
tle.penup()
tle.pencolor('red')
tle.goto(135200)
tle.begin_fill()
tle.fillcolor("red")
tle.pendown()
tle.circle(-5180)
tle.setheading(90)
tle.circle(-5180)
tle.setheading(-120)
tle.forward(17)
tle.penup()
tle.goto(135200)
tle.pendown()
tle.setheading(-60)
tle.forward(17)
tle.end_fill()

tle.pencolor("deep sky blue")
tle.fillcolor("deep sky blue")

# 冰墩墩左腿
tle.penup()
tle.goto(-90-45)
tle.begin_fill()
tle.pendown()
tle.setheading(-90)
tle.circle(-14020)
tle.circle(5109)
tle.forward(30)
tle.circle(10120)
tle.setheading(90)
tle.circle(-14010)
tle.end_fill()

# 冰墩墩右腿
tle.penup()
tle.goto(60-45)
tle.begin_fill()
tle.pendown()
tle.setheading(-90)
tle.circle(14020)
tle.circle(-5109)
tle.forward(30)
tle.circle(-10120)
tle.setheading(90)
tle.circle(14010)
tle.end_fill()

绘制出冰墩墩最外面的一个雪壳,由于3D形象不好绘制,这里使用一个最外面的轮廓线表示。

tle.pensize(5)
tle.penup()
tle.goto(-130195)
tle.setheading(160)
tle.pendown()
tle.circle(-40230)
tle.setheading(30)
tle.circle(-13458)
tle.setheading(60)
tle.circle(-40215)
tle.setheading(-60)
tle.forward(15)
tle.circle(2200)
tle.setheading(65)
tle.forward(30)
tle.circle(-25180)
tle.forward(100)
tle.circle(225)
tle.circle(-20047)
tle.circle(260)
tle.circle(14023)
tle.circle(-290)
tle.setheading(180)
tle.forward(70)
tle.circle(-290)
tle.forward(30)
tle.setheading(-160)
tle.circle(-10035)
tle.setheading(-90)
tle.forward(30)
tle.circle(-290)
tle.forward(70)
tle.circle(-290)
tle.setheading(60)
tle.circle(14030)
tle.circle(245)
tle.circle(-20019)
tle.circle(2130)
tle.forward(30)
tle.circle(-25180)
tle.forward(100)
tle.setheading(90)
tle.circle(-20030)

绘制冰墩墩的面罩,通过绘制几条不同颜色的弧线来实现效果。

tle.pensize(3)
tle.penup()
tle.goto(95120)
tle.setheading(90)
tle.pendown()

# 绘制第一层红色的弧线
tle.pencolor("red")
agle = 1
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:  # 控制a的变化
        agle = agle + 0.25
        tle.left(3)  # 向左转3度
        tle.forward(agle)  # 向前走a的步长
    else:
        agle = agle - 0.25
        tle.left(3)
        tle.forward(agle)

# 绘制第二层橘黄色的弧线
tle.pencolor("orange")
tle.penup()
tle.goto(96120)
tle.pendown()
agle = 1
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.255
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.255
        tle.left(3)
        tle.forward(agle)

# 绘制第三层绿色的弧线
tle.pencolor("green")
tle.penup()
tle.goto(97120)
tle.pendown()
agle = 1
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.2555
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.2555
        tle.left(3)
        tle.forward(agle)

# 绘制第四层天蓝色的弧线
tle.pencolor("deep sky blue")
tle.penup()
tle.goto(98120)
tle.pendown()
agle = 1
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.25955
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.25955
        tle.left(3)
        tle.forward(agle)

# 绘制第五层粉色的弧线
tle.pencolor("pink")
tle.penup()
tle.goto(101120)
tle.pendown()
agle = 1
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.26
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.26
        tle.left(3)
        tle.forward(agle)

# 绘制第六层紫色的弧线
tle.pencolor("purple")
tle.penup()
tle.goto(102120)
tle.pendown()
agle = 1
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.269
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.269
        tle.left(3)
        tle.forward(agle)

绘制字母字样,背景欢迎您。

printer = tle.Turtle()
printer.hideturtle()
printer.penup()
printer.goto(-155)
printer.write("welcome to beijing !", move=True, align='center', font=('黑体'14'normal'))

绘制奥运五环。

tle.penup()
tle.goto(-25-10)
tle.pendown()
tle.pencolor("blue")
tle.circle(10)
tle.penup()
tle.goto(-10-10)
tle.pendown()
tle.pencolor("black")
tle.circle(10)
tle.penup()
tle.goto(5-10)
tle.pendown()
tle.pencolor("red")
tle.circle(10)
tle.penup()
tle.goto(-20-20)
tle.pendown()
tle.pencolor("yellow")
tle.circle(10)
tle.penup()
tle.goto(0-20)
tle.pendown()
tle.pencolor("green")
tle.circle(10)

tle.hideturtle()
# 绘制完成
tle.done()
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用 Python 画一个捂脸表情
Python — — turtle 常用代码
Turtle海龟库:python3内置简单却很强大的绘图库
如何用Python画一只机器猫?
画出奥运五环
python内置库turtle绘制有趣图形
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服