import pygame
import random
import math
import time
# 屏幕宽度
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))
# 设置屏幕大小
screen = pygame.display.set_mode((800, 600))
# 创建一个字体对象
font_path = "ttf/SourceHanSansCN-Bold.ttf"
font_size = 32
# 创建宋体字体对象
font = pygame.font.Font(font_path, font_size)
# 渲染文本
text = font.render("新年好!", True, (255, 255, 255))
# 获取文本的矩形区域
text_rect = text.get_rect()
# 将文本绘制到屏幕上
text_rect.center = (400, 300)
screen.blit(text, text_rect)
# 更新屏幕显示
pygame.display.flip()
time.sleep(2)
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()
没有合适的资源?快使用搜索试试~ 我知道了~
python烟花修,过年烟花
共8个文件
ttf:7个
py:1个
需积分: 1 0 下载量 4 浏览量
2024-01-26
16:59:09
上传
评论
收藏 45.53MB ZIP 举报
温馨提示
python写的一个烟花效果
资源推荐
资源详情
资源评论
收起资源包目录
python烟花秀.zip (8个子文件)
python烟花秀
ttf
SourceHanSansCN-Heavy.ttf 10.26MB
SourceHanSansCN-Bold.ttf 10.18MB
SourceHanSansCN-Regular.ttf 10.28MB
SourceHanSansCN-Normal.ttf 10.31MB
SourceHanSansCN-Light.ttf 10.36MB
SourceHanSansCN-ExtraLight.ttf 10.38MB
SourceHanSansCN-Medium.ttf 10.22MB
flower.py 3KB
共 8 条
- 1
资源评论
xiaozixiaoya
- 粉丝: 14
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功