[Qixi Festival Special] Use python to realize beating love + love electrocardiogram + roses +...

Preface

What? ? Tomorrow is Chinese Valentine's Day? After learning python for so long, you still can’t write a confession code to your partner or crush?

Insert image description here

I have to prepare a few for you.
There are the beating hearts that were popular last year, and there are other ones too. Come take a look and choose. After receiving the code, don’t forget to send it to others.

Insert image description here

emmm, let me show you the effect first.

To get the code, click on the business card at the end of the article~

Insert image description here

There are some others that I won’t go into here, but you can get them directly from the business card at the end of the article.

Insert image description here

Insert image description here

Love beating code

Please add image description

code

import random
from math import sin, cos, pi, log
from tkinter import *

CANVAS_WIDTH = 640  # 画布的宽
CANVAS_HEIGHT = 480  # 画布的高

CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标

HEART_COLOR = "#ff8181"  # 心的颜色,芜湖我喜欢的粉色

def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    """
    “爱心函数生成器”
    :param shrink_ratio: 放大比例
    :param t: 参数
    :return: 坐标
    """
    # 基础函数
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

# 放大
    x *= shrink_ratio
    y *= shrink_ratio

    # 移到画布中央
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y

    return int(x), int(y)

def scatter_inside(x, y, beta=0.15):
    """
    随机内部扩散
    :param x: 原x
    :param y: 原y
    :param beta: 强度
    :return: 新坐标
    """
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())

    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)
python学习交流Q群:770699889 ### 源码领取
    return x - dx, y - dy

    """
    抖动
    :param x: 原x
    :param y: 原y
    :param ratio: 比例
    :return: 新坐标
 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 draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
python学习交流Q群:770699889 ### 源码领取

if __name__ == '__main__':
    root = Tk()  # 一个Tk
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()  # 心
    draw(root, canvas, heart)  # 开始画画~
    root.mainloop()

Complete code

[Business card acquisition code at the end of the article]

Insert image description here

Insert image description here

love electrocardiogram

code

Import module

import os
import pygame
import turtle as t
t.title("Python学习交流Q群:770699889")

canvas size

#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.hideturtle()

draw hearts

def heart(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color('pink')
    t.setheading(50)
    t.circle( -5, 180)
    t.circle( -45, 12)
    t.setheading(130)
    t.circle( -45, 12)
    t.circle( -5, 180)
heart(-30, 155)
heart(-220, 145)
heart(-210, 60)
heart(-100, 100)
heart(-20, 20)
heart(-70, 130)
heart(-140, -20)
heart(30, 100)
heart(-60, -20)
heart(10, 60)
heart(-100, -70)
heart(20, 145)
heart(-140, -20)
heart(-130, 130)
heart(-180, 20)
heart(-170, 155)
heart(-230, 100)
def write_mes(x, y, size, ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('black')
    t.write(ss, font=('Times New Roman', size, 'normal'))

draw red hearts

print('画红心')
def heart_fill(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color('red', 'red')
    t.begin_fill()
    t.setheading(50)
    t.circle( -5, 180)
    t.circle( -45, 12)
    t.setheading(130)
    t.circle( -45, 12)
    t.circle( -5, 180)
    t.end_fill()
x = 90
y = 110

The '1' '2' '3' '4' '5' on the right side of the heart
can be changed to what you want to say.

write_mes(x, y, 11, '1')
heart_fill(-100, 100)
heart_fill(-70, 130)
heart_fill(-30, 155)
heart_fill(20, 145)
heart_fill(30, 100)
write_mes(x, y-30, 11, '2')
heart_fill(10, 60)
heart_fill(-20, 20)
heart_fill(-60, -20)
heart_fill(-100, -70)

Heart on the left

write_mes(x, y-30*2, 11, '3')
heart_fill(-140, -20)
heart_fill(-180, 20)
heart_fill(-210, 60)
heart_fill(-230, 100)
write_mes(x, y-30*3, 11, '4')
heart_fill(-220, 145)
heart_fill(-170, 155)
heart_fill(-130, 130)
write_mes(x, y-30*4, 11, '5')
t.speed(200)

draw heart line

t.penup()
t.goto(-170, 40)
t.pendown()
t.pencolor('red')
t.setheading(0)
t.pensize(2)
t.forward(10)

first small wave

t.setheading(45)
t.circle(50, 10)
t.setheading(0)
t.circle(-3,90)
t.circle(50, 5)

horizontal line

t.setheading(0)
t.forward(10)

first lower peak

t.setheading(-80)
t.forward(7)
t.setheading(70)
t.forward(25)
t.setheading(-85)
t.forward(29)
t.setheading(70)
t.forward(13)
t.setheading(0)
t.forward(15)

draw heart

t.setheading(150)
t.circle(-20, 40)
t.circle(-10, 170)
t.setheading(70)
t.circle(-10, 170)
t.circle(-20, 40)
t.setheading(0)
t.forward(15)

Write the names of two people.
Dangdang~ This is where you write the names of the two people in the heart.

    write_name(-180, 70, 11, '小圆')
    write_name(-180, 70, 11, '小圆')
    write_name(-180, 70, 11, '小圆')
    heart_bit()
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back2()
while 1:
    name_heart_bit()

roses

Insert image description here

code

module

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.font_manager import FontProperties
  • Change the perspective of drawing the image, that is, the position of the camera, azim rotates along the z-axis, and elev along the y-axis

The text content in the middle can be customized and modified

fig = plt.figure(figsize=(6, 8))
ax = fig.gca(projection='3d')
elev = 22
azim = 2.5
ax.view_init(elev, azim)  # 改变绘制图像的视角,即相机的位置,azim沿着z轴旋转,elev沿着y轴
font_set = FontProperties(fname=r"C:\Windows\Fonts\simhei.TTF", size=20)
ax.text(1, -0.8, 0, '"唯一的花送给我爱的宝贝"', fontproperties=font_set)

pedicel

  • Divide the circle into 50 equal parts according to the angle
u2 = np.linspace(0, 2 * np.pi, 50)
  • Divide height 1 equally into 20 parts, the height range of rose stems
h2 = np.linspace(0, 4, 20)  
  • xy values ​​repeated 20 times
x2 = np.outer(0.05 * np.sin(u2), np.ones(len(h2)))  
y2 = np.outer(0.05 * np.cos(u2), np.ones(len(h2)))  
  • height corresponding to x, y
z2 = np.outer(np.ones(len(u2)), h2)  

Complete code

[Click on the business card at the end of the article to obtain it directly]

Insert image description here

at last

The article sharing ends here. I hope it will be helpful to everyone ~

Insert image description here

Guess you like

Origin blog.csdn.net/aliYz/article/details/132412203