Python知識分享網(wǎng) - 專業(yè)的Python學(xué)習(xí)網(wǎng)站 學(xué)Python,上Python222
Python浪漫520表白代碼實現(xiàn)
發(fā)布于:2023-07-11 15:28:55

前言
520是每年的5月20日,因數(shù)字“520”與“我愛你”發(fā)音相似而被許多年輕人用作表達(dá)愛意的節(jié)日。這個節(jié)日起源于中國互聯(lián)網(wǎng)文化,逐漸傳遞到其他國家和地區(qū)。在這一天,情侶們通常會互送禮物、發(fā)表情、或者舉行浪漫的活動來慶祝愛情??靵眍I(lǐng)取專屬于程序員的浪漫吧!

表白界面
讓人無法拒絕的表白界面!
Python浪漫520表白代碼實現(xiàn) 圖1

 

程序設(shè)計 

 

import tkinter as tk
import tkinter.messagebox
root = tk.Tk()
root.title('?')
root.resizable(0, 0)
root.wm_attributes("-toolwindow", 1)
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
widths = 300
heights = 100
x = (screenwidth - widths) / 2
y = (screenheight - heights) / 2
root.geometry('%dx%d+%d+%d' % (widths, heights, x, y))  # 設(shè)置在屏幕中居中顯示
tk.Label(root, text='親愛的,做我女朋友好嗎?', width=37, font=('宋體', 12)).place(x=0, y=10)
 
 
def OK():  # 同意按鈕
    root.destroy()
    # 同意后顯示漂浮愛心
 
 
def NO():  # 拒絕按鈕,拒絕不會退出,必須同意才可以退出哦~
    tk.messagebox.showwarning('?', '再給你一次機(jī)會!')
 
 
def closeWindow():
    tk.messagebox.showwarning('?', '逃避是沒有用的哦')
 
 
tk.Button(root, text='好哦', width=5, height=1, command=OK).place(x=80, y=50)
tk.Button(root, text='不要', width=5, height=1, command=NO).place(x=160, y=50)
root.protocol('WM_DELETE_WINDOW', closeWindow)  # 綁定退出事件
root.mainloop()

 

數(shù)據(jù)結(jié)構(gòu)與算法之美 王爭 PDF 下載 圖2

 

跳動的愛心

去年爆火的跳動的愛心!

 

跳動的愛心 去年爆火的跳動的愛心!圖3

 

主要的愛心類 

 

class Heart:
    def __init__(self, generate_frame=20):
        self._points = set()  # 原始愛心坐標(biāo)集合
        self._edge_diffusion_points = set()  # 邊緣擴(kuò)散效果點坐標(biāo)集合
        self._center_diffusion_points = set()  # 中心擴(kuò)散效果點坐標(biāo)集合
        self.all_points = {}  # 每幀動態(tài)點坐標(biāo)
        self.build(2000)
        self.random_halo = 1000
        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)
    def build(self, number):
        for _ in range(number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t)
            self._points.add((x, y))
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))
        point_list = list(self._points)
        for _ in range(4000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.17)
            self._center_diffusion_points.add((x, y))
    @staticmethod
    def calc_position(x, y, ratio):
        force = 1 / (((x - heartx) ** 2 + (y - hearty) ** 2) ** 0.520)  # 魔法參數(shù)
        dx = ratio * force * (x - heartx) + random.randint(-1, 1)
        dy = ratio * force * (y - hearty) + random.randint(-1, 1)
        return x - dx, y - dy
    def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)  # 圓滑的周期的縮放比例
        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
        all_points = []
        heart_halo_point = set()
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t, shrink_ratio=11.6)
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                heart_halo_point.add((x, y))
                x += random.randint(-14, 14)
                y += random.randint(-14, 14)
                size = random.choice((1, 2, 2))
                all_points.append((x, y, size))
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
        self.all_points[generate_frame] = all_points
    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=heartcolor)

 

數(shù)據(jù)結(jié)構(gòu)與算法之美 王爭 PDF 下載  圖4

 

漂浮的愛心

當(dāng)然啦,漂浮的愛心也很美!

漂浮的愛心 當(dāng)然啦,漂浮的愛心也很美! 圖5

 

主要的愛心類 

 

class Heart():    #每個愛心(愛心類)
    def __init__(self):
        self.r = ra.randint(10,15)        #愛心的半徑
        self.x = ra.randint(-1000,1000)   #愛心的橫坐標(biāo)
        self.y = ra.randint(-500,500)     #愛心的縱坐標(biāo)
        self.f = ra.uniform(-3.14,3.14)   #愛心左右移動呈正弦函數(shù)
        self.speed = ra.randint(5,10)     #愛心移動速度
        self.color = ra.choice(colors)    #愛心的顏色
        self.outline = 1                  #愛心的外框大?。刹灰?    def move(self):                    #愛心移動函數(shù)
        if self.y <= 500:            #當(dāng)愛心還在畫布中時
            self.y += self.speed     #設(shè)置上下移動速度
            self.x += self.speed * math.sin(self.f)    #設(shè)置左右移動速度
            self.f += 0.1            #可以理解成標(biāo)志,改變左右移動的方向
        else:                        #當(dāng)愛心漂出了畫布時,重新生成一個愛心
            self.r = ra.randint(10,15)
            self.x = ra.randint(-1000,1000)
            self.y = -500
            self.f = ra.uniform(-3.14,3.14)
            self.speed = ra.randint(5,10)
            self.color = ra.choice(colors)
            self.outline = 1
    def draw(self):       #畫愛心函數(shù),就是用turtle畫愛心
        t.pensize(self.outline)
        t.penup()
        t.color(self.color)
        t.goto(self.x, self.y)
        t.pendown()
        t.begin_fill()
        t.fillcolor('pink')
        t.setheading(120)
        t.circle(self.r, 195)
        t.fd(self.r * 2.4)
        t.lt(90)
        t.fd(self.r * 2.4)
        t.circle(self.r, 195)
        t.end_fill()

 

Python浪漫520表白代碼實現(xiàn) 圖6

 

滿屏表白代碼

誰能拒絕滿屏的表白代碼呢!

滿屏表白代碼 誰能拒絕滿屏的表白代碼呢! 圖7

 

主要的函數(shù) 

 

def Love():
    root=tk.Tk()
    width=200
    height=50
    screenwidth=root.winfo_screenwidth()
    screenheight=root.winfo_screenheight()
    x=ra.randint(0,screenwidth)
    y=ra.randint(0,screenheight)
    root.title("?")
    root.geometry("%dx%d+%d+%d"%(width,height,x,y))
    tk.Label(root,text='I LOVE YOU!',fg='white',bg='pink',font=("Comic Sans MS",15),width=30,height=5).pack()
    root.mainloop()
def Heart():
    root=tk.Tk()
    screenwidth=root.winfo_screenwidth()
    screenheight=root.winfo_screenheight()
    width=600
    height=400
    x=(screenwidth-width)//2
    y=(screenheight-height)//2
    root.title("?")
    root.geometry("%dx%d+%d+%d"%(screenwidth,screenheight,0,0))
    tk.Label(root,text='?',fg='pink',bg='white',font=("Comic Sans MS",500),width=300,height=20).pack()
    root.mainloop()

 

Python浪漫520表白代碼實現(xiàn) 圖8

 

 

轉(zhuǎn)載自:https://blog.csdn.net/m0_68111267/article/details/130666881