2022年北京冬奥会,冰墩墩可谓是出了圈,近几天热搜不断,为了响应一户一墩,应墩尽墩,这次我们使用Python画一个冰墩墩,效果图如下(能认出来是冰墩墩就好):
主要使用的就是Python的turtle库,然后就是不规则图形的切割与拼接,没有什么技术含量,代码如下:
import turtle as t
from turtle import Screen
def buildBody(pen):
"""
画身体
:param pen:
:return:
"""
# 画一个上圆下扁的椭圆形
pen.down()
t.speed(10)
pen.left(90)
# 画个半圆
for i in range(180):
pen.forward(3)
pen.right(1)
# 画个椭圆
for i in range(40):
pen.forward(5)
pen.right(1)
pen.up()
pen.goto(-180,90)
pen.down()
pen.setheading(0)
pen.right(90)
for i in range(40):
pen.forward(5)
pen.left(1)
pen.left(4)
for i in range(50):
pen.forward(5)
pen.left(2)
pen.up()
pen.goto(-142,200)
pen.setheading(0)
pen.left(106)
pen.down()
pen.fillcolor((0,0,0))
pen.begin_fill()
# 画耳朵
for i in range(26):
pen.forward(2)
pen.right(2)
pen.right(10)
for i in range(50):
pen.forward(1)
pen.right(2)
pen.end_fill()
pen.up()
pen.goto(130, 200)
pen.setheading(0)
pen.left(78)
pen.down()
pen.fillcolor((0, 0, 0))
pen.begin_fill()
# 画耳朵
for i in range(26):
pen.forward(2)
pen.left(2)
pen.left(10)
for i in range(50):
pen.forward(1)
pen.left(2)
pen.end_fill()
# 画手
pen.up()
pen.goto(-180,46)
pen.setheading(0)
pen.left(220)
pen.down()
pen.begin_fill()
for i in range(4):
pen.forward(16)
pen.left(4)
for i in range(90):
pen.forward(1)
pen.left(2)
pen.setheading(0)
pen.left(60)
for i in range(4):
pen.forward(6)
pen.left(1)
pen.end_fill()
pen.up()
pen.goto(160, 26)
pen.setheading(0)
pen.down()
pen.begin_fill()
pen.left(20)
for i in range(4):
pen.forward(18)
pen.left(4)
for i in range(90):
pen.forward(1)
pen.left(2)
for i in range(6):
pen.forward(6)
pen.left(1)
pen.end_fill()
# 画爱心
pen.up()
pen.color((255,0,0))
pen.goto(210,90)
pen.setheading(0)
pen.down()
pen.fillcolor((255,0,0))
pen.begin_fill()
pen.left(135)
pen.circle(7.07,180)
pen.goto(210,70)
pen.left(90)
pen.goto(220,80)
pen.circle(7.07,180)
pen.end_fill()
# 开始画腿
pen.color((0,0,0))
pen.up()
pen.goto(-120,-86)
pen.down()
pen.setheading(0)
pen.left(260)
pen.begin_fill()
for i in range(10):
pen.forward(6)
pen.left(1)
for i in range(10):
pen.forward(6)
pen.left(10)
pen.right(20)
for i in range(2):
pen.forward(20)
pen.left(2)
pen.setheading(0)
for i in range(4):
pen.left(25)
pen.forward(2)
for i in range(4):
pen.forward(4)
pen.left(1)
pen.setheading(0)
pen.left(90)
pen.forward(6)
for i in range(28):
pen.left(3)
pen.forward(4)
pen.end_fill()
# 开始画右腿
pen.up()
pen.goto(100, -86)
pen.down()
pen.setheading(0)
pen.right(80)
pen.begin_fill()
for i in range(10):
pen.forward(6)
pen.right(1)
for i in range(10):
pen.forward(6)
pen.right(10)
pen.left(20)
for i in range(2):
pen.forward(20)
pen.right(2)
pen.setheading(0)
for i in range(4):
pen.left(25)
pen.forward(2)
for i in range(4):
pen.forward(4)
pen.right(1)
pen.setheading(0)
pen.left(90)
pen.forward(6)
for i in range(28):
pen.right(3)
pen.forward(4)
pen.end_fill()
def buildFace(pen):
"""
画冰墩墩的脸
:param pen:
:return:
"""
pen.up()
pen.goto(-40,150)
pen.down()
pen.begin_fill()
pen.setheading(0)
pen.left(135)
a = 1
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90: # 控制a的变化
a = a + 0.07
pen.left(3) # 向左转3度
pen.forward(a) # 向前走a的步长
else:
a = a - 0.07
pen.left(3)
pen.forward(a)
pen.end_fill()
pen.up()
pen.color((255,255,255))
pen.setheading(0)
pen.goto(-56,110)
pen.down()
t.pensize(3)
pen.circle(18,360)
pen.up()
pen.goto(-62,134)
pen.down()
pen.fillcolor((255,255,255))
pen.begin_fill()
pen.circle(2,360)
pen.end_fill()
pen.up()
pen.color((0,0,0))
pen.goto(38, 150)
pen.down()
pen.begin_fill()
pen.setheading(0)
pen.right(135)
a = 1
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90: # 控制a的变化
a = a + 0.07
pen.left(3) # 向左转3度
pen.forward(a) # 向前走a的步长
else:
a = a - 0.07
pen.left(3)
pen.forward(a)
pen.end_fill()
pen.up()
pen.color((255, 255, 255))
pen.setheading(0)
pen.goto(54, 110)
pen.down()
t.pensize(3)
pen.circle(18, 360)
pen.up()
pen.goto(50, 134)
pen.down()
pen.fillcolor((255, 255, 255))
pen.begin_fill()
pen.circle(2, 360)
pen.end_fill()
# 画鼻子
pen.up()
pen.color((0,0,0))
pen.goto(0,90)
pen.down()
pen.setheading(0)
pen.down()
pen.fillcolor((0, 0, 0))
pen.begin_fill()
pen.left(135)
pen.circle(7.07, 180)
pen.circle(10,150)
pen.circle(4,180)
pen.end_fill()
pen.setheading(0)
pen.up()
pen.goto(0,0)
# 画嘴巴
pen.up()
pen.goto(-20,60)
pen.down()
pen.begin_fill()
pen.right(180)
pen.circle(30,180)
pen.forward(50)
pen.circle(30,180)
pen.end_fill()
pen.up()
pen.goto(-20,60)
pen.down()
pen.fillcolor((255,255,255))
pen.begin_fill()
pen.color((255,255,255))
pen.setheading(0)
pen.right(45)
for i in range(10):
pen.forward(6)
pen.left(10)
pen.end_fill()
pen.color((255,0,0))
pen.up()
pen.goto(52,10)
pen.setheading(0)
pen.down()
pen.begin_fill()
pen.left(140)
pen.circle(80,76)
pen.setheading(0)
pen.right(34)
for i in range(4):
pen.forward(6)
pen.left(4)
pen.setheading(0)
pen.forward(60)
for i in range(11):
pen.forward(2)
pen.left(3)
pen.end_fill()
def drawBorder(pen,start_point,radius_1,color):
"""
画出冰墩墩的脸部边界
:param pen:
:param start_point:
:param radius_1:
:param color:
:return:
"""
pen.up()
pen.goto(start_point[0],start_point[1])
pen.setheading(0)
pen.color(color)
pen.down()
pen.left(90)
pen.circle(radius_1,360)
def drawLogo(pen):
"""
画logo
:param pen:
:return:
"""
t.pensize(2)
pen.up()
pen.goto(-30,-110)
pen.color((0, 176, 255))
pen.setheading(0)
pen.down()
pen.circle(10,360)
pen.up()
pen.goto(-6, -110)
pen.down()
pen.color((0,0,0))
pen.circle(10,360)
pen.up()
pen.goto(18, -110)
pen.down()
pen.color((255, 0, 0))
pen.circle(10, 360)
pen.up()
pen.goto(-18, -120)
pen.down()
pen.color((255, 255, 0))
pen.circle(10, 360)
pen.up()
pen.goto(6, -120)
pen.down()
pen.color((0, 255, 0))
pen.circle(10, 360)
pen.up()
pen.goto(-50,-90)
pen.color((0,0,0))
t.write("BEIJING 2022",move=False, align="left", font=("Manksa", 18, "normal"))
def draw():
"""
什么参数都不需要 开始画冰墩墩
:return:
"""
# 设置画布大小
t.screensize(512,512,'white')
# 设置窗口大小(与屏幕的比例)
Screen().setup(width=0.4,height=0.75)
Screen().colormode(255)
t.pensize(5)
pen = t.getpen()
pen.up()
pen.goto(-180,90)
# 画冰墩墩的身体
buildBody(pen)
# 画冰墩墩的脸
buildFace(pen)
# 画出边框
drawBorder(pen,(130,100),130,(152, 223, 248))
drawBorder(pen,(134,100),134,(255, 0, 0))
drawBorder(pen,(138,100),138,(80, 119, 177))
drawBorder(pen,(142,100),142,(224, 236, 116))
drawBorder(pen,(146,100),146,(194, 234, 183))
# 画冬奥会logo
drawLogo(pen)
if __name__ == '__main__':
draw()
t.mainloop()
祝愿北京冬奥会举办圆满成功!!!
Q.E.D.