Python基于tkinter跳动的心

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

# 新建画布尺寸大小
CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
CANVAS_CENTER_X = CANVAS_WIDTH / 2
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2
IMAGE_ENLARGE = 8


# 定义心形函数
def heart_function(t):
    x = 16 * sin(t) ** 3
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

    y *= IMAGE_ENLARGE
    x *= IMAGE_ENLARGE

    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y

    return int(x), int(y)


# 内粒子散步
def scatter_inside(x, y, beta=0.15):
    ratiox = - beta * log(random.random())
    ratioy = - beta * log(random.random())
    dx = ratiox * (x - CANVAS_CENTER_X)
    dy = ratioy * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy


# 定义扰动函数
def shrink(x, y, ratio):
    force = 1 / ((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.5
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy


# 定义画心函数
class Heart:
    # 定义self
    def __init__(self):
        self._points = set()
        self._extra_points = set()
        self._inside = set()
        self.all_points = {}
        self.calc_position = shrink
        self.build(2000)

    # 创建心形点
    def build(self, number):
        for i in range(number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t)
            self._points.add((int(x), int(y)))

        for pos_x, pos_y in list(self._points):
            for j in range(3):
                x, y = scatter_inside(pos_x, pos_y, 0.2)
                self._extra_points.add((x, y))

            # inside点
        point_list = list(self._points)
        for k in range(4000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y)
            self._inside.add((int(x), int(y)))

    @staticmethod
    # 计算心形点
    def calc_position(x, y, ratio):
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.5)
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
        return x - dx, y - dy

    def calc(self, frame):
        calc_position = self.calc_position
        ratio = 10 * sin(frame / 10 * pi)

        all_points = []

        # outline 心形点
        for x, y in self._points:
            x, y = calc_position(x, y, ratio)
            size = 1
            all_points.append((x, y, size))

        # inner内圈点
        for x, y in self._extra_points:
            x, y = calc_position(x, y, ratio)
            size = random.randint(0, 2)
            all_points.append((x, y, size))

        for x, y in self._inside:
            x, y = calc_position(x, y, ratio)
            size = random.randint(0, 1)
            all_points.append((x, y, size))

        self.all_points[frame] = all_points

    # 画出心形点
    def render(self, canvas, frame):
        for x, y, size in self.all_points[frame % 20]:
            canvas.create_rectangle(
                x, y, x + size, y + size, width=0, fill="#ff7171")


# 定义动画函数
def draw(root: Tk, canvas: Canvas, heart: Heart, frame=0):
    canvas.delete('all')
    # heart.render(canvas)
    heart.render(canvas, frame)
    root.after(30, draw, root, canvas, heart, frame + 1)
    canvas.create_text((520, 360), text="for DJY~", font=("Monaco", 20))


# 主程序调用所有函数
if __name__ == '__main__':
    root = Tk()
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    # 创建字体
    canvas.pack()
    heart = Heart()
    for frame in range(20):
        heart.calc(frame)
    draw(root, canvas, heart)
    root.mainloop()

你可能感兴趣的:(Python,python,开发语言)