天天看點

七夕,用python表白的各種姿勢

一眨眼又到了七夕,這個古老又神聖的日子。小浪同舉國上下無數名艱苦的程式員一樣,一頭紮入了一場由"女神"這種生物引起的戰争。隻不過,在戰歌的幫助下,指尖浪得飛起。

如何用python優雅地對女神表白呢?

代碼和程式位址:https://github.com/ADlead/Qixi.git

1. 一行代碼

使用print函數,最簡單的真誠

print('\n'.join([''.join([('ILOVEYOU '[(x - y) % 8] if (
        (x * 0.05) ** 2 + (y * 0.1) ** 2 - 1) ** 3 - (x * 0.05) ** 2 * (
        y * 0.1) ** 3 <= 0 else ' ') for x in range(-30, 30)]) for y in range(15, -15, -1)]))
           

直接運作,可得結果

七夕,用python表白的各種姿勢

這個方法簡單直接,缺點是技能比較低階,對進階的女神來說殺傷力不夠。那麼,要怎麼辦呢?

2. 手動繪心

既然以靜制動無法取勝,那就讓python來手動繪制一顆心

import turtle
import math
heart = turtle.Screen()
heart.setworldcoordinates(-7, -7, 7, 7) # set size
alex = turtle.Turtle()
alex.color("purple")  # set color
alex.pensize(7)
alex.penup()
alex.speed(7)
walkStart = -1
walkEnd = 1
i = walkStart
j = walkEnd
while i <= 0 and j >= 0:
    y1 = math.sqrt(1 - i * i) + (i * i) ** (1/4.0)
    y2 = -math.sqrt(1 - i * i) + (i * i) ** (1/4.0)
    y3 = math.sqrt(1 - j * j) + (j * j) ** (1/4.0)
    y4 = -math.sqrt(1 - j * j) + (j * j) ** (1/4.0)
    alex.setx(i)
    alex.sety(y1)
    alex.dot()
    alex.sety(y2)
    alex.dot()
    alex.setx(j)
    alex.sety(y3)
    alex.dot()
    alex.sety(y4)
    alex.dot()
    # adjust density
    i += 0.07
    j -= 0.07
heart.exitonclick()
           

運作效果

七夕,用python表白的各種姿勢

與第一個方法相比,這個顯得更加浪漫,但是感覺還是不夠猛啊,似乎也仍可能會被女神拒絕。

3. 表白程式

那就來一個讓女神無法拒絕的程式,這也是曾經在dou音上很火的一個表白程式…

from tkinter import *
from tkinter import messagebox
import random

def no_close():
    return

def close_all_windows():
    window.destroy()
# cancel tips
def close_windows():
    messagebox.showinfo(title="不要嘛~", message="不選喜歡不許走!")
# “喜歡”window
def Love():
    love = Toplevel(window)
    love.geometry("300x100+580+250")
    love.title("愛你麼麼哒~")
    btn = Button(love, text="那我們在一起吧!", width=15, height=2, command=close_all_window)
    btn.place(x=100, y=30)
    love.protocol("WM_DELETE_WINDOW", no_close)

def on_enter(e):
    global pos
    dx = random.randint(100, 200)
    dy = random.randint(100, 300)
    print(pos, dx, dy)
    pos = (pos[0] + dx) % 200, (pos[1] - 250 + dy) % 350 + 250
    btn2.place(x=pos[0], y=pos[1])

window = Tk()
window.title("嗨,女神~")  # set title
window.geometry("360x640+550+50")  # set size
window.protocol("WM_DELETE_WINDOW", close_windows)  # close window
label = Label(window, text="喜歡你很久了", font=("微軟雅黑", 18))
label.place(x=120, y=50)
label = Label(window, text="你喜歡我嗎?", font=("微軟雅黑", 24))
label.place(x=70, y=100)
btn1 = Button(window, text="喜歡", width=15, height=2, command=Love)
btn1.place(x=110, y=200)
# "不喜歡" button
pos = [110, 300]
btn2 = Button(window, text="不喜歡", width=15, height=2)
btn2.place(x=pos[0], y=pos[1])

btn2.bind("<Enter>", on_enter)
# show window and keep running
window.mainloop()
           

通過pyinstaller把程式打包成exe之後,把程式發給小姐姐,讓她運作

表白的形式各種各樣,最重要的還是要有心意,有心才能打動對方。

浪完了,我也該去花店買一束花,送給我心中的那個小姐姐了。