💌 Python 表白神器:一行命令,满屏爱心 + 打字机情话

147 阅读1分钟

微信图片_20251014151033_10_20.jpg

💻 源码:love.py(30 行)

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()

🚀 运行方式

python love.py

支持 Windows / macOS / Linux 终端
Ctrl+C 随时退出


🌈 可自定义玩法

# 换爱心颜色
heart = "\033[35m💖\033[0m"  # 紫色

# 换文字
msg = "余生请多指教,我的小太阳🌞"

# 换速度
time.sleep(0.05)  # 更快打字

📦 彩蛋进阶(可选)

  1. 语音朗读(需安装库)
pip install pyttsx3
import pyttsx3
engine = pyttsx3.init()
engine.say(msg); engine.runAndWait()
  1. 打包成 exe
pip install pyinstaller
pyinstaller -F love.py
# dist/love.exe 直接发给 TA!