2023年的最后时刻,希望小伙伴们都能找到属于自己的答案,找到自己激情的源泉,找到自己成长的机会。

2024即将到来,快学会这个烟花代码送给自己吧!

新年福利:基于python的电子烟花实现

import pygame as pg

import random as ra

import math

pg.init()

pg.display.set_caption("")

winScreen = pg.display.Info()

screenWidth = winScreen.current_w

screenHeight = winScreen.current_h

vector = pg.math.Vector2

trail_colors = [(45, 45, 45), (60, 60, 60), (75, 75, 75), (125, 125, 125), (150, 150, 150)]

# 烟花类

class Firework:

def __init__(self):

# 随机生成颜色

self.colour = (ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255))

# 随机生成三种颜色

self.colours = (

(ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255)),

(ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255)),

(ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255))

)

# 生成一个表示发射出的火花的粒子对象

self.firework = Particle(ra.randint(0,screenWidth), screenHeight, True, self.colour)

# 初始化爆炸状态为 False

self.exploded = False

self.particles = []

# 爆炸产生的粒子数量范围

self.min_max_particles = vector(666, 999)

def update(self, win):

g = vector(0, ra.uniform(0.15, 0.4))

if not self.exploded:

# 给发射出的火花施加重力

self.firework.apply_force(g)

self.firework.move()

for tf in self.firework.trails:

tf.show(win)

self.show(win)

if self.firework.vel.y >= 0:

self.exploded = True

self.explode()

else:

for particle in self.particles:

# 给爆炸产生的粒子施加随机力

particle.apply_force(vector(g.x + ra.uniform(-1, 1) / 20, g.y / 2 + (ra.randint(1, 8) / 100)))

particle.move()

for t in particle.trails:

t.show(win)

particle.show(win)

def explode(self):

amount = ra.randint(int(self.min_max_particles.x), int(self.min_max_particles.y))

for i in range(amount):

# 在爆炸位置生成粒子对象并添加到粒子列表中

self.particles.append(Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours))

def show(self, win):

# 绘制发射出的火花

pg.draw.circle(win, self.colour, (int(self.firework.pos.x), int(self.firework.pos.y)), self.firework.size)

def remove(self):

if self.exploded:

for p in self.particles:

if p.remove is True:

self.particles.remove(p)

if len(self.particles) == 0:

return True

else:

return False

# 粒子类

class Particle:

def __init__(self, x, y, firework, colour):

self.firework = firework

self.pos = vector(x, y)

self.origin = vector(x, y)

self.radius = 25

self.remove = False

self.explosion_radius = ra.randint(15, 25)

self.life = 0

self.acc = vector(0, 0)

self.trails = []

self.prev_posx = [-10] * 10

self.prev_posy = [-10] * 10

if self.firework:

self.vel = vector(0, -ra.randint(17, 20))

self.size = 5

self.colour = colour

for i in range(5):

self.trails.append(Trail(i, self.size, True))

else:

self.vel = vector(ra.uniform(-1, 1), ra.uniform(-1, 1))

self.vel.x *= ra.randint(7, self.explosion_radius + 2)

self.vel.y *= ra.randint(7, self.explosion_radius + 2)

self.size = ra.randint(2, 4)

self.colour = ra.choice(colour)

for i in range(5):

self.trails.append(Trail(i, self.size, False))

def apply_force(self, force):

# 施加力

self.acc += force

def move(self):

if not self.firework:

# 爆炸产生的粒子减速

self.vel.x *= 0.8

self.vel.y *= 0.8

self.vel += self.acc

self.pos += self.vel

self.acc *= 0

if self.life == 0 and not self.firework:

# 判断是否超出爆炸半径

distance = math.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2)

if distance > self.explosion_radius:

self.remove = True

self.decay()

self.trail_update()

self.life += 1

def show(self, win):

# 绘制粒子

pg.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)), self.size)

def decay(self):

if 50 > self.life > 10:

ran = ra.randint(0, 30)

if ran == 0:

self.remove = True

elif self.life > 50:

ran = ra.randint(0, 5)

if ran == 0:

self.remove = True

def trail_update(self):

self.prev_posx.pop()

self.prev_posx.insert(0, int(self.pos.x))

self.prev_posy.pop()

self.prev_posy.insert(0, int(self.pos.y))

for n, t in enumerate(self.trails):

if t.dynamic:

t.get_pos(self.prev_posx[n + 1], self.prev_posy[n + 1])

else:

t.get_pos(self.prev_posx[n + 5], self.prev_posy[n + 5])

# 痕迹类

class Trail:

def __init__(self, n, size, dynamic):

self.pos_in_line = n

self.pos = vector(-10, -10)

self.dynamic = dynamic

if self.dynamic:

self.colour = trail_colors[n]

self.size = int(size - n / 2)

else:

self.colour = (255, 255, 200)

self.size = size - 2

if self.size < 0:

self.size = 0

def get_pos(self, x, y):

self.pos = vector(x, y)

def show(self, win):

# 绘制痕迹

pg.draw.circle(win, self.colour, (int(self.pos.x), int(self.pos.y)), self.size)

def update(win, fireworks):

for fw in fireworks:

fw.update(win)

if fw.remove():

fireworks.remove(fw)

pg.display.update()

def fire():

screen = pg.display.set_mode((screenWidth, screenHeight - 66))

clock = pg.time.Clock()

fireworks = [Firework() for i in range(2)]

running = True

# 加载字体

font = pg.font.SysFont("comicsansms", 99)

# 渲染文本

text = "Happy New Year!"

text_color = (255, 190, 200) # 字体颜色

rendered_text = font.render(text, True, text_color)

while running:

clock.tick(99)

for event in pg.event.get():

if event.type == pg.QUIT:

running = False

# 计算文本位置

text_width = rendered_text.get_width()

text_height = rendered_text.get_height()

text_x = (screenWidth - text_width) // 2

text_y = (screenHeight - text_height) // 2 - 99

screen.fill((20, 20, 30))

# 绘制文本

screen.blit(rendered_text, (text_x, text_y))

if ra.randint(0, 10) == 1:

fireworks.append(Firework())

update(screen, fireworks)

pg.quit()

quit()

if __name__ == "__main__":

fire()

友情提示:记得安装pygame库

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygame

烟花效果:

以上就是今天的全部内容分享,觉得有用的话欢迎点赞收藏哦!

Python经验分享

学好 Python 不论是用于就业还是做副业赚钱都不错,而且学好Python还能契合未来发展趋势——人工智能、机器学习、深度学习等。 小编是一名Python开发工程师,自己整理了一套最新的Python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。如果你也喜欢编程,想通过学习Python转行、做副业或者提升工作效率,这份【最新全套Python学习资料】 一定对你有用!

小编为对Python感兴趣的小伙伴准备了以下籽料 !

对于0基础小白入门:

如果你是零基础小白,想快速入门Python是可以考虑培训的!

学习时间相对较短,学习内容更全面更集中可以找到适合自己的学习方案

包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习、Python量化交易等学习教程。带你从零基础系统性的学好Python!

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

五、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

最新全套【Python入门到进阶资料 & 实战源码 &安装工具】(安全链接,放心点击)

我已经上传至CSDN官方,如果需要可以扫描下方官方二维码免费获取【保证100%免费】 *今天的分享就到这里,喜欢且对你有所帮助的话,记得点赞关注哦~下回见 !

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: