序号 | 直达链接 |
---|---|
Tkinter | |
1 | Python李峋同款可写字版跳动的爱心 |
2 | Python跳动的双爱心 |
3 | Python蓝色跳动的爱心 |
4 | Python动漫烟花 |
5 | Python粒子烟花 |
Turtle | |
1 | Python满屏飘字 |
2 | Python蓝色流星雨 |
3 | Python金色流星雨 |
4 | Python漂浮爱心 |
5 | Python爱心光波① |
6 | Python爱心光波② |
7 | Python满天繁星 |
8 | Python五彩气球 |
9 | Python白色飘雪 |
10 | Python七彩花朵 |
11 | Python 3D星空 |
12 | Python大雪纷飞 |
13 | Python一闪一闪亮星星 |
14 | Python爱心泡泡 |
15 | Python爱心射线 |
16 | Python圣诞礼物 |
17 | Python礼物圣诞树 |
18 | Python浪漫星空 |
19 | Python飞舞蝙蝠 |
20 | Python万圣礼物 |
21 | Python蓝色飘雪 |
Pygame | |
1 | Python跨年烟花 |
2 | Python炫酷烟花 |
3 | Python黑客帝国字母雨 |
敬请期待…… |
Python实现炫酷新春烟花动画的完整代码。
Pygame库
粒子系统
物理模拟
update()
方法中调整垂直速度 vy
来模拟重力对粒子的影响,使粒子逐渐加速下落,模仿现实生活中烟花爆炸后物体的运动轨迹。随机数生成
图形绘制
pygame.draw.circle()
函数绘制粒子和文字,生动展现烟花动态效果及“Happy New Year!”的祝福语。pygame.font.SysFont()
用于加载系统字体并绘制文本内容。事件处理
pygame.event.get()
捕获并处理用户事件,如关闭窗口等操作,使程序能够响应用户输入,增强应用的交互体验。动画控制
clock.tick(FPS)
)确保动画流畅运行,并借助 time.sleep(0.05)
调整每帧更新速率,使烟花效果更加清晰呈现。面向对象编程(OOP)
Particle
和 Firework
类,采用面向对象编程理念,将粒子和烟花的行为封装于不同类中,提升代码结构化程度,便于维护与扩展。import pygame
import random
import math
import time
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 700
BACKGROUND_COLOR = (0, 0, 0) # Black
FPS = 60
# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('新春烟花')
# Font setup using system default font
font = pygame.font.SysFont("Comic Sans MS", 100) # You can replace "Arial" with any system font
text_color = (255, 255, 255) # White color for the text
# Particle class
class Particle:
def __init__(self, x, y, color, angle, speed, life):
self.x = x
self.y = y
self.color = color
self.angle = angle
self.speed = speed
self.life = life
self.radius = 2
self.vx = math.cos(angle) * speed
self.vy = math.sin(angle) * speed
def update(self):
self.x += self.vx
self.y += self.vy
self.life -= 1
self.vy += 0.05 # Gravity effect
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
# Firework class
class Firework:
def __init__(self, x, y, color, num_particles=100, spread=2 * math.pi):
self.x = x
self.y = y
self.color = color
self.num_particles = num_particles
self.spread = spread
self.particles = []
self.exploded = False
def explode(self):
if not self.exploded:
for _ in range(self.num_particles):
angle = random.uniform(0, self.spread)
speed = random.uniform(1, 3)
life = random.randint(30, 50)
particle = Particle(self.x, self.y, self.color, angle, speed, life)
self.particles.append(particle)
self.exploded = True
……
这段代码运用Python的Pygame库,精心设计了一个动态的新春烟花展示效果。程序不仅模拟了烟花的发射、爆炸及粒子运动,还在屏幕中央呈现了“Happy New Year!”的温馨祝福。接下来,我们将从程序结构、核心功能、代码实现和细节等方面进行深入解析。
该程序由几个关键部分构成:
pygame.init()
此行代码启动Pygame库,使程序能够利用其绘图、音效处理和事件管理等功能。
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 700
BACKGROUND_COLOR = (0, 0, 0) # 黑色背景
FPS = 60
这里定义了窗口尺寸、背景颜色和帧率等常量,为程序的整体布局和动画流畅性奠定基础。
创建并设置Pygame窗口:
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('新春烟花')
pygame.display.set_mode()
创建指定尺寸的屏幕,pygame.display.set_caption()
则将窗口标题设为“新春烟花”。
font = pygame.font.SysFont("Comic Sans MS", 100)
text_color = (255, 255, 255) # 白色文字
通过pygame.font.SysFont()
选择“Comic Sans MS”字体,字号设为100,用于渲染屏幕上的祝福文字,文字颜色为白色。
Particle
类描述烟花中的单个粒子,包含位置、颜色、速度、角度和生命周期等属性。
class Particle:
def __init__(self, x, y, color, angle, speed, life):
self.x = x
self.y = y
self.color = color
self.angle = angle
self.speed = speed
self.life = life
self.radius = 2
self.vx = math.cos(angle) * speed
self.vy = math.sin(angle) * speed
update()
方法更新粒子位置,考虑重力影响:
def update(self):
self.x += self.vx
self.y += self.vy
self.life -= 1
self.vy += 0.05 # 模拟重力
draw()
方法绘制粒子:
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
Firework
类模拟烟花爆炸过程及粒子管理。
class Firework:
def __init__(self, x, y, color, num_particles=100, spread=2 * math.pi):
self.x = x
self.y = y
self.color = color
self.num_particles = num_particles
self.spread = spread
self.particles = []
self.exploded = False
explode()
方法生成爆炸时的粒子:
def explode(self):
if not self.exploded:
for _ in range(self.num_particles):
angle = random.uniform(0, self.spread)
speed = random.uniform(1, 3)
life = random.randint(30, 50)
particle = Particle(self.x, self.y, self.color, angle, speed, life)
self.particles.append(particle)
self.exploded = True
update()
方法更新粒子状态并检查是否已爆炸:
def update(self):
if not self.exploded:
self.explode()
for particle in self.particles:
particle.update()
draw()
方法绘制所有粒子:
def draw(self):
for particle in self.particles:
if particle.life > 0:
particle.draw()
主循环是程序的核心,负责事件处理、烟花发射、状态更新和文字显示。
此代码通过Pygame实现了生动的烟花效果,利用粒子系统模拟烟花爆炸后的动态行为,结合随机化技术确保每次爆炸的独特性。此外,主循环实时更新烟花状态并展示祝福文字,营造出浓厚的节日氛围。
我是一只有趣的兔子,感谢你的喜欢!