使用python写一个脚本,实现满屏放烟花的效果

好的,这里是一个示例代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕的大小
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置窗口标题
pygame.display.set_caption("烟花")

# 设置烟花颜色,这里使用了一些随机颜色
colors = [(255, 0, 0), (255, 165, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255), (75, 0, 130), (238, 130, 238)]

# 定义烟花类
class Firework:
    def __init__(self):
        # 随机设置烟花的起始位置
        self.x = random.randint(0, screen_width)
        self.y = screen_height

        # 随机设置烟花颜色
        self.color = random.choice(colors)

        # 随机设置烟花的速度
        self.speed = random.randint(5, 10)

    # 画出烟花
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), 5)

    # 烟花上升
    def move(self):
        self.y -= self.speed

# 存储所有的烟花
fireworks = []

# 循环播放烟花
while True:
    # 每次循环时都重新绘制屏幕
    screen.fill((0, 0, 0))

    # 随机生成新的烟花
    if random.randint(0, 50) == 0:
        fireworks.append(Firework())

    # 画出所有的烟花
    for f in fireworks:
        f.draw()
        f.move()

    # 更新屏幕
    pygame.display.flip()

    # 处理退出事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

这段代码使用了pygame库来实现满屏放烟花的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值