ITPub博客

首页 > 数据库 > 数据库开发技术 > 520永恒的心动

520永恒的心动

原创 数据库开发技术 作者:阿黎逸阳的代码 时间:2022-05-15 12:00:00 0 删除 编辑

一年一度的520又来了,时光往复,祝陪在你身边的人始终如初。


之前整理过520表白代码合集、情人节表白烟花、带音乐字符画+带颜色字符画—情人节表白代码、教你用Python秀恩爱、撒狗粮—这是我的某某某。
今天向大家介绍520永恒的心动。
希望单身的朋友顺利脱单,有男/女朋友的朋友约会甜蜜~


过尽千帆,皆不是吾心所爱;
弱水三千,只有你知我冷暖。




一、绘制成品




二、绘制代码


实现本文效果的整体思路是:加载库—选择背景音乐—绘制心的外轮廓—填充心并写告白信—绘制心动线。


1.导入库


要想实现本文的效果,首先要先在python中导入本文需要加载的库。
如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。
# -*- coding: UTF-8 -*-'''代码用途 :情人节表白作者     :阿黎逸阳博客     :  https://blog.csdn.net/qq_32532663/article/details/106176609'''import osimport pygameimport turtle as t

本文应用到的库较少,只应用了os、pygame和turtle三个库。

os库可以设置文件读取的位置。

pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐,如果无需背景音乐,不用加载该库。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。



2.选择背景音乐


接着应用pygame库播放背景音乐,本文的音乐是《 瞬间的永恒》。

#播放音乐print('播放音乐')pygame.mixer.init()pygame.mixer.music.load(r"F:\公众号\520\赵海洋 - 《瞬间的永恒》夜色钢琴曲.mp3") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(1, 10)

这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。

如果选择播放音乐,需要在代码music.load函数中把你想放音乐的地址填进去。


3.绘制心的外轮廓


然后绘制心的外轮廓,代码如下:

t.title('阿黎逸阳的代码公众号')t.speed(10)#t.screensize(1000, 800)t.setup(startx=0, starty = 0, width=800, height = 600)t.hideturtle()print('画爱心')#画爱心def heart(x, y):    t.penup()    t.goto(x, y)    t.pendown()    t.color('pink')    t.setheading(50)    t.circle( -5, 180)     t.circle( -45, 12)     t.setheading(130)     t.circle( -45, 12)     t.circle( -5, 180)heart(-30, 155)heart(-220, 145)heart(-210, 60)heart(-100, 100)heart(-20, 20)heart(-70, 130)heart(-140, -20)heart(30, 100)heart(-60, -20)heart(10, 60)heart(-100, -70)heart(20, 145)heart(-140, -20)heart(-130, 130)heart(-180, 20)heart(-170, 155)heart(-230, 100)

关键代码详解:

t.penup():抬起画笔,一般用于另起一个地方绘图使用。

t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。

t.pendown():放下画笔,一般和penup组合使用。

t.color(color):设置画笔的颜色。

t.setheading(θ):设置海龟头与横坐标偏离的度数。

t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。

画外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得小蜜蜂的轮廓比较流畅。


4.填充心并写告白信


接下来边填充心,边写告白信,代码如下:

def write_mes(x, y, size, ss):    t.hideturtle()    t.penup()    t.goto(x, y)    t.pendown()    t.pencolor('black')    t.write(ss, font=('Times New Roman', size, 'normal'))#画红心print('画红心')def heart_fill(x, y):    t.penup()    t.goto(x, y)    t.pendown()    t.color('red', 'red')    t.begin_fill()    t.setheading(50)    t.circle( -5, 180)     t.circle( -45, 12)     t.setheading(130)     t.circle( -45, 12)     t.circle( -5, 180)    t.end_fill()x = 90y = 110#右边爱心write_mes(x, y, 11, '喜 欢 你 的 每 一 天')heart_fill(-100, 100)heart_fill(-70, 130)heart_fill(-30, 155)heart_fill(20, 145)heart_fill(30, 100)write_mes(x, y-30, 11, '爱 意 不 曾 退 减')heart_fill(10, 60)heart_fill(-20, 20)heart_fill(-60, -20)heart_fill(-100, -70)#左边爱心write_mes(x, y-30*2, 11, '时 光 不 曾 走 远')heart_fill(-140, -20)heart_fill(-180, 20)heart_fill(-210, 60)heart_fill(-230, 100)write_mes(x, y-30*3, 11, '幸 福 延 续 到 明 天')heart_fill(-220, 145)heart_fill(-170, 155)heart_fill(-130, 130)write_mes(x, y-30*4, 11, '永 远 不 说 再 见')



5.画心动线


最后是写姓名并画心动线,代码如下:

t.speed(15)print('画心动线')def heart_bit():    #画心动线    t.penup()    t.goto(-170, 40)    t.pendown()    t.pencolor('red')    t.setheading(0)    t.pensize(2)    t.forward(10)    #第一个小波浪    t.setheading(45)    t.circle(50, 10)    t.setheading(0)    t.circle(-3,90)    t.circle(50, 5)    #横线    t.setheading(0)    t.forward(10)    #第一个下尖峰    t.setheading(-80)    t.forward(7)    t.setheading(70)    t.forward(25)    t.setheading(-85)    t.forward(29)    t.setheading(70)    t.forward(13)    t.setheading(0)    t.forward(15)
#画心 t.setheading(150) t.circle(-20, 40) t.circle(-10, 170) t.setheading(70) t.circle(-10, 170) t.circle(-20, 40) t.setheading(0) t.forward(15) #2 t.setheading(-80) t.forward(7) t.setheading(70) t.forward(25) t.setheading(-85) t.forward(29) t.setheading(70) t.forward(13) t.setheading(0) t.forward(15) t.setheading(0) t.forward(10) t.setheading(45) t.circle(50, 10) t.setheading(0) t.circle(-3,90) t.circle(50, 5) t.setheading(0) t.forward(10)def write_name(x, y, size, ss): t.hideturtle() t.penup() t.goto(x, y) t.pendown() t.pencolor('black') t.write(ss, font=('Times New Roman', size, 'normal'))def undo_back(): t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo()def undo_back2(): t.undo() t.undo()def name_heart_bit(): #写两个人的姓名(需替换成真实姓名) write_name(-180, 70, 11, '韩商言') write_name(-180, 70, 11, '韩商言') write_name(-180, 70, 11, '韩商言') heart_bit() write_name(-60, 70, 11, '佟年') write_name(-60, 70, 11, '佟年') write_name(-60, 70, 11, '佟年') write_name(-60, 70, 11, '佟年') write_name(-60, 70, 11, '佟年') undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back2()while 1: name_heart_bit()

注:如想获得本文全量直接可执行的代码,可在本公众号中回复”永恒心动“,即可免费获取。

本文中的姓名我用了影视作品《亲爱的热爱的》男女主演名字,替换成真实姓名即可。
至此,在Python中实现永恒心动已全部讲解完毕,感兴趣的朋友可以自己实现一遍。
往期回顾:

情人节表白烟花

520表白代码合集
黑客帝国中的代码雨
用python绘制皮卡丘
娱乐圈排行榜动态条形图绘制
Python人脸识别—我的眼里只有你
3D星空图V2版——添加背景图片和音乐

扫一扫关注我

13162366985

投稿微信号、手机号

原文作者:阿黎逸阳的代码;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2020-02-15

  • 博文量
    282
  • 访问量
    130896