序号 | 直达链接 |
---|---|
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.display.set_mode
初始化窗口,pygame.display.set_caption
修改标题,screen.fill
清空画布。pygame.event.get
监听系统事件,包括关闭程序(pygame.QUIT
)和键盘输入(pygame.KEYDOWN
)。pygame.time.Clock
控制刷新频率,确保动画播放顺畅。random.randint
和random.uniform
随机确定烟花色调、起始坐标、飞行高度、上升速率及绽放范围。self.y
)和爆炸范围(self.explode_radius
)实现烟花飞行与绽放效果。math.radians
)后配合三角函数(math.cos
/math.sin
)计算爆炸碎片的运动轨迹。Firework
类整合烟花的所有特性与动作(包括移动、渲染和状态重置),保持代码模块化。self.exploded
)区分烟花的上升阶段与爆炸阶段。import pygame
import random
import math
# 初始化 Pygame
pygame.init()
# 定义窗口大小
WIDTH, HEIGHT = 1520, 760 # 设置为全屏分辨率
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # 移除全屏,保留窗口模式
pygame.display.set_caption("炫酷烟花")
# 定义颜色
BLACK = (0, 0, 0)
# 烟花类
class Firework:
def __init__(self):
self.x = random.randint(0, WIDTH)
self.y = HEIGHT + random.randint(50, 250)
self.end_y = random.randint(0, HEIGHT)
self.radius = random.randint(120, 170)
self.explode_radius = 0
self.color = random.choice([
(255, 0, 0), (210, 190, 255), (255, 120, 0),
(255, 0, 150), (255, 240, 100), (10, 255, 255),
(160, 10, 255), (255, 200, 60)
])
self.exploded = False
self.speed = random.uniform(2, 5) # 随机上升速度
def move(self):
if self.y > self.end_y:
self.y -= self.speed
else:
if self.explode_radius >= self.radius:
self.reset()
else:
self.explode_radius += 5
……
以下是重写后的文本:
这段代码利用Pygame库开发了一个烟花模拟动画程序,以下是代码的详细解析。
该程序实现的主要功能包括:
核心模块组成:
pygame.init()
SCREEN_WIDTH, SCREEN_HEIGHT = 1520, 760
display = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("烟花模拟器")
pygame.init()
初始化所有必需模块特点:
BACKGROUND = (0, 0, 0)
优势:
class Firework:
def __init__(self):
self.position_x = random.randint(0, SCREEN_WIDTH)
self.position_y = SCREEN_HEIGHT + random.randint(50, 250)
self.target_height = random.randint(0, SCREEN_HEIGHT)
self.max_radius = random.randint(120, 170)
self.current_radius = 0
self.colors = random.choice([...])
self.has_exploded = False
self.velocity = random.uniform(2, 5)
属性说明:
def update_position(self):
if self.position_y > self.target_height:
self.position_y -= self.velocity
else:
if self.current_radius >= self.max_radius:
self.reset()
else:
self.current_radius += 5
行为逻辑:
def render(self, display):
if not self.has_exploded:
pygame.draw.circle(display, self.colors, (self.position_x, self.position_y), 5)
else:
for angle in range(0, 360, 30):
radians = math.radians(angle)
for size in range(5, 0, -1):
offset_x = math.cos(radians) * (self.current_radius + size * 10)
offset_y = math.sin(radians) * (self.current_radius + size * 10)
particle_color = [...]
pygame.draw.circle(display, particle_color, (int(self.position_x + offset_x), int(self.position_y + offset_y)), size)
绘制方法:
def reset(self):
self.__init__()
重置机制:
firework_objects = [Firework() for _ in range(50)]
运行逻辑:
结构合理:
视觉效果出色:
随机元素丰富:
交互便捷:
该代码通过Pygame实现了高质量的烟花动画效果,其清晰的架构和丰富的视觉表现使其成为图形编程的示范案例。
我是一只有趣的兔子,感谢你的喜欢!