python的烟花特效代码
时间: 2023-12-27 12:25:45 浏览: 88
以下是一个简单的Python烟花特效代码示例:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Fireworks")
# 定义烟花粒子类
class FireworkParticle(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((2, 2))
self.image.fill((255, 255, 255))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speed_x = random.randint(-5, 5)
self.speed_y = random.randint(-15, -5)
self.gravity = 0.5
def update(self):
self.speed_y += self.gravity
self.rect.x += self.speed_x
self.rect.y += self.speed_y
# 创建烟花粒子组
particles = pygame.sprite.Group()
# 游戏主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标点击位置
mouse_pos = pygame.mouse.get_pos()
# 创建一组烟花粒子
for _ in range(100):
particle = FireworkParticle(*mouse_pos)
particles.add(particle)
# 更新烟花粒子
particles.update()
# 绘制背景
screen.fill((0, 0, 0))
# 绘制烟花粒子
particles.draw(screen)
# 更新屏幕显示
pygame.display.flip()
# 退出游戏
pygame.quit()
```
阅读全文