Python粒子效果爱心代码注释

# Beating Heart
# default input
import random
from math import sin, cos, pi, log
from tkinter import *

CANVAS_WIDTH = 980 # 画布高度
CANVAS_HEIGHT = 720 # 画布宽度
CANVAS_CENTER_X = CANVAS_WIDTH / 2 # 画布中心点x轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # 画布中心点x轴坐标
IMAGE_ENLARGE = 11 # 放大比例
# color list
HEART_COLOR_LIST = ["#d974ff", "#be77fa", "#a478f3", "#8b78ea", "#7377e0",
"#4871c6", "#5c74d3", "#fa6ea9", "#dc6db1", "#ec2c2c",
"#e91e41", "#8b4593", "#2bd3ec", "#00be93", "#2bec62"]


def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
"""
生成爱心
:param shrink_ratio: ratio
:param t: parameter
:return: x, y
"""
# 基础爱心生成函数
x = 16 * (sin(t) ** 3)
y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

# zoom
x *= shrink_ratio
y *= shrink_ratio

# center
x += CANVAS_CENTER_X
y += CANVAS_CENTER_Y

return int(x), int(y)


def scatter_inside(x, y, beta=1.15):
"""
random inner spreading
:param x: orig x
:param y: orig y
:param beta:

Python粒子3D爱心代码可以通过使用Python中的Pygame和PyOpenGL库来实现。具体步骤如下: 1. 导入所需库:Pygame和PyOpenGL库。 2. 创建窗口并设置参数。 3. 定义粒子类,包含粒子的位置、速度、大小等信息。 4. 创建粒子群并初始化,包含随机位置、速度和大小。 5. 定义粒子运动轨迹,即根据速度和位置更新粒子的位置。 6. 在主循环中绘制粒子,并根据运动轨迹更新粒子的位置。 7. 添加交互功能,例如通过鼠标移动控制粒子运动等。 以下是一段简单的Python粒子3D爱心代码示例: ```python import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * import random class Particle: def __init__(self): self.pos = [random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1)] self.vel = [random.uniform(-0.01, 0.01), random.uniform(-0.01, 0.01), random.uniform(-0.01, 0.01)] self.size = random.uniform(0.005, 0.015) def move(self): self.pos += self.vel self.pos += self.vel self.pos += self.vel if abs(self.pos) > 1: self.vel *= -1 if abs(self.pos) > 1: self.vel *= -1 if abs(self.pos) > 1: self.vel *= -1 def main(): pygame.init() display = (800, 600) pygame.display.set_mode(display, DOUBLEBUF|OPENGL) glClearColor(0, 0, 0, 1) glMatrixMode(GL_PROJECTION) gluPerspective(45, (display/display), 0.1, 50.0) glMatrixMode(GL_MODELVIEW) glEnable(GL_DEPTH_TEST) particles = [Particle() for i in range(200)] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) glBegin(GL_POINTS) for p in particles: glColor3f(1, 0, 0) glVertex3fv(p.pos) p.move() glEnd() pygame.display.flip() pygame.time.wait(10) if __name__ == '__main__': main() ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值