源码如下:
import turtle # 导入turtle库,用于图形绘制
import random # 导入random库,生成随机数
import math # 导入math库,进行数学计算
turtle.setup(1.0, 1.0) # 设置窗口大小为屏幕大小
turtle.title("流星雨动画") # 设置窗口标题
turtle.bgcolor('black') # 设置背景颜色为黑色
t = turtle.Turtle() # 创建一个画笔对象
t.hideturtle() # 隐藏画笔,不显示画布的形状
t.pensize(1) # 设置画笔的大小
# 定义流星的颜色列表
colors = ['gold', 'yellow', 'orange', 'green'] # 金色
class Meteor: # 定义流星类
def __init__(self): # 初始化方法,创建每颗流星时调用
self.r = random.randint(50, 100) # 随机生成流星的半径
self.k = random.uniform(2, 4) # 随机生成角度参数
self.x = random.randint(-1000, 1000) # 随机生成流星的x坐标
self.y = random.randint(-500, 500) # 随机生成流星的y坐标
self.speed = random.randint(5, 10) # 随机生成流星的移动速度
self.color = random.choice(colors) # 随机选择流星的颜色
def meteor(self): # 绘制流星的方法
# 移动画笔到指定的坐标位置处
t.penup()
t.goto(self.x, self.y)
t.pendown()
# 设置流星的颜色
t.begin_fill()
t.fillcolor(self.color)
# 开始绘制流星
t.setheading(-30) # 设置流星的朝向
t.right(self.k) # 根据随机角度右转
t.forward(self.r) # 沿直线前进一定长度
t.left(self.k) # 左转回到垂直方向
t.circle(self.r * math.sin(math.radians(self.k)), 180) # 绘制半圆弧
t.left(self.k) # 再次左转恢复角度
t.forward(self.r) # 沿直线前进相同长度以闭合流星形状
t.end_fill() # 结束填充
def move(self): # 更新流星位置的方法
if self.y >= -500: # 当流星的y坐标大于等于-500时
self.y -= self.speed # 减小流星y坐标的大小,将画笔向下移动
self.x += 2 * self.speed # 增加流星x坐标的大小,将画笔向右移动
else: # 当流星的y坐标小于-500时
self.r = random.randint(50, 100) # 重新设置流星的半径
self.k = random.uniform(2, 4) # 重新设置角度参数
self.x = random.randint(-2000, 1000) # 重新设置流星的x坐标
self.y = 500 # 重新设置流星的y坐标
self.speed = random.randint(5, 10) # 重新设置流星的速度
self.color = random.choice(colors) # 重新设置流星的颜色
# 创建一个流星列表,用来存储流星实例
Meteors = []
for i in range(100):
Meteors.append(Meteor())
# 进行无限循环,模拟流星雨动画
while True:
turtle.tracer(0) # 关闭tracer,提高性能
t.clear() # 清除画布内容
for i in range(100):
Meteors[i].move() # 更新每颗流星的位置
Meteors[i].meteor() # 重新绘制每颗流星
turtle.update() # 更新屏幕显示内容
源码如下:
import random
from math import sin, cos, pi, log
from tkinter import *
CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
CANVAS_CENTER_X = CANVAS_WIDTH / 2
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2
IMAGE_ENLARGE = 11
HEART_COLOR = "#FFC0CB" # ff2121
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
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):
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
def shrink(x, y, ratio):
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
def curve(p):
return 2 * (2 * sin(4 * p)) / (2 * pi)
class Heart:
def __init__(self, generate_frame=20):
self._points = set() # 原始爱心坐标集合
self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
self._center_diffusion_points = set() # 中心扩散效果点坐标集合
self.all_points = {} # 每帧动态点坐标
self.build(2000)
self.random_halo = 1000
self.generate_frame = generate_frame
for frame in range(generate_frame):
self.calc(frame)
def build(self, number):
for _ in range(number):
t = random.uniform(0, 2 * pi)
x, y = heart_function(t)
self._points.add((x, y))
for _x, _y in list(self._points):
for _ in range(3):
x, y = scatter_inside(_x, _y, 0.05)
self._edge_diffusion_points.add((x, y))
point_list = list(self._points)
for _ in range(4000):
x, y = random.choice(point_list)
x, y = scatter_inside(x, y, 0.17)
self._center_diffusion_points.add((x, y))
@staticmethod
def calc_position(x, y, ratio):
force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520) # 魔法参数
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, 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 render(self, render_canvas, render_frame):
for x, y, size in self.all_points[render_frame % self.generate_frame]:
render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
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)
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()
啊这个烟花有待优化啊,哈哈,丑版烟花5毛钱特效。
import turtle # 导入turtle库,用于图形绘制
import random # 导入random库,生成随机数
import math # 导入math库,进行数学计算
# 设置窗口大小和背景颜色
turtle.setup(1.0, 1.0)
turtle.title("烟花绽放动画")
turtle.bgcolor('black')
t = turtle.Turtle()
t.hideturtle()
t.pensize(1)
# 定义烟花的颜色列表
colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange']
class Firework:
def __init__(self):
self.x = random.randint(-400, 400)
self.y = random.randint(-300, 300)
self.color = random.choice(colors)
self.particles = []
self.exploded = False
self.lifetime = random.randint(50, 100)
self.create_particles()
def create_particles(self):
for _ in range(random.randint(50, 100)):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(1, 6)
dx = math.cos(angle) * speed
dy = math.sin(angle) * speed
self.particles.append([self.x, self.y, dx, dy])
def update(self):
if not self.exploded:
self.lifetime -= 1
if self.lifetime <= 0:
self.explode()
else:
for particle in self.particles:
particle[0] += particle[2]
particle[1] += particle[3]
particle[3] -= 0.1 # gravity effect
def explode(self):
self.exploded = True
def draw(self):
if not self.exploded:
t.penup()
t.goto(self.x, self.y)
t.dot(10, self.color) # 放大烟花点的大小
else:
for particle in self.particles:
t.penup()
t.goto(particle[0], particle[1])
t.dot(5, self.color) # 放大光粒的半径
# 创建一个烟花列表,用来存储烟花实例
fireworks = [Firework() for _ in range(5)]
# 进行无限循环,模拟烟花绽放动画
while True:
turtle.tracer(0) # 关闭tracer,提高性能
t.clear() # 清除画布内容
for firework in fireworks:
firework.update() # 更新每颗烟花的状态
firework.draw() # 重新绘制每颗烟花
turtle.update() # 更新屏幕显示内容
# 创建新的烟花实例以保持持续绽放效果
if random.random() < 0.1: # 控制新烟花出现的频率
fireworks.append(Firework())
# 移除已经爆炸并消失的烟花实例,防止内存泄漏
fireworks = [fw for fw in fireworks if not (fw.exploded and all(p[3] <= -1 for p in fw.particles))]
喜欢的可以关注一下我哦,后续更精彩。