
💻 源码:love.py(30 行)
python
import os, time, random, sys
def heart_screen():
rows, cols = os.get_terminal_size()
heart = "💖"
while True:
print("".join(random.choices([heart, " "], weights=[1, 3]) * cols))
time.sleep(0.08)
def type_writer(text):
for ch in text:
sys.stdout.write(ch)
sys.stdout.flush()
time.sleep(0.08)
def main():
# 1. 满屏爱心雨(后台线程)
import threading
t = threading.Thread(target=heart_screen, daemon=True)
t.start()
# 2. 打字机情话
time.sleep(2) # 让爱心飞一会
msg = "我想把全世界的温柔都给你,也包括我自己。❤️"
type_writer(msg)
# 3. 彩蛋:等待回应
input("\n\n👉 你愿意吗?(按 Enter 接受💕)")
if __name__ == "__main__":
main()
🚀 运行方式
bash
python love.py
支持 Windows / macOS / Linux 终端
按
Ctrl+C随时退出
🌈 可自定义玩法
python
# 换爱心颜色
heart = "\033[35m💖\033[0m" # 紫色
# 换文字
msg = "余生请多指教,我的小太阳🌞"
# 换速度
time.sleep(0.05) # 更快打字
📦 彩蛋进阶(可选)
- 语音朗读(需安装库)
bash
pip install pyttsx3
python
import pyttsx3
engine = pyttsx3.init()
engine.say(msg); engine.runAndWait()
- 打包成 exe
bash
pip install pyinstaller
pyinstaller -F love.py
# dist/love.exe 直接发给 TA!