一、准备工作
(1)、导入必要的模块:
代码首先导入了需要使用的模块:requests、lxml和csv。
import requestsfrom lxml import etreeimport csv
如果出现模块报错
进入控制台输入:建议使用国内镜像源
pip install 模块名称 -i Https://mirrors.aliyun.com/pypi/simple
我大致罗列了以下几种国内镜像源:
清华大学https://pypi.tuna.tsinghua.edu.cn/simple阿里云https://mirrors.aliyun.com/pypi/simple/豆瓣https://pypi.douban.com/simple/ 百度云https://mirror.baidu.com/pypi/simple/中科大https://pypi.mirrors.ustc.edu.cn/simple/华为云https://mirrors.huaweicloud.com/repository/pypi/simple/腾讯云https://mirrors.cloud.tencent.com/pypi/simple/
(2) 、定义粒子类
接下来,我们定义一个粒子类,每个粒子具有位置、颜色、半径、角度、速度、重力和生命周期等属性。我们还为粒子类添加更新和绘制方法。
class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = 3 self.angle = randint(0, 360) self.speed = randint(1, 5) self.gravity = 0.1 self.life = randint(20, 25) def update(self): if self.life > 0: radian = math.radians(self.angle) self.x += self.speed * math.cos(radian) self.y -= self.speed * math.sin(radian) self.speed -= self.gravity self.life -= 1 def draw(self): pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), self.radius)
(3)、定义烟花类
接下来,我们定义一个烟花类,每个烟花具有位置、颜色、粒子列表和是否已经爆炸的属性。我们为烟花类添加爆炸和更新方法,并在绘制方法中绘制烟花本身。
class Firework: def __init__(self): self.x = randint(100, DISPLAY_WIDTH - 100) self.y = DISPLAY_HEIGHT self.color = (randint(0, 255), randint(0, 255), randint(0, 255)) self.particles = [] self.exploded = False def explode(self): for _ in range(100): particle = Particle(self.x, self.y, self.color) self.particles.append(particle) def update(self): if not self.exploded: self.y -= 3 if self.y <= randint(200, 400): self.explode() self.exploded = True else: for particle in self.particles: particle.update() def draw(self): pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), 5)
(4)、游戏主循环
在主循环中,我们处理退出事件,清空窗口,更新和绘制每个烟花及其粒子,移除完成的烟花和消失的粒子,并更新显示。
# 创建烟花列表fireworks = []# 游戏主循环running = Trueclock = pygame.time.Clock()while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False win.fill(BLACK) # 添加新的烟花 if len(fireworks) < 10 and randint(0, 100) < 2: fireworks.append(Firework()) # 更新和绘制烟花 for firework in fireworks: firework.update() firework.draw() for particle in firework.particles: particle.draw() # 移除完成的烟花及消失的粒子 fireworks = [firework for firework in fireworks if not firework.exploded or len(firework.particles) > 0] for firework in fireworks: firework.particles = [particle for particle in firework.particles if particle.life > 0] pygame.display.update()pygame.quit()
英杰社区https://bbs.csdn.net/topics/617804998
二、完整代码:
import pygameimport mathfrom random import randint, choice# 初始化 Pygamepygame.init()# 设置窗口大小和标题DISPLAY_WIDTH = 800DISPLAY_HEIGHT = 600win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))pygame.display.set_caption("烟花")# 定义颜色WHITE = (255, 255, 255)BLACK = (0, 0, 0)# 定义粒子类class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = 3 self.angle = randint(0, 360) self.speed = randint(1, 5) self.gravity = 0.1 self.life = randint(20, 25) def update(self): if self.life > 0: radian = math.radians(self.angle) self.x += self.speed * math.cos(radian) self.y -= self.speed * math.sin(radian) self.speed -= self.gravity self.life -= 1 def draw(self): pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), self.radius)# 定义烟花类class Firework: def __init__(self): self.x = randint(100, DISPLAY_WIDTH - 100) self.y = DISPLAY_HEIGHT self.color = (randint(0, 255), randint(0, 255), randint(0, 255)) self.particles = [] self.exploded = False def explode(self): for _ in range(100): particle = Particle(self.x, self.y, self.color) self.particles.append(particle) def update(self): if not self.exploded: self.y -= 3 if self.y <= randint(200, 400): self.explode() self.exploded = True else: for particle in self.particles: particle.update() def draw(self): pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), 5)# 创建烟花列表fireworks = []# 游戏主循环running = Trueclock = pygame.time.Clock()while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False win.fill(BLACK) # 添加新的烟花 if len(fireworks) < 10 and randint(0, 100) < 2: fireworks.append(Firework()) # 更新和绘制烟花 for firework in fireworks: firework.update() firework.draw() for particle in firework.particles: particle.draw() # 移除完成的烟花及消失的粒子 fireworks = [firework for firework in fireworks if not firework.exploded or len(firework.particles) > 0] for firework in fireworks: firework.particles = [particle for particle in firework.particles if particle.life > 0] pygame.display.update()pygame.quit()
通过上述步骤,我们已经成功创建了一个令人惊叹的烟花盛典。在这个过程中,我们学习了如何使用 Pygame 库和 Python 编程,创建粒子类和烟花类,并在主循环中更新和绘制烟花效果。