赞
踩
大家好,小编来为大家解答以下问题,python绘制烟花特定爆炸效果,python炫酷烟花表白源代码,现在让我们一起来看看吧!
- import pygame
- import random
- import math
-
- # 屏幕宽度
- SCREEN_WIDTH = 1350
- SCREEN_HEIGHT = 800
-
- # 烟花颜色
- COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]
-
-
- class Particle:
- def __init__(self, x, y, angle, speed):
- self.x = x
- self.y = y
- self.angle = angle
- self.speed = speed
- self.size = random.randint(1, 2) # 粒子大小
- self.color = random.choice(COLORS)
- self.lifetime = random.randint(1 * 60, 2 * 60) # 粒子生命周期在1-2秒之间
- self.active = True
-
- def update(self):
- self.lifetime -= 1
- if self.lifetime <= 0:
- self.active = False
- return False
-
- dx = math.cos(self.angle) * self.speed
- dy = math.sin(self.angle) * self.speed
- self.x += dx
- self.y += dy
- return True
-
-
- class Firework:
- def __init__(self, x, y, color):
- self.x = x
- self.y = y
- self.color = color
- self.particles = []
- for _ in range(random.randint(10, 300)): # 粒子数量
- angle = random.uniform(0, 2 * math.pi)
- speed = random.uniform(0, 3) # 粒子中心,粒子速度
- particle = Particle(self.x, self.y, angle, speed)
- self.particles.append(particle)
-
- def update(self):
- for particle in self.particles:
- if not particle.update():
- self.particles.remove(particle)
-
- def draw(self, screen):
- for particle in self.particles:
- pygame.draw.circle(screen, particle.color, (int(particle.x), int(particle.y)), particle.size)
-
-
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('Fireworks')
- clock = pygame.time.Clock()
-
- fireworks = []
- firework_count = 0 # 用于控制烟花的创建间隔
- running = True
-
- while running:
- clock.tick(60) # 每秒60帧
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- running = False
- # 屏幕背景
- screen.fill((0, 0, 0))
-
- for firework in fireworks:
- firework.update()
- firework.draw(screen)
-
- if not firework.particles: # 如果烟花的所有粒子都消失,则移除烟花
- fireworks.remove(firework)
-
- if firework_count == 0: # 控制烟花的创建间隔
- fireworks.append(
- Firework(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT), random.choice(COLORS)))
- firework_count = random.randint(15, 20) # 随机设置计数器,实现无序效果,烟花的出现间隔为1-2秒
- else:
- firework_count -= 1
-
- pygame.display.flip()
-
- pygame.quit()
安装PyInstaller:在命令行中执行pip install pyinstaller
命令即可安装。
1.打包项目:在命令行中进入项目所在目录,执行pyinstaller --onefile your_.py
命令即可开始打包熟练掌握Python就业是不是好一点。其中--onefile
表示将项目打包成一个可执行文件,your_.py
为项目入口文件。
2.会生成一个dist的目录,里面就是Python生成的exe文件。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。