一年一度的520又来了,时光往复,祝陪在你身边的人始终如初。
希望单身的朋友顺利脱单,有男/女朋友的朋友约会甜蜜~
实现本文效果的整体思路是:加载库—选择背景音乐—绘制心的外轮廓—填充心并写告白信—绘制心动线。
要想实现本文的效果,首先要先在python中导入本文需要加载的库。如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。
'''
代码用途 :情人节表白
作者 :阿黎逸阳
博客 : https://blog.csdn.net/qq_32532663/article/details/106176609
'''
import os
import pygame
import turtle as t
本文应用到的库较少,只应用了os、pygame和turtle三个库。
os库可以设置文件读取的位置。
pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐,如果无需背景音乐,不用加载该库。
turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。
接着应用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函数中把你想放音乐的地址填进去。
然后绘制心的外轮廓,代码如下:
t.title('阿黎逸阳的代码公众号')
t.speed(10)
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函数中的半径和弧度来调节曲线的弧度,从而使得小蜜蜂的轮廓比较流畅。
接下来边填充心,边写告白信,代码如下:
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 = 90
y = 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, '永 远 不 说 再 见')
最后是写姓名并画心动线,代码如下:
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)
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中实现永恒心动已全部讲解完毕,感兴趣的朋友可以自己实现一遍。
由于文章是手动转到博客中来的,微信公众号的发送时间一般会早于博客,如果想第一时间获取文章信息,可以关注 “阿黎逸阳的代码”公众号。
长按(扫一扫)识别上方二维码学习更多Python和建模知识,让你的学习和工作更出彩。
Original: https://blog.csdn.net/qq_32532663/article/details/124786507
Author: 阿黎逸阳
Title: Python绘制520表白代码——永恒的心动
相关阅读
Title: python的eval函数
目录
(4)与input函数以及if条件语句、while语句一起结合使用。
1.eval函数的语法及用法
(1)语法:eval(expression)
参数说明
expression:字符串表达式,可为算法,也可为input函数等。
说明:表达式必需是字符串,否则会报错,比如直接输入数值会报错为:”TypeError: eval() arg 1 must be a string, bytes or code object”,如下图所示。

(2)作用:接收运行一个字符串表达式,返回表达式的结果值。
2.实例
(1)简单的计算用法
例1:求得2+3得值。
eval('2+3') #jupyter运行可直接输出结果
print(eval('2+3')) #pycharm若需要直接输出改结果可以用print函数输出。
jupyter运行的结果如下图所示。

例2:将某字符串的数字转为数值型数字。
eval('3') #jupyter运行可直接输出结果
print(eval('3')) #pycharm若需要直接输出改结果可以用print函数输出。

x = 233
eval('x+123')

(2)与其它函数结合使用,比如结合input函数使用
例1:提示用户输入目标值,并用于计算。
x = eval(input('请输入数字:'))
y = x + 234
print(y)

(3)与while语句、input函数结合使用。
例:连续计算两个参数输入的相加值,相当于计算器输入的两个值相加。
[En]
Example: the addition value of two parameter inputs is calculated continuously, which is equivalent to the addition of two values input by calculator.
while True:
x = eval(input('请输入数字:')) #输入一个x值
y = eval(input('请输入数字:')) #输入y值
z = x + y #相加
print(z) #输出z的值

(4)与input函数以及if条件语句、while语句一起结合使用。
例: 无限输入成绩数值判断成绩等级。
while True:
score = eval(input('输入数值:'))
if score >= 90:
print('优秀')
elif score >=80:
print('良好')
elif score >= 70:
print('一般')
elif score >= 60:
print('及格')
else:
print('不及格')

说明:eval函数在计算过程具有int函数、float函数功能。
参考文章:Python eval() 函数 | 菜鸟教程 (runoob.com)
if条件语句具体用法实例可参考:https://blog.csdn.net/weixin_50853979/article/details/124975196
input函数具体用法实例可参考:https://blog.csdn.net/weixin_50853979/article/details/124978009
while函数具体用法可参考:https://blog.csdn.net/weixin_50853979/article/details/124979562
Original: https://blog.csdn.net/weixin_50853979/article/details/124997408
Author: 小白修炼晋级中
Title: python的eval函数
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/285253/
转载文章受原作者版权保护。转载请注明原作者出处!