用python实现放烟花

本文介绍了如何使用Python代码创建烟花效果,以供在疫情居家期间欣赏。提供了代码示例和最终效果的展示,鼓励读者关注并点赞。
摘要由CSDN通过智能技术生成

疫情太严重了,有很多小伙伴都不能出门玩耍了。给大家看一个特别的风景吧,Python烟花代码,虽然没有现实中的烟花那么美丽,但是在特殊时期足矣安慰自己了。(https://jq.qq.com/?_wv=1027&k=hDGOjqqw)

在这里插入图片描述

先给大家看一下效果图

在这里插入图片描述

在这里插入图片描述

代码如下

# -*- coding: utf-8 -*-

import math, random,time
import threading
import tkinter as tk
import re
#import uuid

Fireworks=[]
maxFireworks=8
height,width=600,600

class firework(object):
    def __init__(self,color,speed,width,height):
        #uid=uuid.uuid1()
        self.radius=random.randint(2,4)  #粒子半径为2~4像素
        self.color=color   #粒子颜色
        self.speed=speed  #speed是1.5-3.5秒
        self.status=0   #在烟花未爆炸的情况下,status=0;爆炸后,status>=1;当status>100时,烟花的生命期终止
        self.nParticle=random.randint(20,30)  #粒子数量
        self.center=[random.randint(0,width-1),random.randint(0,height-1)]   #烟花随机中心坐标
        self.oneParticle=[]    #原始粒子坐标(100%状态时)
        self.rotTheta=random.uniform(0,2*math.pi)  #椭圆平面旋转角

        #椭圆参数方程:x=a*cos(theta),y=b*sin(theta)
        #ellipsePara=[a,b]

        self.ellipsePara=[random.randint(30,40),random.randint(20,30)]   
        theta=2*math.pi/self
以下是一个简单的 Python 放烟花代码,需要使用 Pygame 模块: ```python import pygame import random # 初始化 Pygame pygame.init() # 设置屏幕大小和标题 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("放烟花啦!") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 定义烟花类 class Firework: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.explosion_radius = 0 self.explosion_speed = random.randint(1, 10) def explode(self): if self.explosion_radius < 100: pygame.draw.circle(screen, self.color, (self.x, self.y), self.explosion_radius) self.explosion_radius += self.explosion_speed else: fireworks.remove(self) # 创建一个烟花列表 fireworks = [] # 循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 点击鼠标左键发射烟花 if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: x, y = event.pos color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) fireworks.append(Firework(x, y, color)) # 更新烟花 for firework in fireworks: firework.explode() # 绘制背景 screen.fill(BLACK) # 绘制烟花 for firework in fireworks: pygame.draw.circle(screen, firework.color, (firework.x, firework.y), 5) # 刷新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() ``` 运行代码后,点击鼠标左键即可发射烟花。注意需要在 Pygame 环境下运行该代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值