一、效果
快放10倍
总共分为三部分,左上角的正文,下方的心形和右下角的署名
特别需要注意的一点是这种东西不但要装Python,还与分辨率有关(换个屏幕可能效果雪崩,因为用的是绝对坐标),因此并不建议实际拿去弄(哪怕能解决上述两个问题)
二、正文部分
效果:
本质是每写一行话,然后将坐标下移换行,再写一行,以此类推
# content就是该行的内容了,想些啥写啥吧 def drawLine(content, x, y, sleep=3): goto(x, y) write(content, font=("Microsoft YaHei",14,), align="left",move=True) time.sleep(sleep)
字体用的是微软雅黑,左对齐,每写一行休眠3秒,防止来不及看
总体结构如下,这里的h就是每次下移的像素数量,在本机上测试下移45个像素比较合适
最后通过每次输出一个点,然后右移,模拟“加载”的效果
def drawHope(x, y, h): time.sleep(3) drawLine("line1", x, y) y = y - h drawLine("line2", x, y) y = y - h # ... # ...省略一些相似代码 # ... drawLine("line11", x, y) y = y - h drawLine("最后再给你画个画啦", x, y) y = y - h drawLine(".", x, y) x = x + 40 # ... # 省略四个点... # ... drawLine(".", x, y) x = x + 40 drawLine(">_<", x, y, sleep=2)
三、爱心部分
效果:
坐标切至正下方,外面的大心形其实就是两条直线(左下+右下)加上两个半圆(左上+右上)
def drawHeart(x, y, h): # 切到正下方 goto(x, y) r = h / 5 # 画左下直线 drawBigL(r, h) # 画左上半圆 drawBigArc(r, 140) # 画右上半圆 drawBigArc(r, 70) # 画右下直线 drawBigR(r, h)
具体画四部分的逻辑比较杂,这部分代码也基本用不着修改,这里就不贴出来,感兴趣可以对照源码理解
里面的小心形其实就是由左右两个弧线拼接而成
def drawHeart(r): down() begin_fill() factor = 180 seth(45) circle(-r, factor) fd(2 * r) right(90) fd(2 * r) circle(-r, factor) end_fill() up()
四、署名部分
效果:
坐标切至右下角,留上名字和日期,比如“一直喜欢xx的x”
这里的画笔颜色用的是蓟色(#D8BFD8)
def drawName(x, y): # 画笔颜色 pencolor("#D8BFD8") # 切到右下角 goto(x, y) # 名字,喜欢你的xxx,居中对齐,consolas字体 write("xiongxyowo", font=("consolas", 16, ), align="center", move=True) # 停一下防止输出太快来不及看 time.sleep(1) # 下移50个像素换行 y = y - 50 goto(x, y) # 日期 write("2021.4.3", font=("consolas", 16, ), align="center", move=True)
五、完整代码
from turtle import * import time def setTurtle(): title("点到窗口的话就会关闭呀>_<") screensize(800, 800) pensize(3) speed(6) penup() def getStart(): # x=-600, y=545 drawHope(-600, 545, 45) # x=0, y=-450 drawHeart(0, -450, 80) # x=450, y=-490 drawName(450, -490) def drawHope(x, y, h): time.sleep(3) drawLine("line1", x, y) y = y - h drawLine("line2", x, y) y = y - h drawLine("line3", x, y) y = y - h drawLine("line4", x, y) y = y - h drawLine("line5", x, y) y = y - h drawLine("line6", x, y) y = y - h drawLine("line7", x, y) y = y - h drawLine("line8", x, y) y = y - h drawLine("line9", x, y) y = y - h drawLine("line10", x, y) y = y - h drawLine("line11", x, y) y = y - h drawLine("最后再给你画个画啦", x, y) y = y - h drawLine(".", x, y) x = x + 40 drawLine(".", x, y) x = x + 40 drawLine(".", x, y) x = x + 40 drawLine(".", x, y) x = x + 40 drawLine(".", x, y) x = x + 40 drawLine(".", x, y) x = x + 40 drawLine(">_<", x, y, sleep=2) def drawHeart(x, y, h): goto(x, y) r = h / 5 drawBigL(r, h) drawBigArc(r, 140) drawBigArc(r, 70) drawBigR(r, h) def drawBigL(r, h): for i in range(int(240 / h) + 1): seth(0) color("pink", "pink") drawMiniHeart(r) seth(140) fd(h) def drawBigArc(r, rad): for i in range(50): if(i % 10 == 0): seth(0) color("pink", "pink") drawMiniHeart(r) seth(rad - (i + 1) * 4) rt(4) fd(10.5) def drawBigR(r, h): for i in range(int(240 / h) + 1): seth(0) color("pink", "pink") drawMiniHeart(r) setheading(220) fd(h) def drawMiniHeart(r): down() begin_fill() factor = 180 seth(45) circle(-r, factor) fd(2 * r) right(90) fd(2 * r) circle(-r, factor) end_fill() up() def drawName(x, y): pencolor("#D8BFD8") goto(x, y) write("xiongxyowo", font=("consolas", 16, ), align="center", move=True) time.sleep(1) y = y - 50 goto(x, y) write("2021.4.3", font=("consolas", 16, ), align="center", move=True) def drawLine(content, x, y, sleep=3): goto(x, y) write(content, font=("Microsoft YaHei",14,), align="left",move=True) time.sleep(sleep) if __name__ == "__main__": setTurtle() getStart() window = Screen() window.exitonclick()
六、结局回收
无法读档重来,慎重
这种东西个人感觉更适合作为平时生活的小甜点,而非正菜
到此这篇关于教你利用Python+Turtle绘制简易版爱心表白的文章就介绍到这了,更多相关Python+Turtle爱心表白内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_40714949/article/details/115899554