赞
踩
完整实例:
import pygame as pg``import random as ra``import math`` ``# 初始化pygame``pg.init()``pg.display.set_caption("新年快乐")`` ``# 获取屏幕尺寸``winScreen = pg.display.Info()``screenWidth = winScreen.current_w``screenHeight = winScreen.current_h`` ``# 矢量辅助函数``vector = pg.math.Vector2`` ``# 烟花尾迹颜色``trail_colors = [(128, 0, 0), (139, 0, 0), (165, 42, 42), (178, 34, 34), (220, 20, 60)]` ` ``class Firework:` `"""定义烟花的类"""` `def __init__(self):` `self.colour = (ra.randint(200, 255), ra.randint(0, 60), ra.randint(0, 60))` `self.colours = (` `(ra.randint(200, 255), ra.randint(0, 60), ra.randint(0, 60)),` `(ra.randint(200, 255), ra.randint(0, 60), ra.randint(0, 60)),` `(ra.randint(200, 255), ra.randint(0, 60), ra.randint(0, 60))` `)` `self.firework = Particle(ra.randint(0, screenWidth), screenHeight, True, self.colour)` `self.exploded = False` `self.particles = []` `self.min_max_particles = vector(666, 999)`` ` `def update(self, win):` `"""更新烟花状态"""` `if not self.exploded:` `self.firework.apply_force(vector(0, ra.uniform(0.15, 0.4)))` `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(ra.uniform(-1, 1) / 20, ra.uniform(0.05, 0.2)))` `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:` `self.particles = [p for p in self.particles if not p.remove]` `return len(self.particles) == 0` `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.explosion_radius = ra.randint(15, 25)` `self.remove = False` `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 *= 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):` `"""移动粒子"""` `self.vel += self.acc` `self.pos += self.vel` `self.acc *= 0` `distance = math.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2)` `if distance > self.explosion_radius and not self.firework:` `self.remove = True` `self.decay()` `self.trail_update()` `self.life += 1`` ` `def show(self, win):` `"""显示粒子"""` `pg.draw.circle(win, self.colour, (int(self.pos.x), int(self.pos.y)), self.size)`` ` `def decay(self):` `"""粒子衰减"""` `if self.life > 50:` `if ra.randint(0, 5) == 0:` `self.remove = True`` ` `def trail_update(self):` `"""更新粒子尾迹"""` `self.prev_posx.pop(0)` `self.prev_posx.append(int(self.pos.x))` `self.prev_posy.pop(0)` `self.prev_posy.append(int(self.pos.y))` `for n, t in enumerate(self.trails):` `t.get_pos(self.prev_posx[n], self.prev_posy[n])`` ``class Trail:` `"""定义尾迹的类"""` `def __init__(self, n, size, dynamic):` `self.pos_in_line = n` `self.pos = vector(-10, -10)` `self.dynamic = dynamic` `self.colour = trail_colors[n] if dynamic else (255, 255, 200)` `self.size = max(int(size - n / 2), 0) if dynamic else max(size - 2, 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 _ in range(2)]` `running = True` `font = pg.font.SysFont("simsun", 99, bold=True) # 使用粗体字` `text = "新年快乐!(2024)"` `text_color = (255, 0, 0) # 使用鲜艳的红色` `rendered_text = font.render(text, True, text_color)` `while running:` `clock.tick(30)` `for event in pg.event.get():` `if event.type == pg.QUIT:` `running = False` `screen.fill((20, 20, 30))` `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.blit(rendered_text, (text_x, text_y))` `if ra.randint(0, 10) == 1:` `fireworks.append(Firework())` `update(screen, fireworks)` `pg.quit()`` ``if __name__ == "__main__":` `fire()
以上就是“Python新年烟花效果代码(2024版)”的全部内容,希望对你有所帮助。
关于Python技术储备
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python必备开发工具
三、Python视频合集
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、Python练习题
检查学习结果。
六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
最后祝大家天天进步!!
上面这份完整版的Python全套学习资料已经上传至CSDN官方,朋友如果需要可以直接微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。