今天是520,作为程序员,我们不妨用turtle来表达我们的浪漫。让我们来看看如何用代码绘制一个爱心,并在其中写上”520″。
程序员的浪漫,提前预览:

以下是实现这个效果的代码:
# 导入需要用到的模块import turtle as ttimport time画爱心的顶部
def LittleHeart():for i in range(200):tt.right(1)tt.forward(2)
love = '520'
窗口大小
tt.setup(width=800, height=500)
颜色
tt.color('red', 'pink')
笔粗细
tt.pensize(5)
速度
tt.speed(2)
提笔
tt.up()
隐藏笔
tt.hideturtle()
去到的坐标,窗口中心为0,0
tt.goto(0, -180)tt.showturtle()
画上线
tt.down()tt.speed(1)tt.begin_fill()tt.left(140)tt.forward(224)
调用画爱心左边的顶部
LittleHeart()
调用画爱右边的顶部
tt.left(120)LittleHeart()
画下线
tt.forward(224)tt.end_fill()
tt.pensize(5)tt.up()tt.hideturtle()
在心中写字 一次
tt.goto(0, 0)tt.showturtle()tt.color('#CD5C5C', 'pink')
在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
tt.write(love, font=('kaiti', 40,), align="center")tt.up()tt.hideturtle()
time.sleep(2)
在心中写字 二次
tt.goto(0, 0)tt.showturtle()tt.color('red', 'pink')tt.write(love, font=('kaiti', 40,), align="center")tt.up()tt.hideturtle()
点击窗口关闭
window = tt.Screen()window.exitonclick()
在安装turtle模块时,如果你是Windows用户,可能会遇到以下问题:
(studyenv) E:workingGBasedjango_orm_48scodespacedjango_gbasedbt>pip install turtleCollecting turtle
Downloading turtle-0.0.2.tar.gz (11 kB)
ERROR: Command errored out with exit status 1:
command: 'E:devlopenvsanacondaenvsstudyenvpython.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:UsersdafeiAppDataLocalTemppip-install-i3wdw9zcturtle_bb87823cd0bf40c7b00659bf322275b4setup.py'"'"'; file='"'"'C:UsersdafeiAppDataLocalTemppip-install-i3wdw9zcturtle_bb87823cd0bf40c7b00659bf322275b4setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base 'C:UsersdafeiAppDataLocalTemppip-pip-egg-info-haf9ipy6'
cwd: C:UsersdafeiAppDataLocalTemppip-install-i3wdw9zcturtle_bb87823cd0bf40c7b00659bf322275b4
Complete output (6 lines):
Traceback (most recent call last):
File "", line 1, in
File "C:UsersdafeiAppDataLocalTemppip-install-i3wdw9zcturtle_bb87823cd0bf40c7b00659bf322275b4setup.py", line 40
except ValueError, ve: ^
SyntaxError: invalid syntaxERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
解决这个问题的方法是下载turtle的安装包,并修改setup.py文件中的第40行,将except ValueError, ve:改为except ValueError as ve:,然后再进行安装。

以上就是程序猿的520的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/430598.html
微信扫一扫
支付宝扫一扫