用python写一个带有名字的烟花
时间: 2023-02-17 10:25:31 浏览: 154
可以使用 Pygame 库在 Python 中绘制烟花。以下是一个简单的示例代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置窗口大小和标题
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("烟花")
# 载入字体
font = pygame.font.Font(None, 36)
# 创建烟花粒子列表
particles = []
# 用于控制程序循环
running = True
# 游戏循环
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 添加新的烟花粒子
if random.randint(0, 10) == 0:
x = random.randint(0, size[0])
y = random.randint(0, size[1])
particles.append([x, y])
# 清除屏幕
screen.fill((0, 0, 0))
# 在屏幕上绘制烟花
for i in range(len(particles)):
pygame.draw.circle(screen, (255, 255, 255), particles[i], 2)
particles[i][1] += 1
# 在屏幕上绘制文本
text = font.render("名字:Fireworks", True, (255, 255, 255))
screen.blit(text, (10, 10))
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
```
这段代码使用了 Pygame 库来创建烟花效果,并使用随机数来控制烟花粒子的出现。可以在屏幕上绘制文本"名字:Fireworks"
请注意,这只是一个简单的示例,可以根据需要进行修改和扩展。