欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > Python画图3个小案例之“一起看流星雨”、“爱心跳动”、“烟花绚丽”

Python画图3个小案例之“一起看流星雨”、“爱心跳动”、“烟花绚丽”

2024/11/5 19:22:31 来源:https://blog.csdn.net/2301_79810514/article/details/143336805  浏览:    关键词:Python画图3个小案例之“一起看流星雨”、“爱心跳动”、“烟花绚丽”

在这里插入图片描述
源码如下:

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"  # ff2121def 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_ratioy *= shrink_ratiox += CANVAS_CENTER_Xy += CANVAS_CENTER_Yreturn 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 - dydef 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 - dydef 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 = 1000self.generate_frame = generate_framefor 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))@staticmethoddef 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 - dydef 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_pointsdef 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()  # 一个Tkcanvas = 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 = Falseself.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) * speeddy = math.sin(angle) * speedself.particles.append([self.x, self.y, dx, dy])def update(self):if not self.exploded:self.lifetime -= 1if self.lifetime <= 0:self.explode()else:for particle in self.particles:particle[0] += particle[2]particle[1] += particle[3]particle[3] -= 0.1  # gravity effectdef explode(self):self.exploded = Truedef 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))]

喜欢的可以关注一下我哦,后续更精彩。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com