用python编写程序,条件:需要烟花效果,烟花绽放出的形状有“祝小龙天天开心,万事胜意!”的字样
时间: 2024-01-06 13:05:32 浏览: 32
可以使用Python的pygame模块来实现烟花效果,并在烟花绽放出的时候绘制出“祝小龙天天开心,万事胜意!”的字样。
以下是一个简单的示例代码:
```
import pygame
import random
# 初始化pygame模块
pygame.init()
# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
# 定义烟花颜色
colors = [(255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]
# 定义字体和字号
font = pygame.font.SysFont("arial", 36)
# 定义文字颜色
text_color = (255, 255, 255)
# 定义烟花绽放函数
def explode(x, y):
particles = []
for i in range(100):
particles.append([x, y, random.randint(0, 20), random.randint(0, 360), random.randint(5, 15), random.choice(colors)])
for particle in particles:
while particle[2] > 0:
particle[0] += particle[4] * math.sin(math.radians(particle[3]))
particle[1] -= particle[4] * math.cos(math.radians(particle[3]))
particle[2] -= 1
particle[4] -= 0.05
pygame.draw.circle(screen, particle[5], (int(particle[0]), int(particle[1])), 2)
# 游戏循环
while True:
# 处理退出事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 绘制背景
screen.fill((0, 0, 0))
# 获取鼠标位置
mx, my = pygame.mouse.get_pos()
# 绘制文字
text = font.render("祝小龙天天开心,万事胜意!", True, text_color)
text_rect = text.get_rect(center=(mx, my))
screen.blit(text, text_rect)
# 如果鼠标左键按下,则绽放烟花
if pygame.mouse.get_pressed()[0]:
explode(mx, my)
# 更新屏幕
pygame.display.update()
```
这段代码使用pygame模块实现了烟花效果,并在烟花绽放出的时候绘制出“祝小龙天天开心,万事胜意!”的字样。你可以将代码复制到你的Python环境中运行,并根据需要进行修改和调整。