Python动态粒子爱心

发布于:2024-12-18 ⋅ 阅读:(51) ⋅ 点赞:(0)

效果展示

**在这里插入图片描述
在这里插入图片描述

代码介绍

**

  1. 初始化和设置
    pygame.init(): 初始化 Pygame 库,使其准备好使用。

屏幕设置: 使用 pygame.display.set_mode() 创建一个 1200x800 像素的窗口,标题为 “Panda”。

  1. 图标和颜色
    图标设置: 加载并设置窗口图标。

颜色定义: 定义了一组颜色,以用于粒子。

  1. 字体加载
    使用 pygame.font.Font() 加载字体,用于显示粒子和文本。

  2. 坐标转换
    screen_x 和 screen_y 函数: 将逻辑坐标转换为屏幕坐标,使得原点在屏幕中心。

  3. 随机数生成
    creat_random 函数: 生成给定范围内的随机整数。

  4. 数据创建
    creat_data 函数: 生成粒子和心形曲线的坐标。

计算心形方程的点并存储在 origin_points 列表中。

为每个粒子随机选择起始位置和目标位置,并为粒子分配颜色和速度。

  1. 主循环
    事件处理: 处理窗口关闭事件。

粒子运动: 根据速度将每个粒子逐渐移动到其目标位置,并在屏幕上绘制粒子。

文本显示: 当所有粒子到达目标位置后,显示 “Leaf” 文本。

  1. 渲染和刷新
    使用 pygame.display.flip() 刷新屏幕,以显示更新后的内容。

帧率控制: 使用 clock.tick(60) 控制循环的帧率为 60 FPS。

**

部分源码

#源码GongzhongHao:PandaYY回复1005
import pygame
import random
import math

# 初始化 pygame
pygame.init()

# 常量定义
xScreen = 1200
yScreen = 800
PI = 3.1426535159
averag_distance = 0.162
particles = 10000  # 初始粒子数量

# 设置屏幕
screen = pygame.display.set_mode((xScreen, yScreen))
pygame.display.set_caption("Panda")
# 加载并设置图标
icon_img = pygame.image.load('logo.gif')  # 替换为你的图标文件路径
pygame.display.set_icon(icon_img)  # 设置窗口图标

# 颜色定义
colors = [(0, 0, 255), (252, 222, 250), (5, 14, 255), (1, 55, 255), (0, 0, 255), (0, 0, 255), (0, 0, 255)]


font_size = 5  # 粒子大小
font = pygame.font.Font(None, font_size)
text_font = pygame.font.Font(None, 100)

def screen_x(x):
    return int(x + xScreen / 2)

def screen_y(y):
    return int(-y + yScreen / 2)

def creat_random(x1, x2):
    return random.randint(int(x1), int(x2))

def creat_data():
    origin_points = []
    points = []

    # 生成心形数据点
    x1, y1 = 0, 0
    for radian in range(1, int(2 * PI * 1000) + 1):
        radian /= 1000
        x2 = 16 * math.pow(math.sin(radian), 3)
        y2 = 13 * math.cos(radian) - 5 * math.cos(2 * radian) - 2 * math.cos(3 * radian) - math.cos(4 * radian)
        distance = math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
        if distance > averag_distance:
            x1, y1 = x2, y2
            origin_points.append((x2, y2))

    # 生成随机点数据
    for _ in range(particles):
        x = creat_random(-xScreen // 2, xScreen // 2)
        y = creat_random(-yScreen // 2, yScreen // 2)
        target = origin_points[creat_random(0, len(origin_points) - 1)]
        points.append({
            'color': colors[creat_random(0, 6)],
            'x': x,
            'y': y,
            'target_x': target[0] * 20,  # 扩大目标位置
            'target_y': target[1] * 20,
            'speed': creat_random(1, 3) / 1.0
        })

    return points, origin_points

def main():
    points, origin_points = creat_data()

    clock = pygame.time.Clock()
    running = True
    show_text = False
    text_displayed = False

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        all_particles_at_target = all(
            (abs(point['x'] - point['target_x']) < point['speed'] and abs(point['y'] - point['target_y']) < point['speed'])
            for point in points
        )




网站公告

今日签到

点亮在社区的每一天
去签到