用python绘制出烟花
时间: 2023-11-19 22:06:07 浏览: 63
抱歉,作为语言模型AI,我无法绘制出烟花。但是,我可以提供一份Python代码,可以在Python中实现一个烟花效果。你可以尝试运行以下代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义窗口大小
width, height = 800, 600
# 创建窗口
screen = pygame.display.set_mode((width, height))
# 设置窗口标题
pygame.display.set_caption("Fireworks")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义烟花粒子类
class Particle:
def __init__(self, x, y, size):
self.x = x
self.y = y
self.size = size
self.colour = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
def display(self):
pygame.draw.circle(screen, self.colour, (self.x, self.y), self.size)
def move(self):
self.x += random.randint(-5, 5)
self.y += random.randint(-5, 5)
self.size -= 0.1
# 定义烟花类
class Firework:
def __init__(self):
self.particles = []
self.exploded = False
self.colour = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for i in range(100):
self.particles.append(Particle(random.randint(0, width), height, random.randint(2, 5)))
def display(self):
if not self.exploded:
pygame.draw.circle(screen, self.colour, (self.particles[0].x, self.particles[0].y), 5)
for particle in self.particles:
particle.display()
def explode(self):
self.exploded = True
for particle in self.particles:
particle.colour = self.colour
speed = random.randint(1, 10)
angle = random.uniform(0, 2 * 3.14)
particle.xspeed = speed * math.cos(angle)
particle.yspeed = speed * math.sin(angle)
def move(self):
if not self.exploded:
self.particles[0].y -= 10
if self.particles[0].y <= 0:
self.explode()
else:
for particle in self.particles:
particle.move()
# 创建烟花列表
fireworks = []
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 添加新烟花
if random.randint(1, 10) == 1:
fireworks.append(Firework())
# 绘制背景
screen.fill(BLACK)
# 显示和移动烟花
for firework in fireworks:
firework.display()
firework.move()
# 删除烟花
fireworks = [firework for firework in fireworks if not firework.exploded]
# 更新屏幕
pygame.display.update()
```
这是一份简单的代码,可能不是很完美,但它可以帮助你开始制作自己的烟花效果。
阅读全文