196

抢不到冰墩墩?那就用Python自己做一个吧!

 2 years ago
source link: https://zhuanlan.zhihu.com/p/464968851
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

抢不到冰墩墩?那就用Python自己做一个吧!

上海交通大学 计算机应用技术硕士

大家好,欢迎来到 Crossin的编程教室 !

这两天,随着北京冬奥会的开幕,吉祥物“冰墩墩”可是火出了圈,多少人排长队都买不到。据说甚至有人把价格炒到了几千元。

就连昨天的“虎虎生威”文章下面,都有同学求冰墩墩

v2-8358a03c26a97c1386a07a9b0da24999_720w.jpg

为了帮助大家早日实现“一户一墩”,Crossin连夜加班,肝出了这个:

Python版冰墩墩

话不多说,直接上效果。

绘制过程:

v2-20da613a1bc5e18a1e17688bf2ec3bfa_720w.jpg

说实话,这个没什么技术含量,就是硬“肝”,不断调整曲线细节。

用的是 Python 自带的 turtle 库。这个库之前介绍过很多次,就不再啰嗦了,历史文章里搜索“turtle”或“海龟作图”就能看到。

说个用 turtle 画图的小技巧:你可以通过

turtle.bgpic(r'bg.png')

这个语句把你要参考的图先绘制在窗口中,然后再对照着进行绘制调整,就方便许多了。

由于代码比较长,这里贴一小段供参考:

# 爱心
turtle.penup()
turtle.goto(220, 115)
turtle.pencolor("brown")
turtle.pensize(1)
turtle.fillcolor("brown")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(36)
turtle.circle(-8, 180)
turtle.circle(-60, 24)
turtle.setheading(110)
turtle.circle(-60, 24)
turtle.circle(-8, 180)
turtle.end_fill()

# 五环
turtle.penup()
turtle.goto(-5, -170)
turtle.pendown()
turtle.pencolor("blue")
turtle.circle(6)
turtle.penup()
turtle.goto(10, -170)
turtle.pendown()
turtle.pencolor("black")
turtle.circle(6)
turtle.penup()
turtle.goto(25, -170)
turtle.pendown()
turtle.pencolor("brown")
turtle.circle(6)
turtle.penup()
turtle.goto(2, -175)
turtle.pendown()
turtle.pencolor("lightgoldenrod")
turtle.circle(6)
turtle.penup()
turtle.goto(16, -175)
turtle.pendown()
turtle.pencolor("green")
turtle.circle(6)
turtle.penup()

turtle.pencolor("black")
turtle.goto(-16, -160)
turtle.write("BEIJING 2022", font=('Arial', 10, 'bold italic'))
turtle.hideturtle()

turtle.done()

完整代码在github上开源:

https://github.com/crossin/snippet/tree/master/BingDwenDwen

如果对你有帮助,欢迎点赞转发支持一下~

获取更多教程和案例,

欢迎搜索及关注:Crossin的编程教室

每天5分钟,轻松学编程。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK