还没有表白神器?情人节来喽,快为心爱的她送上一份专属的礼物吧~
- 2022 年 2 月 15 日
本文字数:7719 字
阅读完需:约 25 分钟
📢📢📢📣📣📣🌻🌻🌻Hello,大家好我叫是 Dream 呀,一个有趣的 Python 博主,多多关照😜😜😜🏅🏅🏅2021 年度博客之星 TOP100,2021 年度博客之星领域 TOP5,Python 领域优质创作者,欢迎大家找我合作学习(文末有 VX 想进学习交流群 or 学习资料 欢迎+++)💕
入门须知:这片乐园从不缺乏天才,努力才是你的最终入场券!🚀🚀🚀
💓最后,愿我们都能在看不到的地方闪闪发光,一起加油进步🍺🍺🍺
🍉🍉🍉“一万次悲伤,依然会有 Dream,我一直在最温暖的地方等你”,唱的就是我!哈哈哈~🌈🌈🌈🌟🌟🌟✨✨✨
前言:
一转眼,又是一年情人节,今年你是否还是一个人呢?
if 不是:
祝福你们,看下去,看完感情牢固
print('成功进入好男人关卡')
elif 是:
抱抱铁子,看下去,看完脱单
print('成功进入海王关卡')
一、背景故事👻👻👻
情人节其前夕,又有许多小伙伴们纷纷私信 Dream,能不能帮忙脱一下单啊,出一期表白神器!更有许多小伙伴因为去年用过这个: python跨年表白神器--你值得拥有!, 今年更是成功加入海王行列,纷纷来感谢 Dream,问我是否还有别的好东西。 一个资深老海王的自述(不是):好东西是有的!还有很多!于是冒着情人节不和女朋友一起过的风险,Dream 连夜赶制最新表白神器。应广大朋友们的需求它来喽:情人节专属礼物,快送给最心爱的她吧~话不多说先看效果:
🌈🌈🌈哎嘿嘿,是不是非常的 nice 呀!看到这先别着急,为了我的情人节着想,是不是得先给我来个一键三连呀!!!
接下来让我们慢慢来聊,请大家前排就坐,爆米花自取哟(点赞+收藏相当于一份爆米花😜😜😜)
二、制作过程
1.交互界面
这里使用定义了 clear_screen()函数来实现对每一次动画的清屏,有利于下一次动画的展示。全局运用了 turtle 库、time 时间库、random 随机库,以及 tkinter 库和 threading 库。安装方法:cmd 之后直接 pip install turtle(所需要的库的名称)
# 实现清屏
def clear_screen():
turtle.penup() #画笔抬起
turtle.goto(0,0) #定位到(0,0)
turtle.color('white')
turtle.pensize(800) #画笔粗细
turtle.pendown() #画笔落下
turtle.setheading(0) #设置朝向
turtle.fd(300) #前进
turtle.bk(600) #后退
# 初始化海龟的位置
def go_start(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
#画线,state为真时海龟回到原点,为假时不回到原来的出发点
def draw_line(length, angle, state):
turtle.pensize(1)
turtle.pendown()
turtle.setheading(angle)
turtle.fd(length)
turtle.bk(length) if state else turtle.penup()
turtle.penup()
2.绘制发射爱心的小人
这里是海龟画图的一些操作:
# 画出发射爱心的小人
def draw_people(x, y):
turtle.speed(0)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.pensize(2)
turtle.color('black')
turtle.setheading(0)
turtle.circle(35, 360)
turtle.penup()
turtle.pensize(3)
turtle.setheading(90)
turtle.fd(45)
turtle.setheading(180)
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.pendown()
turtle.circle(4, 360)
turtle.penup()
turtle.goto(x, y)
turtle.pensize(2)
turtle.setheading(0)
turtle.fd(20)
turtle.setheading(90)
turtle.fd(20)
turtle.setheading(-90)
turtle.pendown()
turtle.circle(5, 180)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(40)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(-60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(60)
turtle.setheading(-135)
turtle.fd(60)
turtle.bk(60)
turtle.setheading(-45)
turtle.fd(30)
turtle.setheading(-135)
turtle.fd(35)
turtle.penup()
3.绘制一箭穿心
这里面的文字都是可以根据自己需要进行修改的,大家按自己的需求来改就可以啦!
# 画爱心
def draw_heart(size):
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(size)
turtle.circle(size * -3.745, 45)
turtle.circle(size * -1.431, 165)
turtle.left(120)
turtle.circle(size * -1.431, 165)
turtle.circle(size * -3.745, 45)
turtle.fd(size)
turtle.end_fill()
turtle.speed(0)
# 画箭羽
def draw_feather(size):
angle = 30 # 箭的倾角
feather_num = size // 6 # 羽毛的数量
feather_length = size // 3 # 羽毛的长度
feather_gap = size // 10 # 羽毛的间隔
for i in range(feather_num):
draw_line(feather_gap, angle + 180, False) # 箭柄,不折返
draw_line(feather_length, angle + 145, True) # 羽翼,要折返
draw_line(feather_length, angle + 145, False)
draw_line(feather_num * feather_gap, angle, False)
draw_line(feather_length, angle + 145 + 180, False)
for i in range(feather_num):
draw_line(feather_gap, angle + 180, False) # 箭柄,不折返
draw_line(feather_length, angle - 145, True) # 羽翼,要折返
draw_line(feather_length, angle - 145, False)
draw_line(feather_num * feather_gap, angle, False)
draw_line(feather_length, angle - 145 + 180, False)
# 画一箭穿心,最后箭的头没有画出来,用海龟来代替
def arrow_heart(x, y, size):
go_start(x, y, False)
draw_heart(size * 1.15)
turtle.setheading(-150)
turtle.penup()
turtle.fd(size * 2.2)
draw_heart(size)
turtle.penup()
turtle.setheading(150)
turtle.fd(size * 2.2)
turtle.color('black')
draw_feather(size)
turtle.pensize(4)
turtle.setheading(30)
turtle.pendown()
turtle.fd(size * 2)
turtle.penup()
turtle.setheading(29)
turtle.fd(size * 5.7)
turtle.color('black')
turtle.pensize(4)
turtle.pendown()
turtle.fd(size * 1.2)
#显示倒数3,2,1
def draw_0(i):
turtle.speed(0)
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-50, -100)
turtle.color('red')
write = turtle.write(i, font=('宋体', 200, 'normal'))
time.sleep(1)
# 显示文字
def draw_1():
turtle.penup()
turtle.hideturtle() #隐藏箭头显示
turtle.goto(-450, 0)
turtle.color('red')
write = turtle.write('宝贝,情人节快乐💕💕💕', font=('宋体', 60, 'normal'))
time.sleep(2)
# 显示发射爱心的小人儿
def draw_2():
turtle.speed(3)
draw_people(-250, 20)
turtle.penup()
turtle.goto(-150, -30)
draw_heart(14)
turtle.penup()
turtle.goto(-20, -60)
draw_heart(25)
turtle.penup()
turtle.goto(205, -100)
draw_heart(43)
turtle.hideturtle()
time.sleep(2)
def draw_3():
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-220, 50)
turtle.color('red')
write = turtle.write('💕宝贝', font=('宋体', 60, 'normal'))
turtle.penup()
turtle.goto(0, -50)
write = turtle.write('宝贝💕', font=('宋体', 60, 'normal'))
time.sleep(2)
# 显示一箭穿心
def draw_4():
turtle.speed(1)
turtle.penup()
turtle.goto(-410, -200)
turtle.color('pink')
turtle.pendown()
turtle.write('猫咪公主 狗王', font=('wisdom', 60, 'normal'))
turtle.speed(1)
turtle.penup()
turtle.color("red")
turtle.goto(-31, -220)
turtle.write('❤',font=('wisdom', 80, 'normal'))
arrow_heart(20, -60, 51)
turtle.showturtle()
4.浪漫弹窗绘制
这里使用了 tkinter 模块来进行小弹窗的绘制,最后是用 for 循环来实现弹窗的重复性和随机性,这也是我觉得最大的创新之处吧!惊喜重重,满满心意与浪漫,哈哈哈。
def dow():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = random.randrange(0, width)
b = random.randrange(0, height)
window.title('宝贝!')
window.geometry("200x50" + "+" + str(a) + "+" + str(b))
tk.Label(window,
text='💕宝贝我爱你💕!', # 标签的文字
bg='pink', # 背景颜色
font=('..', 17), # 字体和字体大小
width=18, height=2 # 标签长宽
).pack() # 固定窗口位置
window.mainloop()
number=[3,2,1] #储存显示界面倒数数字1,2,3
if __name__ == '__main__':
turtle.setup(900, 500) #调画布的尺寸
for i in number:
draw_0(i)
clear_screen()
draw_1()
clear_screen()
draw_2()
clear_screen()
draw_3()
clear_screen()
draw_4()
time.sleep(5)
# 弹窗设置
threads = []
for i in range(200): # 需要的弹框数量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.01)
threads[i].start()
三、源码分享
源码来喽:
# Time : 2022/2/14 12:21
# File : 情人节.py
# Author : 是Dream呀!
# VX : Xu18300396393
# 一万次悲伤,依然会有Dream,我一直在最温暖的地方等你!
import turtle
import time
import random
import tkinter as tk
import threading
# 实现清屏
def clear_screen():
turtle.penup() #画笔抬起
turtle.goto(0,0) #定位到(0,0)
turtle.color('white')
turtle.pensize(800) #画笔粗细
turtle.pendown() #画笔落下
turtle.setheading(0) #设置朝向
turtle.fd(300) #前进
turtle.bk(600) #后退
# 初始化海龟的位置
def go_start(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
#画线,state为真时海龟回到原点,为假时不回到原来的出发点
def draw_line(length, angle, state):
turtle.pensize(1)
turtle.pendown()
turtle.setheading(angle)
turtle.fd(length)
turtle.bk(length) if state else turtle.penup()
turtle.penup()
# 画出发射爱心的小人
def draw_people(x, y):
turtle.speed(0)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.pensize(2)
turtle.color('black')
turtle.setheading(0)
turtle.circle(35, 360)
turtle.penup()
turtle.pensize(3)
turtle.setheading(90)
turtle.fd(45)
turtle.setheading(180)
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.pendown()
turtle.circle(4, 360)
turtle.penup()
turtle.goto(x, y)
turtle.pensize(2)
turtle.setheading(0)
turtle.fd(20)
turtle.setheading(90)
turtle.fd(20)
turtle.setheading(-90)
turtle.pendown()
turtle.circle(5, 180)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(40)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(-60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(60)
turtle.setheading(-135)
turtle.fd(60)
turtle.bk(60)
turtle.setheading(-45)
turtle.fd(30)
turtle.setheading(-135)
turtle.fd(35)
turtle.penup()
# 画爱心
def draw_heart(size):
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(size)
turtle.circle(size * -3.745, 45)
turtle.circle(size * -1.431, 165)
turtle.left(120)
turtle.circle(size * -1.431, 165)
turtle.circle(size * -3.745, 45)
turtle.fd(size)
turtle.end_fill()
turtle.speed(0)
# 画箭羽
def draw_feather(size):
angle = 30 # 箭的倾角
feather_num = size // 6 # 羽毛的数量
feather_length = size // 3 # 羽毛的长度
feather_gap = size // 10 # 羽毛的间隔
for i in range(feather_num):
draw_line(feather_gap, angle + 180, False) # 箭柄,不折返
draw_line(feather_length, angle + 145, True) # 羽翼,要折返
draw_line(feather_length, angle + 145, False)
draw_line(feather_num * feather_gap, angle, False)
draw_line(feather_length, angle + 145 + 180, False)
for i in range(feather_num):
draw_line(feather_gap, angle + 180, False) # 箭柄,不折返
draw_line(feather_length, angle - 145, True) # 羽翼,要折返
draw_line(feather_length, angle - 145, False)
draw_line(feather_num * feather_gap, angle, False)
draw_line(feather_length, angle - 145 + 180, False)
# 画一箭穿心,最后箭的头没有画出来,用海龟来代替
def arrow_heart(x, y, size):
go_start(x, y, False)
draw_heart(size * 1.15)
turtle.setheading(-150)
turtle.penup()
turtle.fd(size * 2.2)
draw_heart(size)
turtle.penup()
turtle.setheading(150)
turtle.fd(size * 2.2)
turtle.color('black')
draw_feather(size)
turtle.pensize(4)
turtle.setheading(30)
turtle.pendown()
turtle.fd(size * 2)
turtle.penup()
turtle.setheading(29)
turtle.fd(size * 5.7)
turtle.color('black')
turtle.pensize(4)
turtle.pendown()
turtle.fd(size * 1.2)
#显示倒数3,2,1
def draw_0(i):
turtle.speed(0)
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-50, -100)
turtle.color('red')
write = turtle.write(i, font=('宋体', 200, 'normal'))
time.sleep(1)
# 显示文字
def draw_1():
turtle.penup()
turtle.hideturtle() #隐藏箭头显示
turtle.goto(-450, 0)
turtle.color('red')
write = turtle.write('宝贝,情人节快乐💕💕💕', font=('宋体', 60, 'normal'))
time.sleep(2)
# 显示发射爱心的小人儿
def draw_2():
turtle.speed(3)
draw_people(-250, 20)
turtle.penup()
turtle.goto(-150, -30)
draw_heart(14)
turtle.penup()
turtle.goto(-20, -60)
draw_heart(25)
turtle.penup()
turtle.goto(205, -100)
draw_heart(43)
turtle.hideturtle()
time.sleep(2)
def draw_3():
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-220, 50)
turtle.color('red')
write = turtle.write('💕宝贝', font=('宋体', 60, 'normal'))
turtle.penup()
turtle.goto(0, -50)
write = turtle.write('宝贝💕', font=('宋体', 60, 'normal'))
time.sleep(2)
# 显示一箭穿心
def draw_4():
turtle.speed(1)
turtle.penup()
turtle.goto(-410, -200)
turtle.color('pink')
turtle.pendown()
turtle.write('猫咪公主 狗王', font=('wisdom', 60, 'normal'))
turtle.speed(1)
turtle.penup()
turtle.color("red")
turtle.goto(-31, -220)
turtle.write('❤',font=('wisdom', 80, 'normal'))
arrow_heart(20, -60, 51)
turtle.showturtle()
def dow():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = random.randrange(0, width)
b = random.randrange(0, height)
window.title('宝贝!')
window.geometry("200x50" + "+" + str(a) + "+" + str(b))
tk.Label(window,
text='💕宝贝我爱你💕!', # 标签的文字
bg='pink', # 背景颜色
font=('..', 17), # 字体和字体大小
width=18, height=2 # 标签长宽
).pack() # 固定窗口位置
window.mainloop()
number=[3,2,1] #储存显示界面倒数数字1,2,3
if __name__ == '__main__':
turtle.setup(900, 500) #调画布的尺寸
for i in number:
draw_0(i)
clear_screen()
draw_1()
clear_screen()
draw_2()
clear_screen()
draw_3()
clear_screen()
draw_4()
time.sleep(5)
# 弹窗设置
threads = []
for i in range(200): # 需要的弹框数量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.01)
threads[i].start()
四、🎄美好祝愿🎄
希望大家在情人节都可以和喜欢的人在一起,送上那棵专属于她 or 他的专属礼物!天天开心,好好生活,我一直都在哟~💘💘💘(有任何问题的话,欢迎评论区或者私信我嗷,我都会看的!)
五、原工程文件分享
如果你实在是不懂 Python,那就直接下载这个压缩包就可以把,一键运行即可,需要自取:情人节表白神器exe文件+直接运行就可以
🌲🌲🌲 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!❤️❤️❤️如果你喜欢的话,就不要吝惜你的一键三连了~
是Dream呀
Python领域优质创作者 2021.03.30 加入
2021年度博客之星TOP100,2021年度领域TOP5 Python领域优质创作者,交流、合作、学习,欢迎私信我VX+++ 一万次悲伤,依然会有Dream,我一直在最温暖的地方等你!
评论