Use Python to make Li Xun’s love code (with original code)

foreword

Recently, there is a drama that is very popular. It is the drama of the programmer. What is it called to warm you up?

Cough cough, I didn’t read much about the plot, but, there is a love code in it, which is quite popular recently, let’s try it with Python today

It's not bad to make this confession.

Ok, without further ado, let's start the code part

the code

module import

Original code. Click to receive [Remarks: Su]
import random
from math import sin, cos, pi, log
from tkinter import *

small details

CANVAS_WIDTH = 640  # 画布的宽
CANVAS_HEIGHT = 480  # 画布的高
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 11  # 放大比例
HEART_COLOR = "#ff8181"  # 心的颜色,芜湖我喜欢的粉色

Builder

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)

internal diffusion

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)
    return x - dx, y - dy

jitter


def shrink(x, y, ratio):
    """
    抖动
    :param x: 原x
    :param y: 原y
    :param ratio: 比例
    :return: 新坐标
    """
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy

Adjust beating period

def curve(p):
    """
    自定义曲线函数,调整跳动周期
    :param p: 参数
    :return: 正弦
    """
    # 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)
    return 2 * (2 * sin(4 * p)) / (2 * pi)

full code

It's too troublesome to write them out one by one

Original code. Click to receive [Remarks: Su]Please add a picture description

Ok, sharing is over here

If you have any questions about the article, or other questions about python, you can leave a message in the comment area or private message me. If you think the
article I shared is good, you can follow me or give the article a thumbs up (/≧▽≦)/

Guess you like

Origin blog.csdn.net/sunanpython/article/details/128302315
Recommended